summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorThomas Tanner <tanner@gmx.net>2014-11-29 17:41:06 +0100
committerTim Graham <timograham@gmail.com>2014-12-04 07:42:03 -0500
commit018d110ef547acb71b0526d0ff934e1e476244e4 (patch)
treef8abd2126879aa8f9f543be5be7847027e1c549e /django
parentdc2d75f4d43ca735bfdded52ff90458ebecb9348 (diff)
Fixed #23911 -- Added support for buffer file uploads in the test client
Diffstat (limited to 'django')
-rw-r--r--django/test/client.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/django/test/client.py b/django/test/client.py
index b2633bd7e7..62f7c5fb6c 100644
--- a/django/test/client.py
+++ b/django/test/client.py
@@ -195,20 +195,25 @@ def encode_multipart(boundary, data):
def encode_file(boundary, key, file):
to_bytes = lambda s: force_bytes(s, settings.DEFAULT_CHARSET)
+ filename = os.path.basename(file.name) if hasattr(file, 'name') else ''
if hasattr(file, 'content_type'):
content_type = file.content_type
+ elif filename:
+ content_type = mimetypes.guess_type(filename)[0]
else:
- content_type = mimetypes.guess_type(file.name)[0]
+ content_type = None
if content_type is None:
content_type = 'application/octet-stream'
+ if not filename:
+ filename = key
return [
to_bytes('--%s' % boundary),
to_bytes('Content-Disposition: form-data; name="%s"; filename="%s"'
- % (key, os.path.basename(file.name))),
+ % (key, filename)),
to_bytes('Content-Type: %s' % content_type),
b'',
- file.read()
+ to_bytes(file.read())
]