예제 #1
0
        def to_lines(d, prefix='', dot='.'):
            for (key, value) in d.items():
                if prefix:
                    key = '%s%s%s' % (prefix, dot, key)

                if value is None:
                    raise TypeError('Cannot encode None as POST data. '
                                    'Did you mean to pass an '
                                    'empty string or omit the value?')
                elif isinstance(value, dict):
                    to_lines(value, key)
                elif is_file(value):
                    lines.extend(encode_file(boundary, key, value))
                elif not isinstance(value, str) and is_iterable(value):
                    for index, item in enumerate(value):
                        if isinstance(item, dict):
                            to_lines(item, '%s[%s]' % (key, index), '')
                        elif is_file(item):
                            lines.extend(
                                encode_file(boundary, '%s[%s]' % (key, index),
                                            item))
                        else:
                            lines.extend(
                                to_bytes(val) for val in [
                                    '--%s' % boundary,
                                    ('Content-Disposition: form-data; '
                                     'name="%s[%s]"' % (key, index)), '', item
                                ])
                else:
                    lines.extend(
                        to_bytes(val) for val in [
                            '--%s' % boundary,
                            'Content-Disposition: form-data; name="%s"' %
                            key, '', value
                        ])
예제 #2
0
 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])
예제 #3
0
 def test_guesses_content_type_on_file_encoding(self):
     self.assertEqual(b'Content-Type: application/octet-stream',
                      encode_file('IGNORE', 'IGNORE', DummyFile("file.bin"))[2])
     self.assertEqual(b'Content-Type: text/plain',
                      encode_file('IGNORE', 'IGNORE', DummyFile("file.txt"))[2])
     self.assertIn(encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2], (
                     b'Content-Type: application/x-compress',
                     b'Content-Type: application/x-zip',
                     b'Content-Type: application/x-zip-compressed',
                     b'Content-Type: application/zip',))
     self.assertEqual(b'Content-Type: application/octet-stream',
                      encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2])
예제 #4
0
 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.assertIn(
         encode_file("IGNORE", "IGNORE", DummyFile("file.zip"))[2],
         (
             "Content-Type: application/x-compress",
             "Content-Type: application/x-zip",
             "Content-Type: application/x-zip-compressed",
             "Content-Type: application/zip",
         ),
     )
     self.assertEqual(
         "Content-Type: application/octet-stream", encode_file("IGNORE", "IGNORE", DummyFile("file.unknown"))[2]
     )
예제 #5
0
 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])
예제 #6
0
 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])
예제 #7
0
파일: tests.py 프로젝트: rafacv/django
 def test_file_encoding(self):
     encoded_file = encode_file("TEST_BOUNDARY", "TEST_KEY", DummyFile("test_name.bin"))
     self.assertEqual(b"--TEST_BOUNDARY", encoded_file[0])
     self.assertEqual(b'Content-Disposition: form-data; name="TEST_KEY"; filename="test_name.bin"', encoded_file[1])
     self.assertEqual(b"TEST_FILE_CONTENT", encoded_file[-1])