Coverage for tests/test_content.py: 100%
78 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-26 22:55 +0100
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-26 22:55 +0100
1import httpx
2import pytest
5# HTML
7def test_html():
8 html = httpx.HTML("<html><body>Hello, world</body></html>")
10 stream, content_type = html.encode()
11 assert stream == httpx.ByteStream(b'<html><body>Hello, world</body></html>')
12 assert content_type == "text/html; charset='utf-8'"
15# Text
17def test_text():
18 text = httpx.Text("Hello, world")
20 stream, content_type = text.encode()
21 assert stream == httpx.ByteStream(b'Hello, world')
22 assert content_type == "text/plain; charset='utf-8'"
25# JSON
27def test_json():
28 data = httpx.JSON({'data': 123})
30 stream, content_type = data.encode()
31 assert stream == httpx.ByteStream(b'{"data":123}')
32 assert content_type == "application/json"
35# Form
37def test_form():
38 f = httpx.Form([("a", "123"), ("a", "456"), ("b", "789")])
40 # Accessors
41 assert "a" in f
42 assert "A" not in f
43 assert "c" not in f
44 assert f["a"] == "123"
45 assert f.get("a") == "123"
46 assert f.get("nope", default=None) is None
48 # Standard dict operations
49 assert list(f.keys()) == ["a", "b"]
50 assert list(f.values()) == ["123", "789"]
51 assert list(f.items()) == [("a", "123"), ("b", "789")]
52 assert list(f) == ["a", "b"]
53 assert dict(f) == {"a": "123", "b": "789"}
55 assert len(f) == 2
56 assert bool(f)
57 assert hash(f)
59 # Comparison
60 assert f == httpx.Form([("a", "123"), ("a", "456"), ("b", "789")])
62 # Multi-dict interface
63 assert f.get_list("a") == ["123", "456"]
64 assert list(f.multi_items()) == [("a", "123"), ("a", "456"), ("b", "789")]
65 assert f.multi_dict() == {"a": ["123", "456"], "b": ["789"]}
67 # Update
68 assert f.copy_set("a", "abc") == httpx.Form([("a", "abc"), ("b", "789")])
69 assert f.copy_append("a", "abc") == httpx.Form([("a", "123"), ("a", "456"), ("a", "abc"), ("b", "789")])
70 assert f.copy_remove("a") == httpx.Form([("b", "789")])
72 assert str(f) == "a=123&a=456&b=789"
75def test_empty_form():
76 f = httpx.Form()
77 assert f.multi_dict() == {}
80def test_form_from_dict():
81 f = httpx.Form({
82 "a": ["123", "456"],
83 "b": "789"
84 })
85 assert f.multi_dict() == {
86 "a": ["123", "456"],
87 "b": ["789"]
88 }
91def test_form_encode():
92 form = httpx.Form({'email': 'address@example.com'})
93 assert form['email'] == "address@example.com"
95 stream, content_type = form.encode()
96 assert stream == httpx.ByteStream(b"email=address%40example.com")
97 assert content_type == "application/x-www-form-urlencoded"
100# Files
102def test_files():
103 f = httpx.Files([
104 ("a", httpx.File("123.json")),
105 ("a", httpx.File("456.json")),
106 ("b", httpx.File("789.json"))
107 ])
109 # Accessors
110 assert "a" in f
111 assert "A" not in f
112 assert "c" not in f
113 assert f["a"] == httpx.File("123.json")
114 assert f.get("a") == httpx.File("123.json")
115 assert f.get("nope", default=None) is None
117 # Standard dict operations
118 assert list(f.keys()) == ["a", "b"]
119 assert list(f.values()) == [httpx.File("123.json"), httpx.File("789.json")]
120 assert list(f.items()) == [("a", httpx.File("123.json")), ("b", httpx.File("789.json"))]
121 assert list(f) == ["a", "b"]
122 assert dict(f) == {"a": httpx.File("123.json"), "b": httpx.File("789.json")}
124 assert len(f) == 2
125 assert bool(f)
127 # Comparison
128 assert f == httpx.Files([
129 ("a", httpx.File("123.json")),
130 ("a", httpx.File("456.json")),
131 ("b", httpx.File("789.json")),
132 ])
134 # Multi-dict interface
135 assert f.get_list("a") == [
136 httpx.File("123.json"),
137 httpx.File("456.json"),
138 ]
139 assert list(f.multi_items()) == [
140 ("a", httpx.File("123.json")),
141 ("a", httpx.File("456.json")),
142 ("b", httpx.File("789.json")),
143 ]
144 assert f.multi_dict() == {
145 "a": [
146 httpx.File("123.json"),
147 httpx.File("456.json"),
148 ],
149 "b": [
150 httpx.File("789.json"),
151 ]
152 }
155def test_empty_files():
156 f = httpx.Files()
157 assert f.multi_dict() == {}
160def test_files_from_dict():
161 f = httpx.Files({
162 "a": [
163 httpx.File("123.json"),
164 httpx.File("456.json"),
165 ],
166 "b": httpx.File("789.json")
167 })
168 assert f.multi_dict() == {
169 "a": [
170 httpx.File("123.json"),
171 httpx.File("456.json"),
172 ],
173 "b": [
174 httpx.File("789.json"),
175 ]
176 }
180# def test_multipart(tmpfile):
181# multipart = httpx.MultiPart(form={'name': '...'}, files={'upload': httpx.File(tmpfile)})
182# assert multipart.form['name'] == "..."
183# assert multipart.files['upload'].name == 'upload.txt'