diff --git a/AUTHORS b/AUTHORS index 0ea0ca6da7..5fa957885c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -373,6 +373,7 @@ answer newbie questions, and generally made Django that much better: michael.mcewan@gmail.com Paul McLanahan Tobias McNulty + Andrews Medina Zain Memon Christian Metts michal@plovarna.cz diff --git a/django/test/client.py b/django/test/client.py index 771f7e0da5..2b61c51ce1 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import sys import os import re @@ -108,7 +110,7 @@ def encode_multipart(boundary, data): as an application/octet-stream; otherwise, str(value) will be sent. """ lines = [] - to_str = lambda s: smart_bytes(s, settings.DEFAULT_CHARSET) + to_bytes = lambda s: smart_bytes(s, settings.DEFAULT_CHARSET) # Not by any means perfect, but good enough for our purposes. is_file = lambda thing: hasattr(thing, "read") and callable(thing.read) @@ -124,37 +126,37 @@ def encode_multipart(boundary, data): if is_file(item): lines.extend(encode_file(boundary, key, item)) else: - lines.extend([ - '--' + boundary, - 'Content-Disposition: form-data; name="%s"' % to_str(key), + lines.extend([to_bytes(val) for val in [ + '--%s' % boundary, + 'Content-Disposition: form-data; name="%s"' % key, '', - to_str(item) - ]) + item + ]]) else: - lines.extend([ - '--' + boundary, - 'Content-Disposition: form-data; name="%s"' % to_str(key), + lines.extend([to_bytes(val) for val in [ + '--%s' % boundary, + 'Content-Disposition: form-data; name="%s"' % key, '', - to_str(value) - ]) + value + ]]) lines.extend([ - '--' + boundary + '--', - '', + to_bytes('--%s--' % boundary), + b'', ]) - return '\r\n'.join(lines) + return b'\r\n'.join(lines) def encode_file(boundary, key, file): - to_str = lambda s: smart_bytes(s, settings.DEFAULT_CHARSET) + to_bytes = lambda s: smart_bytes(s, settings.DEFAULT_CHARSET) content_type = mimetypes.guess_type(file.name)[0] if content_type is None: content_type = 'application/octet-stream' return [ - '--' + to_str(boundary), - 'Content-Disposition: form-data; name="%s"; filename="%s"' \ - % (to_str(key), to_str(os.path.basename(file.name))), - 'Content-Type: %s' % content_type, - '', + to_bytes('--%s' % boundary), + to_bytes('Content-Disposition: form-data; name="%s"; filename="%s"' \ + % (key, os.path.basename(file.name))), + to_bytes('Content-Type: %s' % content_type), + b'', file.read() ]