[py3] Fixed content encoding in test client

Thanks Andrews Medina for the initial patch.
This commit is contained in:
Claude Paroz 2012-08-12 23:25:42 +02:00
parent 6d68022a27
commit a06503d09b
2 changed files with 23 additions and 20 deletions

View File

@ -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 File

@ -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()
]