[py3] Fixed content encoding in test client
Thanks Andrews Medina for the initial patch.
This commit is contained in:
parent
6d68022a27
commit
a06503d09b
1
AUTHORS
1
AUTHORS
|
@ -373,6 +373,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
michael.mcewan@gmail.com
|
michael.mcewan@gmail.com
|
||||||
Paul McLanahan <paul@mclanahan.net>
|
Paul McLanahan <paul@mclanahan.net>
|
||||||
Tobias McNulty <http://www.caktusgroup.com/blog>
|
Tobias McNulty <http://www.caktusgroup.com/blog>
|
||||||
|
Andrews Medina <andrewsmedina@gmail.com>
|
||||||
Zain Memon
|
Zain Memon
|
||||||
Christian Metts
|
Christian Metts
|
||||||
michal@plovarna.cz
|
michal@plovarna.cz
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
@ -108,7 +110,7 @@ def encode_multipart(boundary, data):
|
||||||
as an application/octet-stream; otherwise, str(value) will be sent.
|
as an application/octet-stream; otherwise, str(value) will be sent.
|
||||||
"""
|
"""
|
||||||
lines = []
|
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.
|
# Not by any means perfect, but good enough for our purposes.
|
||||||
is_file = lambda thing: hasattr(thing, "read") and callable(thing.read)
|
is_file = lambda thing: hasattr(thing, "read") and callable(thing.read)
|
||||||
|
@ -124,37 +126,37 @@ def encode_multipart(boundary, data):
|
||||||
if is_file(item):
|
if is_file(item):
|
||||||
lines.extend(encode_file(boundary, key, item))
|
lines.extend(encode_file(boundary, key, item))
|
||||||
else:
|
else:
|
||||||
lines.extend([
|
lines.extend([to_bytes(val) for val in [
|
||||||
'--' + boundary,
|
'--%s' % boundary,
|
||||||
'Content-Disposition: form-data; name="%s"' % to_str(key),
|
'Content-Disposition: form-data; name="%s"' % key,
|
||||||
'',
|
'',
|
||||||
to_str(item)
|
item
|
||||||
])
|
]])
|
||||||
else:
|
else:
|
||||||
lines.extend([
|
lines.extend([to_bytes(val) for val in [
|
||||||
'--' + boundary,
|
'--%s' % boundary,
|
||||||
'Content-Disposition: form-data; name="%s"' % to_str(key),
|
'Content-Disposition: form-data; name="%s"' % key,
|
||||||
'',
|
'',
|
||||||
to_str(value)
|
value
|
||||||
])
|
]])
|
||||||
|
|
||||||
lines.extend([
|
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):
|
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]
|
content_type = mimetypes.guess_type(file.name)[0]
|
||||||
if content_type is None:
|
if content_type is None:
|
||||||
content_type = 'application/octet-stream'
|
content_type = 'application/octet-stream'
|
||||||
return [
|
return [
|
||||||
'--' + to_str(boundary),
|
to_bytes('--%s' % boundary),
|
||||||
'Content-Disposition: form-data; name="%s"; filename="%s"' \
|
to_bytes('Content-Disposition: form-data; name="%s"; filename="%s"' \
|
||||||
% (to_str(key), to_str(os.path.basename(file.name))),
|
% (key, os.path.basename(file.name))),
|
||||||
'Content-Type: %s' % content_type,
|
to_bytes('Content-Type: %s' % content_type),
|
||||||
'',
|
b'',
|
||||||
file.read()
|
file.read()
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue