Debian Codegraph @

test/test_filepost.py

raw
1 from __future__ import annotations
2
3 import pytest
4
5 from urllib3.fields import RequestField
6 from urllib3.filepost import _TYPE_FIELDS, encode_multipart_formdata
7
8 BOUNDARY = "!! test boundary !!"
9 BOUNDARY_BYTES = BOUNDARY.encode()
10
11
12 class TestMultipartEncoding:
13 @pytest.mark.parametrize(
14 "fields", [dict(k="v", k2="v2"), [("k", "v"), ("k2", "v2")]]
15 )
16 def test_input_datastructures(self, fields: _TYPE_FIELDS) -> None:
17 encoded, _ = encode_multipart_formdata(fields, boundary=BOUNDARY)
18 assert encoded.count(BOUNDARY_BYTES) == 3
19
20 @pytest.mark.parametrize(
21 "fields",
22 [
23 [("k", "v"), ("k2", "v2")],
24 [("k", b"v"), ("k2", b"v2")],
25 [("k", b"v"), ("k2", "v2")],
26 ],
27 )
28 def test_field_encoding(self, fields: _TYPE_FIELDS) -> None:
29 encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
30 expected = (
31 b"--" + BOUNDARY_BYTES + b"\r\n"
32 b'Content-Disposition: form-data; name="k"\r\n'
33 b"\r\n"
34 b"v\r\n"
35 b"--" + BOUNDARY_BYTES + b"\r\n"
36 b'Content-Disposition: form-data; name="k2"\r\n'
37 b"\r\n"
38 b"v2\r\n"
39 b"--" + BOUNDARY_BYTES + b"--\r\n"
40 )
41
42 assert encoded == expected
43
44 assert content_type == "multipart/form-data; boundary=" + str(BOUNDARY)
45
46 def test_filename(self) -> None:
47 fields = [("k", ("somename", b"v"))]
48
49 encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
50 expected = (
51 b"--" + BOUNDARY_BYTES + b"\r\n"
52 b'Content-Disposition: form-data; name="k"; filename="somename"\r\n'
53 b"Content-Type: application/octet-stream\r\n"
54 b"\r\n"
55 b"v\r\n"
56 b"--" + BOUNDARY_BYTES + b"--\r\n"
57 )
58
59 assert encoded == expected
60
61 assert content_type == "multipart/form-data; boundary=" + str(BOUNDARY)
62
63 def test_textplain(self) -> None:
64 fields = [("k", ("somefile.txt", b"v"))]
65
66 encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
67 expected = (
68 b"--" + BOUNDARY_BYTES + b"\r\n"
69 b'Content-Disposition: form-data; name="k"; filename="somefile.txt"\r\n'
70 b"Content-Type: text/plain\r\n"
71 b"\r\n"
72 b"v\r\n"
73 b"--" + BOUNDARY_BYTES + b"--\r\n"
74 )
75
76 assert encoded == expected
77
78 assert content_type == "multipart/form-data; boundary=" + str(BOUNDARY)
79
80 def test_explicit(self) -> None:
81 fields = [("k", ("somefile.txt", b"v", "image/jpeg"))]
82
83 encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
84 expected = (
85 b"--" + BOUNDARY_BYTES + b"\r\n"
86 b'Content-Disposition: form-data; name="k"; filename="somefile.txt"\r\n'
87 b"Content-Type: image/jpeg\r\n"
88 b"\r\n"
89 b"v\r\n"
90 b"--" + BOUNDARY_BYTES + b"--\r\n"
91 )
92
93 assert encoded == expected
94
95 assert content_type == "multipart/form-data; boundary=" + str(BOUNDARY)
96
97 def test_request_fields(self) -> None:
98 fields = [
99 RequestField(
100 "k",
101 b"v",
102 filename="somefile.txt",
103 headers={"Content-Type": "image/jpeg"},
104 )
105 ]
106
107 encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
108 expected = (
109 b"--" + BOUNDARY_BYTES + b"\r\n"
110 b"Content-Type: image/jpeg\r\n"
111 b"\r\n"
112 b"v\r\n"
113 b"--" + BOUNDARY_BYTES + b"--\r\n"
114 )
115
116 assert encoded == expected