Skip to content
Browse files

[py3] Fixed content encoding in test client

Thanks Andrews Medina for the initial patch.
  • Loading branch information...
1 parent 6d68022 commit a06503d09b73fa2842ec79170b84453034fca904 @claudep claudep committed
Showing with 23 additions and 20 deletions.
  1. +1 −0 AUTHORS
  2. +22 −20 django/test/client.py
View
1 AUTHORS
@@ -373,6 +373,7 @@ answer newbie questions, and generally made Django that much better:
michael.mcewan@gmail.com
Paul McLanahan <paul@mclanahan.net>
Tobias McNulty <http://www.caktusgroup.com/blog>
+ Andrews Medina <andrewsmedina@gmail.com>
Zain Memon
Christian Metts
michal@plovarna.cz
View
42 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()
]

0 comments on commit a06503d

Please sign in to comment.
Something went wrong with that request. Please try again.