Permalink
Browse files

Fixed a caching test on Python 3.

  • Loading branch information...
1 parent 6a057e1 commit 6a8f95d812797a0c7af85df27c51f588c8649c69 @carljm carljm committed Feb 26, 2013
Showing with 11 additions and 11 deletions.
  1. +11 −11 tests/regressiontests/cache/tests.py
@@ -1327,7 +1327,7 @@ def test_cache_key_i18n_translation(self):
def check_accept_language_vary(self, accept_language, vary, reference_key):
request = self._get_request()
request.META['HTTP_ACCEPT_LANGUAGE'] = accept_language
- request.META['HTTP_ACCEPT_ENCODING'] = 'gzip;q=1.0, identity; q=0.5, *;q=0'
+ request.META['HTTP_ACCEPT_ENCODING'] = b'gzip;q=1.0, identity; q=0.5, *;q=0'
response = HttpResponse()
response['Vary'] = vary
key = learn_cache_key(request, response)
@@ -1340,53 +1340,53 @@ def test_cache_key_i18n_translation_accept_language(self):
lang = translation.get_language()
self.assertEqual(lang, 'en')
request = self._get_request()
- request.META['HTTP_ACCEPT_ENCODING'] = 'gzip;q=1.0, identity; q=0.5, *;q=0'
+ request.META['HTTP_ACCEPT_ENCODING'] = b'gzip;q=1.0, identity; q=0.5, *;q=0'
response = HttpResponse()
response['Vary'] = 'accept-encoding'
key = learn_cache_key(request, response)
self.assertIn(lang, key, "Cache keys should include the language name when translation is active")
self.check_accept_language_vary(
- 'en-us',
+ b'en-us',
'cookie, accept-language, accept-encoding',
key
)
self.check_accept_language_vary(
- 'en-US',
+ b'en-US',
'cookie, accept-encoding, accept-language',
key
)
self.check_accept_language_vary(
- 'en-US,en;q=0.8',
+ b'en-US,en;q=0.8',
'accept-encoding, accept-language, cookie',
key
)
self.check_accept_language_vary(
- 'en-US,en;q=0.8,ko;q=0.6',
+ b'en-US,en;q=0.8,ko;q=0.6',
'accept-language, cookie, accept-encoding',
key
)
self.check_accept_language_vary(
- 'ko-kr,ko;q=0.8,en-us;q=0.5,en;q=0.3 ',
+ b'ko-kr,ko;q=0.8,en-us;q=0.5,en;q=0.3 ',
'accept-encoding, cookie, accept-language',
key
)
self.check_accept_language_vary(
- 'ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4',
+ b'ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4',
'accept-language, accept-encoding, cookie',
key
)
self.check_accept_language_vary(
- 'ko;q=1.0,en;q=0.5',
+ b'ko;q=1.0,en;q=0.5',
'cookie, accept-language, accept-encoding',
key
)
self.check_accept_language_vary(
- 'ko, en',
+ b'ko, en',
'cookie, accept-encoding, accept-language',
key
)
self.check_accept_language_vary(
- 'ko-KR, en-US',
+ b'ko-KR, en-US',
'accept-encoding, accept-language, cookie',
key
)

2 comments on commit 6a8f95d

Owner

aaugustin replied Feb 26, 2013

This doesn't look correct. request.META is the WSGI environ, which must contain native strings (ie. str objects) per PEP 3333, for example:
request.META['HTTP_ACCEPT_ENCODING'] = str('gzip;q=1.0, identity; q=0.5, *;q=0').

Since Python 2 is more lax with unicode handling, and this string contains only ASCII character, the original version should work: it uses str under Python 3, and unicode that can be converted to str under Python 2.

Owner

carljm replied Feb 26, 2013

Ok, I need to look into this more. Without this change, on Python 3 it was failing when it tries to feed Py3 str objects to hash construction in learn_cache_key, saying they needed to be bytes.

Please sign in to comment.