Coverage for tests/test_urls.py: 100%
95 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
5def test_url():
6 url = httpx.URL('https://www.example.com/')
7 assert str(url) == "https://www.example.com/"
10def test_url_repr():
11 url = httpx.URL('https://www.example.com/')
12 assert repr(url) == "<URL 'https://www.example.com/'>"
15def test_url_params():
16 url = httpx.URL('https://www.example.com/', params={"a": "b", "c": "d"})
17 assert str(url) == "https://www.example.com/?a=b&c=d"
20def test_url_normalisation():
21 url = httpx.URL('https://www.EXAMPLE.com:443/path/../main')
22 assert str(url) == 'https://www.example.com/main'
25def test_url_relative():
26 url = httpx.URL('/README.md')
27 assert str(url) == '/README.md'
30def test_url_escaping():
31 url = httpx.URL('https://example.com/path to here?search=🦋')
32 assert str(url) == 'https://example.com/path%20to%20here?search=%F0%9F%A6%8B'
35def test_url_components():
36 url = httpx.URL(scheme="https", host="example.com", path="/")
37 assert str(url) == 'https://example.com/'
40# QueryParams
42def test_queryparams():
43 params = httpx.QueryParams({"color": "black", "size": "medium"})
44 assert str(params) == 'color=black&size=medium'
47def test_queryparams_repr():
48 params = httpx.QueryParams({"color": "black", "size": "medium"})
49 assert repr(params) == "<QueryParams 'color=black&size=medium'>"
52def test_queryparams_list_of_values():
53 params = httpx.QueryParams({"filter": ["60GHz", "75GHz", "100GHz"]})
54 assert str(params) == 'filter=60GHz&filter=75GHz&filter=100GHz'
57def test_queryparams_from_str():
58 params = httpx.QueryParams("color=black&size=medium")
59 assert str(params) == 'color=black&size=medium'
62def test_queryparams_access():
63 params = httpx.QueryParams("sort_by=published&author=natalie")
64 assert params["sort_by"] == 'published'
67def test_queryparams_escaping():
68 params = httpx.QueryParams({"email": "user@example.com", "search": "How HTTP works!"})
69 assert str(params) == 'email=user%40example.com&search=How+HTTP+works%21'
72def test_queryparams_empty():
73 q = httpx.QueryParams({"a": ""})
74 assert str(q) == "a="
76 q = httpx.QueryParams("a=")
77 assert str(q) == "a="
79 q = httpx.QueryParams("a")
80 assert str(q) == "a="
83def test_queryparams_set():
84 q = httpx.QueryParams("a=123")
85 q = q.copy_set("a", "456")
86 assert q == httpx.QueryParams("a=456")
89def test_queryparams_append():
90 q = httpx.QueryParams("a=123")
91 q = q.copy_append("a", "456")
92 assert q == httpx.QueryParams("a=123&a=456")
95def test_queryparams_remove():
96 q = httpx.QueryParams("a=123")
97 q = q.copy_remove("a")
98 assert q == httpx.QueryParams("")
101def test_queryparams_merge():
102 q = httpx.QueryParams("a=123")
103 q = q.copy_update({"b": "456"})
104 assert q == httpx.QueryParams("a=123&b=456")
105 q = q.copy_update({"a": "000", "c": "789"})
106 assert q == httpx.QueryParams("a=000&b=456&c=789")
109def test_queryparams_are_hashable():
110 params = (
111 httpx.QueryParams("a=123"),
112 httpx.QueryParams({"a": "123"}),
113 httpx.QueryParams("b=456"),
114 httpx.QueryParams({"b": "456"}),
115 )
117 assert len(set(params)) == 2
120@pytest.mark.parametrize(
121 "source",
122 [
123 "a=123&a=456&b=789",
124 {"a": ["123", "456"], "b": "789"},
125 {"a": ("123", "456"), "b": "789"},
126 [("a", "123"), ("a", "456"), ("b", "789")],
127 (("a", "123"), ("a", "456"), ("b", "789")),
128 ],
129)
130def test_queryparams_misc(source):
131 q = httpx.QueryParams(source)
132 assert "a" in q
133 assert "A" not in q
134 assert "c" not in q
135 assert q["a"] == "123"
136 assert q.get("a") == "123"
137 assert q.get("nope", default=None) is None
138 assert q.get_list("a") == ["123", "456"]
139 assert bool(q)
141 assert list(q.keys()) == ["a", "b"]
142 assert list(q.values()) == ["123", "789"]
143 assert list(q.items()) == [("a", "123"), ("b", "789")]
144 assert len(q) == 2
145 assert list(q) == ["a", "b"]
146 assert dict(q) == {"a": "123", "b": "789"}
147 assert str(q) == "a=123&a=456&b=789"
148 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(
149 [("a", "123"), ("b", "456")]
150 )
151 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(
152 "a=123&b=456"
153 )
154 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(
155 {"b": "456", "a": "123"}
156 )
157 assert httpx.QueryParams() == httpx.QueryParams({})
158 assert httpx.QueryParams([("a", "123"), ("a", "456")]) == httpx.QueryParams(
159 "a=123&a=456"
160 )
161 assert httpx.QueryParams({"a": "123", "b": "456"}) != "invalid"
163 q = httpx.QueryParams([("a", "123"), ("a", "456")])
164 assert httpx.QueryParams(q) == q