1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
import pytest
import httpx
@pytest.mark.parametrize(
"source",
[
"a=123&a=456&b=789",
{"a": ["123", "456"], "b": 789},
{"a": ("123", "456"), "b": 789},
[("a", "123"), ("a", "456"), ("b", "789")],
(("a", "123"), ("a", "456"), ("b", "789")),
],
)
def test_queryparams(source):
q = httpx.QueryParams(source)
assert "a" in q
assert "A" not in q
assert "c" not in q
assert q["a"] == "123"
assert q.get("a") == "123"
assert q.get("nope", default=None) is None
assert q.get_list("a") == ["123", "456"]
assert list(q.keys()) == ["a", "b"]
assert list(q.values()) == ["123", "789"]
assert list(q.items()) == [("a", "123"), ("b", "789")]
assert len(q) == 2
assert list(q) == ["a", "b"]
assert dict(q) == {"a": "123", "b": "789"}
assert str(q) == "a=123&a=456&b=789"
assert repr(q) == "QueryParams('a=123&a=456&b=789')"
assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(
[("a", "123"), ("b", "456")]
)
assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(
"a=123&b=456"
)
assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(
{"b": "456", "a": "123"}
)
assert httpx.QueryParams() == httpx.QueryParams({})
assert httpx.QueryParams([("a", "123"), ("a", "456")]) == httpx.QueryParams(
"a=123&a=456"
)
assert httpx.QueryParams({"a": "123", "b": "456"}) != "invalid"
q = httpx.QueryParams([("a", "123"), ("a", "456")])
assert httpx.QueryParams(q) == q
def test_queryparam_types():
q = httpx.QueryParams(None)
assert str(q) == ""
q = httpx.QueryParams({"a": True})
assert str(q) == "a=true"
q = httpx.QueryParams({"a": False})
assert str(q) == "a=false"
q = httpx.QueryParams({"a": ""})
assert str(q) == "a="
q = httpx.QueryParams({"a": None})
assert str(q) == "a="
q = httpx.QueryParams({"a": 1.23})
assert str(q) == "a=1.23"
q = httpx.QueryParams({"a": 123})
assert str(q) == "a=123"
q = httpx.QueryParams({"a": [1, 2]})
assert str(q) == "a=1&a=2"
def test_empty_query_params():
q = httpx.QueryParams({"a": ""})
assert str(q) == "a="
q = httpx.QueryParams("a=")
assert str(q) == "a="
q = httpx.QueryParams("a")
assert str(q) == "a="
def test_queryparam_update_is_hard_deprecated():
q = httpx.QueryParams("a=123")
with pytest.raises(RuntimeError):
q.update({"a": "456"})
def test_queryparam_setter_is_hard_deprecated():
q = httpx.QueryParams("a=123")
with pytest.raises(RuntimeError):
q["a"] = "456"
def test_queryparam_set():
q = httpx.QueryParams("a=123")
q = q.set("a", "456")
assert q == httpx.QueryParams("a=456")
def test_queryparam_add():
q = httpx.QueryParams("a=123")
q = q.add("a", "456")
assert q == httpx.QueryParams("a=123&a=456")
def test_queryparam_remove():
q = httpx.QueryParams("a=123")
q = q.remove("a")
assert q == httpx.QueryParams("")
def test_queryparam_merge():
q = httpx.QueryParams("a=123")
q = q.merge({"b": "456"})
assert q == httpx.QueryParams("a=123&b=456")
q = q.merge({"a": "000", "c": "789"})
assert q == httpx.QueryParams("a=000&b=456&c=789")
def test_queryparams_are_hashable():
params = (
httpx.QueryParams("a=123"),
httpx.QueryParams({"a": 123}),
httpx.QueryParams("b=456"),
httpx.QueryParams({"b": 456}),
)
assert len(set(params)) == 2
|