summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2012-08-12 23:25:42 +0200
committerClaude Paroz <claude@2xlibre.net>2012-08-13 09:56:14 +0200
commita06503d09b73fa2842ec79170b84453034fca904 (patch)
treef857368bb73fb10c100a212708dd884876c5b04e /django
parent6d68022a274df2e19205d7313034cf6eb619f346 (diff)
[py3] Fixed content encoding in test client
Thanks Andrews Medina for the initial patch.
Diffstat (limited to 'django')
-rw-r--r--django/test/client.py42
1 files changed, 22 insertions, 20 deletions
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()
]