Code

Ticket #11159: test_client_test.diff

File test_client_test.diff, 2.3 KB (added by lomin, 4 years ago)

Test of encode_file(). Documents current and desired behavior.

Line 
1diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
2index 6da7ae4..e51f7cc 100644
3--- a/tests/regressiontests/test_client_regress/models.py
4+++ b/tests/regressiontests/test_client_regress/models.py
5@@ -11,6 +11,7 @@ from django.core.urlresolvers import reverse
6 from django.core.exceptions import SuspiciousOperation
7 from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
8 from django.template import loader
9+from django.test.client import encode_file
10 
11 class AssertContainsTests(TestCase):
12     def setUp(self):
13@@ -821,3 +822,32 @@ class UnicodePayloadTests(TestCase):
14         response = self.client.post("/test_client_regress/parse_unicode_json/", json,
15                                     content_type="application/json; charset=koi8-r")
16         self.assertEqual(response.content, json.encode('koi8-r'))
17+
18+class TestClientTest(TestCase):
19+    class DummyFile(object):
20+        def __init__(self, filename):
21+            self.name = filename
22+        def read(self):
23+            return 'TEST_FILE_CONTENT'
24+           
25+    def setUp(self):
26+        self.encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', self.DummyFile('TEST_NAME.BIN'))
27+   
28+    def test_first_line_starts_with_dashes_and_boundary(self):
29+        self.assertEqual('--TEST_BOUNDARY', self.encoded_file[0])
30+       
31+    def test_adds_content_disposition_form_data_filename(self):
32+        self.assertEqual('Content-Disposition: form-data; name="TEST_KEY"; filename="TEST_NAME.BIN"', self.encoded_file[1])
33+   
34+    def test_guesses_content_type_on_file_encoding(self):
35+        def assert_content_type_for_file_extension(expected, file_extension):
36+            self.assertEqual('Content-Type: %s' % expected,
37+                             encode_file('IGNORE', 'IGNORE', self.DummyFile("FILE.%s" % file_extension))[2])
38+           
39+        assert_content_type_for_file_extension('application/octet-stream', 'BIN')
40+        assert_content_type_for_file_extension('text/plain', 'TXT')
41+        assert_content_type_for_file_extension('application/zip', 'ZIP')
42+        assert_content_type_for_file_extension('application/octet-stream', 'UNKNOWN')
43+       
44+    def tests_appends_file_content(self):
45+        self.assertEqual('TEST_FILE_CONTENT', self.encoded_file[-1])
46\ No newline at end of file