Ticket #11159: test_client_test.diff

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

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

  • tests/regressiontests/test_client_regress/models.py

    diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
    index 6da7ae4..e51f7cc 100644
    a b from django.core.urlresolvers import reverse 
    1111from django.core.exceptions import SuspiciousOperation
    1212from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
    1313from django.template import loader
     14from django.test.client import encode_file
    1415
    1516class AssertContainsTests(TestCase):
    1617    def setUp(self):
    class UnicodePayloadTests(TestCase): 
    821822        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
    822823                                    content_type="application/json; charset=koi8-r")
    823824        self.assertEqual(response.content, json.encode('koi8-r'))
     825
     826class TestClientTest(TestCase):
     827    class DummyFile(object):
     828        def __init__(self, filename):
     829            self.name = filename
     830        def read(self):
     831            return 'TEST_FILE_CONTENT'
     832           
     833    def setUp(self):
     834        self.encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', self.DummyFile('TEST_NAME.BIN'))
     835   
     836    def test_first_line_starts_with_dashes_and_boundary(self):
     837        self.assertEqual('--TEST_BOUNDARY', self.encoded_file[0])
     838       
     839    def test_adds_content_disposition_form_data_filename(self):
     840        self.assertEqual('Content-Disposition: form-data; name="TEST_KEY"; filename="TEST_NAME.BIN"', self.encoded_file[1])
     841   
     842    def test_guesses_content_type_on_file_encoding(self):
     843        def assert_content_type_for_file_extension(expected, file_extension):
     844            self.assertEqual('Content-Type: %s' % expected,
     845                             encode_file('IGNORE', 'IGNORE', self.DummyFile("FILE.%s" % file_extension))[2])
     846           
     847        assert_content_type_for_file_extension('application/octet-stream', 'BIN')
     848        assert_content_type_for_file_extension('text/plain', 'TXT')
     849        assert_content_type_for_file_extension('application/zip', 'ZIP')
     850        assert_content_type_for_file_extension('application/octet-stream', 'UNKNOWN')
     851       
     852    def tests_appends_file_content(self):
     853        self.assertEqual('TEST_FILE_CONTENT', self.encoded_file[-1])
     854 No newline at end of file
Back to Top