Fixed #11159 -- Added mimetype detection to the test client for file uploads. Thanks to notanumber for the report and patch, and lomin for the test case.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13517 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
7c03f1c97a
commit
917f433727
|
@ -3,6 +3,7 @@ from urlparse import urlparse, urlunparse, urlsplit
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import mimetypes
|
||||||
try:
|
try:
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -138,11 +139,14 @@ def encode_multipart(boundary, data):
|
||||||
|
|
||||||
def encode_file(boundary, key, file):
|
def encode_file(boundary, key, file):
|
||||||
to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
|
to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
|
||||||
|
content_type = mimetypes.guess_type(file.name)[0]
|
||||||
|
if content_type is None:
|
||||||
|
content_type = 'application/octet-stream'
|
||||||
return [
|
return [
|
||||||
'--' + boundary,
|
'--' + boundary,
|
||||||
'Content-Disposition: form-data; name="%s"; filename="%s"' \
|
'Content-Disposition: form-data; name="%s"; filename="%s"' \
|
||||||
% (to_str(key), to_str(os.path.basename(file.name))),
|
% (to_str(key), to_str(os.path.basename(file.name))),
|
||||||
'Content-Type: application/octet-stream',
|
'Content-Type: %s' % content_type,
|
||||||
'',
|
'',
|
||||||
file.read()
|
file.read()
|
||||||
]
|
]
|
||||||
|
|
|
@ -11,6 +11,7 @@ from django.core.urlresolvers import reverse
|
||||||
from django.core.exceptions import SuspiciousOperation
|
from django.core.exceptions import SuspiciousOperation
|
||||||
from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
|
from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
|
||||||
from django.template import loader
|
from django.template import loader
|
||||||
|
from django.test.client import encode_file
|
||||||
|
|
||||||
class AssertContainsTests(TestCase):
|
class AssertContainsTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -823,3 +824,26 @@ class UnicodePayloadTests(TestCase):
|
||||||
response = self.client.post("/test_client_regress/parse_unicode_json/", json,
|
response = self.client.post("/test_client_regress/parse_unicode_json/", json,
|
||||||
content_type="application/json; charset=koi8-r")
|
content_type="application/json; charset=koi8-r")
|
||||||
self.assertEqual(response.content, json.encode('koi8-r'))
|
self.assertEqual(response.content, json.encode('koi8-r'))
|
||||||
|
|
||||||
|
class DummyFile(object):
|
||||||
|
def __init__(self, filename):
|
||||||
|
self.name = filename
|
||||||
|
def read(self):
|
||||||
|
return 'TEST_FILE_CONTENT'
|
||||||
|
|
||||||
|
class UploadedFileEncodingTest(TestCase):
|
||||||
|
def test_file_encoding(self):
|
||||||
|
encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', DummyFile('test_name.bin'))
|
||||||
|
self.assertEqual('--TEST_BOUNDARY', encoded_file[0])
|
||||||
|
self.assertEqual('Content-Disposition: form-data; name="TEST_KEY"; filename="test_name.bin"', encoded_file[1])
|
||||||
|
self.assertEqual('TEST_FILE_CONTENT', encoded_file[-1])
|
||||||
|
|
||||||
|
def test_guesses_content_type_on_file_encoding(self):
|
||||||
|
self.assertEqual('Content-Type: application/octet-stream',
|
||||||
|
encode_file('IGNORE', 'IGNORE', DummyFile("file.bin"))[2])
|
||||||
|
self.assertEqual('Content-Type: text/plain',
|
||||||
|
encode_file('IGNORE', 'IGNORE', DummyFile("file.txt"))[2])
|
||||||
|
self.assertEqual('Content-Type: application/zip',
|
||||||
|
encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2])
|
||||||
|
self.assertEqual('Content-Type: application/octet-stream',
|
||||||
|
encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2])
|
||||||
|
|
Loading…
Reference in New Issue