Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
341 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import unittest | ||
|
||
from urllib3.filepost import encode_multipart_formdata, iter_fields | ||
from urllib3.packages.six import b, u | ||
|
||
|
||
BOUNDARY = '!! test boundary !!' | ||
|
||
|
||
class TestIterfields(unittest.TestCase): | ||
|
||
def test_dict(self): | ||
for fieldname, value in iter_fields(dict(a='b')): | ||
self.assertEqual((fieldname, value), ('a', 'b')) | ||
|
||
self.assertEqual( | ||
list(sorted(iter_fields(dict(a='b', c='d')))), | ||
[('a', 'b'), ('c', 'd')]) | ||
|
||
def test_tuple_list(self): | ||
for fieldname, value in iter_fields([('a', 'b')]): | ||
self.assertEqual((fieldname, value), ('a', 'b')) | ||
|
||
self.assertEqual( | ||
list(iter_fields([('a', 'b'), ('c', 'd')])), | ||
[('a', 'b'), ('c', 'd')]) | ||
|
||
|
||
class TestMultipartEncoding(unittest.TestCase): | ||
|
||
def test_input_datastructures(self): | ||
fieldsets = [ | ||
dict(k='v', k2='v2'), | ||
[('k', 'v'), ('k2', 'v2')], | ||
] | ||
|
||
for fields in fieldsets: | ||
encoded, _ = encode_multipart_formdata(fields, boundary=BOUNDARY) | ||
self.assertEqual(encoded.count(b(BOUNDARY)), 3) | ||
|
||
|
||
def test_field_encoding(self): | ||
fieldsets = [ | ||
[('k', 'v'), ('k2', 'v2')], | ||
[('k', b'v'), (u('k2'), b'v2')], | ||
[('k', b'v'), (u('k2'), 'v2')], | ||
] | ||
|
||
for fields in fieldsets: | ||
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY) | ||
|
||
self.assertEqual(encoded, | ||
b'--' + b(BOUNDARY) + b'\r\n' | ||
b'Content-Disposition: form-data; name="k"\r\n' | ||
b'Content-Type: text/plain\r\n' | ||
b'\r\n' | ||
b'v\r\n' | ||
b'--' + b(BOUNDARY) + b'\r\n' | ||
b'Content-Disposition: form-data; name="k2"\r\n' | ||
b'Content-Type: text/plain\r\n' | ||
b'\r\n' | ||
b'v2\r\n' | ||
b'--' + b(BOUNDARY) + b'--\r\n' | ||
, fields) | ||
|
||
self.assertEqual(content_type, | ||
b'multipart/form-data; boundary=' + b(BOUNDARY)) | ||
|
||
|
||
def test_filename(self): | ||
fields = [('k', ('somename', b'v'))] | ||
|
||
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY) | ||
|
||
self.assertEqual(encoded, | ||
b'--' + b(BOUNDARY) + b'\r\n' | ||
b'Content-Disposition: form-data; name="k"; filename="somename"\r\n' | ||
b'Content-Type: application/octet-stream\r\n' | ||
b'\r\n' | ||
b'v\r\n' | ||
b'--' + b(BOUNDARY) + b'--\r\n' | ||
) | ||
|
||
self.assertEqual(content_type, | ||
b'multipart/form-data; boundary=' + b(BOUNDARY)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.