Ticket #10115: 10115. test_mimetypes.diff
File 10115. test_mimetypes.diff, 9.6 KB (added by , 16 years ago) |
---|
-
django/django/test/client.py
2 2 from urlparse import urlparse, urlunparse 3 3 import sys 4 4 import os 5 import mimetypes 5 6 try: 6 7 from cStringIO import StringIO 7 8 except ImportError: … … 83 84 store.setdefault('template',[]).append(template) 84 85 store.setdefault('context',[]).append(context) 85 86 86 def encode_multipart(boundary, data ):87 def encode_multipart(boundary, data, guess_mimetypes): 87 88 """ 88 89 Encodes multipart POST data from a dictionary of form values. 89 90 … … 88 89 Encodes multipart POST data from a dictionary of form values. 89 90 90 91 The key will be used as the form data name; the value will be transmitted 91 as content. If the value is a file, the contents of the file will be sent 92 as an application/octet-stream; otherwise, str(value) will be sent. 92 as content. If the value is a file, its mimetype will be guessed if 93 guess_mimetypes is True or will be set to application/octet-stream by 94 default; otherwise, str(value) will be sent. 93 95 """ 94 96 lines = [] 95 97 to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET) … … 102 104 # names can be duplicated! 103 105 for (key, value) in data.items(): 104 106 if is_file(value): 105 lines.extend(encode_file(boundary, key, value ))107 lines.extend(encode_file(boundary, key, value, guess_mimetypes)) 106 108 elif not isinstance(value, basestring) and is_iterable(value): 107 109 for item in value: 108 110 if is_file(item): 109 lines.extend(encode_file(boundary, key, item ))111 lines.extend(encode_file(boundary, key, item, guess_mimetypes)) 110 112 else: 111 113 lines.extend([ 112 114 '--' + boundary, … … 128 130 ]) 129 131 return '\r\n'.join(lines) 130 132 131 def encode_file(boundary, key, file ):133 def encode_file(boundary, key, file, guess_mimetype): 132 134 to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET) 135 filename = os.path.basename(file.name) 136 if guess_mimetype: 137 mimetype, encoding = mimetypes.guess_type(filename) 138 if mimetype is None: 139 mimetype = 'application/octet-stream' 140 else: 141 mimetype = 'application/octet-stream' 133 142 return [ 134 143 '--' + boundary, 135 144 'Content-Disposition: form-data; name="%s"; filename="%s"' \ 136 % (to_str(key), to_str( os.path.basename(file.name))),137 'Content-Type: application/octet-stream',145 % (to_str(key), to_str(filename)), 146 'Content-Type: %s' % mimetype, 138 147 '', 139 148 file.read() 140 149 ] … … 276 285 277 286 return self.request(**r) 278 287 279 def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):288 def post(self, path, data={}, content_type=MULTIPART_CONTENT, guess_mimetypes=False, **extra): 280 289 """ 281 290 Requests a response from the server using POST. 282 291 """ … … 281 290 Requests a response from the server using POST. 282 291 """ 283 292 if content_type is MULTIPART_CONTENT: 284 post_data = encode_multipart(BOUNDARY, data )293 post_data = encode_multipart(BOUNDARY, data, guess_mimetypes) 285 294 else: 286 295 post_data = data 287 296 … … 329 338 330 339 return self.request(**r) 331 340 332 def put(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):341 def put(self, path, data={}, content_type=MULTIPART_CONTENT, guess_mimetypes=False, **extra): 333 342 """ 334 343 Send a resource to the server using PUT. 335 344 """ … … 334 343 Send a resource to the server using PUT. 335 344 """ 336 345 if content_type is MULTIPART_CONTENT: 337 post_data = encode_multipart(BOUNDARY, data )346 post_data = encode_multipart(BOUNDARY, data, guess_mimetypes) 338 347 else: 339 348 post_data = data 340 349 -
django/docs/topics/testing.txt
505 505 If you provide URL both an encoded GET data and a data argument, 506 506 the data argument will take precedence. 507 507 508 .. method:: Client.post(path, data={}, content_type=MULTIPART_CONTENT )508 .. method:: Client.post(path, data={}, content_type=MULTIPART_CONTENT, guess_mimetypes=False) 509 509 510 510 Makes a POST request on the provided ``path`` and returns a 511 511 ``Response`` object, which is documented below. … … 553 553 (The name ``attachment`` here is not relevant; use whatever name your 554 554 file-processing code expects.) 555 555 556 Note that you should manually close the file after it has been provided 556 Note that by default all files will be sent as application/octet-stream. 557 If you want to let python guess the files' MIME types, simply set 558 the ``guess_mimetypes`` parameter as True. 559 560 Also note that you should manually close the file after it has been provided 557 561 to ``post()``. 558 562 559 563 .. versionadded:: development … … 583 587 Makes an OPTIONS request on the provided ``path`` and returns a 584 588 ``Response`` object. Useful for testing RESTful interfaces. 585 589 586 .. method:: Client.put(path, data={}, content_type=MULTIPART_CONTENT )590 .. method:: Client.put(path, data={}, content_type=MULTIPART_CONTENT, guess_mimetypes=False) 587 591 588 592 .. versionadded:: development 589 593 -
django/tests/regressiontests/test_client_regress/files/test2.txt
Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Property changes on: E:\Software\workspace\django\tests\regressiontests\test_client_regress\files\test1.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream
1 This is a text, doh. 2 No newline at end of file -
django/tests/regressiontests/test_client_regress/models.py
472 472 self.assertEqual(response.context['post-bar'], 'bang') 473 473 self.assertEqual(response.context['request-foo'], 'whiz') 474 474 self.assertEqual(response.context['request-bar'], 'bang') 475 476 class MimetypesTests(TestCase): 477 urls = 'regressiontests.test_client_regress.urls' 478 479 def test_mimetypes(self): 480 f1 = open(os.path.join(os.path.dirname(__file__), 'files/test1.png'), 'rb') 481 f2 = open(os.path.join(os.path.dirname(__file__), 'files/test2.txt'), 'rb') 482 post_data = { 483 'file_field1': f1, 484 'file_field2': f2, 485 } 486 # Guessed mimetypes 487 response = self.client.post('/mimetypes/', post_data, guess_mimetypes=True) 488 self.assertContains(response, 'test1.png:image/png', 1, 200) 489 self.assertContains(response, 'test2.txt:text/plain', 1, 200) 490 491 # Default mimetypes 492 response = self.client.post('/mimetypes/', post_data, guess_mimetypes=False) 493 self.assertContains(response, 'test1.png:application/octet-stream', 1, 200) 494 self.assertContains(response, 'test2.txt:application/octet-stream', 1, 200) 495 496 # Without 'guess_mimetypes' (default mimetypes) 497 response = self.client.post('/mimetypes/', post_data) 498 self.assertContains(response, 'test1.png:application/octet-stream', 1, 200) 499 self.assertContains(response, 'test2.txt:application/octet-stream', 1, 200) 500 501 f1.close() 502 f2.close() 503 No newline at end of file -
django/tests/regressiontests/test_client_regress/urls.py
11 11 (r'^set_session/$', views.set_session_view), 12 12 (r'^check_session/$', views.check_session_view), 13 13 (r'^request_methods/$', views.request_methods_view), 14 (r'^mimetypes/$', views.mimetypes_view), 14 15 ) -
django/tests/regressiontests/test_client_regress/views.py
58 58 59 59 def request_methods_view(request): 60 60 "A view that responds with the request method" 61 return HttpResponse('request method: %s' % request.method) 62 No newline at end of file 61 return HttpResponse('request method: %s' % request.method) 62 63 def mimetypes_view(request): 64 "A view that checks uploaded files' mimtypes" 65 content = ','.join(['%s:%s' % (v.name, v.content_type) for k, v in request.FILES.items()]) 66 return HttpResponse(str(content)) 67 No newline at end of file