From 3ccc25b10c1992238f756e27c5f984e6a02f4a86 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 6 Aug 2010 17:16:19 +0000 Subject: [PATCH] [1.2.X] 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. Backport of r13517 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13518 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/test/client.py | 6 ++++- .../test_client_regress/models.py | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/django/test/client.py b/django/test/client.py index e5a16b6e79898..498af5c3444fd 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -3,6 +3,7 @@ import sys import os import re +import mimetypes try: from cStringIO import StringIO except ImportError: @@ -138,11 +139,14 @@ def encode_multipart(boundary, data): def encode_file(boundary, key, file): 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 [ '--' + boundary, 'Content-Disposition: form-data; name="%s"; filename="%s"' \ % (to_str(key), to_str(os.path.basename(file.name))), - 'Content-Type: application/octet-stream', + 'Content-Type: %s' % content_type, '', file.read() ] diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index 6da7ae4445b22..692e0f63d3c4e 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -11,6 +11,7 @@ from django.core.exceptions import SuspiciousOperation from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context from django.template import loader +from django.test.client import encode_file class AssertContainsTests(TestCase): def setUp(self): @@ -821,3 +822,26 @@ def test_unicode_payload_non_utf(self): response = self.client.post("/test_client_regress/parse_unicode_json/", json, content_type="application/json; charset=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])