Fixed #23333 -- Made urlsafe_base64_decode() return proper type on Python 3.

This commit is contained in:
Ian Foote 2014-08-21 12:53:22 +01:00 committed by Tim Graham
parent f9c7732e96
commit 03d89168a2
2 changed files with 8 additions and 2 deletions

View File

@ -10,7 +10,7 @@ from binascii import Error as BinasciiError
from email.utils import formatdate from email.utils import formatdate
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
from django.utils.encoding import force_str, force_text from django.utils.encoding import force_bytes, force_str, force_text
from django.utils.functional import allow_lazy from django.utils.functional import allow_lazy
from django.utils import six from django.utils import six
from django.utils.six.moves.urllib.parse import ( from django.utils.six.moves.urllib.parse import (
@ -227,7 +227,7 @@ def urlsafe_base64_decode(s):
Decodes a base64 encoded string, adding back any trailing equal signs that Decodes a base64 encoded string, adding back any trailing equal signs that
might have been stripped. might have been stripped.
""" """
s = s.encode('utf-8') # base64encode should only return ASCII. s = force_bytes(s)
try: try:
return base64.urlsafe_b64decode(s.ljust(len(s) + len(s) % 4, b'=')) return base64.urlsafe_b64decode(s.ljust(len(s) + len(s) % 4, b'='))
except (LookupError, BinasciiError) as e: except (LookupError, BinasciiError) as e:

View File

@ -119,6 +119,12 @@ class TestUtilsHttp(unittest.TestCase):
'/url%20with%20spaces/'): '/url%20with%20spaces/'):
self.assertTrue(http.is_safe_url(good_url, host='testserver'), "%s should be allowed" % good_url) self.assertTrue(http.is_safe_url(good_url, host='testserver'), "%s should be allowed" % good_url)
def test_urlsafe_base64_roundtrip(self):
bytestring = b'foo'
encoded = http.urlsafe_base64_encode(bytestring)
decoded = http.urlsafe_base64_decode(encoded)
self.assertEqual(bytestring, decoded)
class ETagProcessingTests(unittest.TestCase): class ETagProcessingTests(unittest.TestCase):
def test_parsing(self): def test_parsing(self):