Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 917f433727b485ea3bb14bb74dcfcbcd5f789de8 1 parent 7c03f1c
Russell Keith-Magee authored August 06, 2010
6  django/test/client.py
@@ -3,6 +3,7 @@
3 3
 import sys
4 4
 import os
5 5
 import re
  6
+import mimetypes
6 7
 try:
7 8
     from cStringIO import StringIO
8 9
 except ImportError:
@@ -138,11 +139,14 @@ def encode_multipart(boundary, data):
138 139
 
139 140
 def encode_file(boundary, key, file):
140 141
     to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
  142
+    content_type = mimetypes.guess_type(file.name)[0]
  143
+    if content_type is None:
  144
+        content_type = 'application/octet-stream'
141 145
     return [
142 146
         '--' + boundary,
143 147
         'Content-Disposition: form-data; name="%s"; filename="%s"' \
144 148
             % (to_str(key), to_str(os.path.basename(file.name))),
145  
-        'Content-Type: application/octet-stream',
  149
+        'Content-Type: %s' % content_type,
146 150
         '',
147 151
         file.read()
148 152
     ]
24  tests/regressiontests/test_client_regress/models.py
@@ -11,6 +11,7 @@
11 11
 from django.core.exceptions import SuspiciousOperation
12 12
 from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
13 13
 from django.template import loader
  14
+from django.test.client import encode_file
14 15
 
15 16
 class AssertContainsTests(TestCase):
16 17
     def setUp(self):
@@ -823,3 +824,26 @@ def test_unicode_payload_non_utf(self):
823 824
         response = self.client.post("/test_client_regress/parse_unicode_json/", json,
824 825
                                     content_type="application/json; charset=koi8-r")
825 826
         self.assertEqual(response.content, json.encode('koi8-r'))
  827
+
  828
+class DummyFile(object):
  829
+    def __init__(self, filename):
  830
+        self.name = filename
  831
+    def read(self):
  832
+        return 'TEST_FILE_CONTENT'
  833
+
  834
+class UploadedFileEncodingTest(TestCase):
  835
+    def test_file_encoding(self):
  836
+        encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', DummyFile('test_name.bin'))
  837
+        self.assertEqual('--TEST_BOUNDARY', encoded_file[0])
  838
+        self.assertEqual('Content-Disposition: form-data; name="TEST_KEY"; filename="test_name.bin"', encoded_file[1])
  839
+        self.assertEqual('TEST_FILE_CONTENT', encoded_file[-1])
  840
+
  841
+    def test_guesses_content_type_on_file_encoding(self):
  842
+        self.assertEqual('Content-Type: application/octet-stream',
  843
+                         encode_file('IGNORE', 'IGNORE', DummyFile("file.bin"))[2])
  844
+        self.assertEqual('Content-Type: text/plain',
  845
+                         encode_file('IGNORE', 'IGNORE', DummyFile("file.txt"))[2])
  846
+        self.assertEqual('Content-Type: application/zip',
  847
+                         encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2])
  848
+        self.assertEqual('Content-Type: application/octet-stream',
  849
+                         encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2])

0 notes on commit 917f433

Please sign in to comment.
Something went wrong with that request. Please try again.