test_queryparams.py
This file
Path
tests/models/test_queryparams.py
94 lines · 2.5 KiB · Python
Last changed by
Allow tuple as input of query parameters. (#1426)SarunasAznabefore adoption8696405
History of this fileTake it
1import pytest23import httpx456@pytest.mark.parametrize(7 "source",8 [9 "a=123&a=456&b=789",10 {"a": ["123", "456"], "b": 789},11 {"a": ("123", "456"), "b": 789},12 [("a", "123"), ("a", "456"), ("b", "789")],13 (("a", "123"), ("a", "456"), ("b", "789")),14 ],15)16def test_queryparams(source):17 q = httpx.QueryParams(source)18 assert "a" in q19 assert "A" not in q20 assert "c" not in q21 assert q["a"] == "456"22 assert q.get("a") == "456"23 assert q.get("nope", default=None) is None24 assert q.get_list("a") == ["123", "456"]2526 assert list(q.keys()) == ["a", "b"]27 assert list(q.values()) == ["456", "789"]28 assert list(q.items()) == [("a", "456"), ("b", "789")]29 assert len(q) == 230 assert list(q) == ["a", "b"]31 assert dict(q) == {"a": "456", "b": "789"}32 assert str(q) == "a=123&a=456&b=789"33 assert repr(q) == "QueryParams('a=123&a=456&b=789')"34 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(35 [("a", "123"), ("b", "456")]36 )37 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(38 "a=123&b=456"39 )40 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(41 {"b": "456", "a": "123"}42 )43 assert httpx.QueryParams() == httpx.QueryParams({})44 assert httpx.QueryParams([("a", "123"), ("a", "456")]) == httpx.QueryParams(45 "a=123&a=456"46 )47 assert httpx.QueryParams({"a": "123", "b": "456"}) != "invalid"4849 q = httpx.QueryParams([("a", "123"), ("a", "456")])50 assert httpx.QueryParams(q) == q515253def test_queryparam_types():54 q = httpx.QueryParams(None)55 assert str(q) == ""5657 q = httpx.QueryParams({"a": True})58 assert str(q) == "a=true"5960 q = httpx.QueryParams({"a": False})61 assert str(q) == "a=false"6263 q = httpx.QueryParams({"a": ""})64 assert str(q) == "a="6566 q = httpx.QueryParams({"a": None})67 assert str(q) == "a="6869 q = httpx.QueryParams({"a": 1.23})70 assert str(q) == "a=1.23"7172 q = httpx.QueryParams({"a": 123})73 assert str(q) == "a=123"7475 q = httpx.QueryParams({"a": [1, 2]})76 assert str(q) == "a=1&a=2"777879def test_queryparam_setters():80 q = httpx.QueryParams({"a": 1})81 q.update([])8283 assert str(q) == "a=1"8485 q = httpx.QueryParams([("a", 1), ("a", 2)])86 q["a"] = "3"87 assert str(q) == "a=3"8889 q = httpx.QueryParams([("a", 1), ("b", 1)])90 u = httpx.QueryParams([("b", 2), ("b", 3)])91 q.update(u)9293 assert str(q) == "a=1&b=2&b=3"94 assert q["b"] == u["b"]