Skip to content
Myr

test_headers.py

This file

Path

tests/models/test_headers.py

177 lines · 5.4 KiB · Python

Last changed by

Use get_list consistently (#1119)Tom Christiebefore adoptione5f8743

History of this file

Take it

The walk stopped after 200 commits: lines older than c5037f0 point at it.

Drop per-request 'cert'/'verify'/'trust_env', and 'stream=bool' arguments (#730)Tom Christiebefore adoptionc5037f0

lines 1–15why, one step back
1import pytest
23import httpx456def test_headers():7 h = httpx.Headers([("a", "123"), ("a", "456"), ("b", "789")])8 assert "a" in h9 assert "A" in h10 assert "b" in h11 assert "B" in h12 assert "c" not in h13 assert h["a"] == "123, 456"14 assert h.get("a") == "123, 456"15 assert h.get("nope", default=None) is None

Consistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e

line 16why, one step back
16 assert h.get_list("a") == ["123", "456"]

Use get_list consistently (#1119)Tom Christiebefore adoptione5f8743

lines 17–20why, one step back
17
18 with pytest.warns(DeprecationWarning):19 assert h.getlist("a") == ["123", "456"]20

Make Headers.keys(), Headers.values() and Headers.items() return set-like views (#1114)Florimond Mancabefore adoptiona928421

lines 21–23why, one step back
21 assert list(h.keys()) == ["a", "b"]
22 assert list(h.values()) == ["123, 456", "789"]23 assert list(h.items()) == [("a", "123, 456"), ("b", "789")]

Consistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e

lines 24–25why, one step back
24 assert h.multi_items() == [("a", "123"), ("a", "456"), ("b", "789")]
25 assert list(h) == ["a", "b"]

Drop per-request 'cert'/'verify'/'trust_env', and 'stream=bool' arguments (#730)Tom Christiebefore adoptionc5037f0

lines 26–53why, one step back
26 assert dict(h) == {"a": "123, 456", "b": "789"}
27 assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"28 assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])29 assert h != [("a", "123"), ("A", "456"), ("b", "789")]3031 h = httpx.Headers({"a": "123", "b": "789"})32 assert h["A"] == "123"33 assert h["B"] == "789"34 assert h.raw == [(b"a", b"123"), (b"b", b"789")]35 assert repr(h) == "Headers({'a': '123', 'b': '789'})"363738def test_header_mutations():39 h = httpx.Headers()40 assert dict(h) == {}41 h["a"] = "1"42 assert dict(h) == {"a": "1"}43 h["a"] = "2"44 assert dict(h) == {"a": "2"}45 h.setdefault("a", "3")46 assert dict(h) == {"a": "2"}47 h.setdefault("b", "4")48 assert dict(h) == {"a": "2", "b": "4"}49 del h["a"]50 assert dict(h) == {"b": "4"}51 assert h.raw == [(b"b", b"4")]5253

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

lines 54–61why, one step back
54def test_copy_headers_method():
55 headers = httpx.Headers({"custom": "example"})56 headers_copy = headers.copy()57 assert headers == headers_copy58 assert headers is not headers_copy596061def test_copy_headers_init():

Drop per-request 'cert'/'verify'/'trust_env', and 'stream=bool' arguments (#730)Tom Christiebefore adoptionc5037f0

lines 62–160why, one step back
62 headers = httpx.Headers({"custom": "example"})
63 headers_copy = httpx.Headers(headers)64 assert headers == headers_copy656667def test_headers_insert_retains_ordering():68 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})69 headers["b"] = "123"70 assert list(headers.values()) == ["a", "123", "c"]717273def test_headers_insert_appends_if_new():74 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})75 headers["d"] = "123"76 assert list(headers.values()) == ["a", "b", "c", "123"]777879def test_headers_insert_removes_all_existing():80 headers = httpx.Headers([("a", "123"), ("a", "456")])81 headers["a"] = "789"82 assert dict(headers) == {"a": "789"}838485def test_headers_delete_removes_all_existing():86 headers = httpx.Headers([("a", "123"), ("a", "456")])87 del headers["a"]88 assert dict(headers) == {}899091def test_headers_dict_repr():92 """93 Headers should display with a dict repr by default.94 """95 headers = httpx.Headers({"custom": "example"})96 assert repr(headers) == "Headers({'custom': 'example'})"979899def test_headers_encoding_in_repr():100 """101 Headers should display an encoding in the repr if required.102 """103 headers = httpx.Headers({b"custom": "example ☃".encode("utf-8")})104 assert repr(headers) == "Headers({'custom': 'example ☃'}, encoding='utf-8')"105106107def test_headers_list_repr():108 """109 Headers should display with a list repr if they include multiple identical keys.110 """111 headers = httpx.Headers([("custom", "example 1"), ("custom", "example 2")])112 assert (113 repr(headers) == "Headers([('custom', 'example 1'), ('custom', 'example 2')])"114 )115116117def test_headers_decode_ascii():118 """119 Headers should decode as ascii by default.120 """121 raw_headers = [(b"Custom", b"Example")]122 headers = httpx.Headers(raw_headers)123 assert dict(headers) == {"custom": "Example"}124 assert headers.encoding == "ascii"125126127def test_headers_decode_utf_8():128 """129 Headers containing non-ascii codepoints should default to decoding as utf-8.130 """131 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]132 headers = httpx.Headers(raw_headers)133 assert dict(headers) == {"custom": "Code point: ☃"}134 assert headers.encoding == "utf-8"135136137def test_headers_decode_iso_8859_1():138 """139 Headers containing non-UTF-8 codepoints should default to decoding as iso-8859-1.140 """141 raw_headers = [(b"Custom", "Code point: ÿ".encode("iso-8859-1"))]142 headers = httpx.Headers(raw_headers)143 assert dict(headers) == {"custom": "Code point: ÿ"}144 assert headers.encoding == "iso-8859-1"145146147def test_headers_decode_explicit_encoding():148 """149 An explicit encoding may be set on headers in order to force a150 particular decoding.151 """152 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]153 headers = httpx.Headers(raw_headers)154 headers.encoding = "iso-8859-1"155 assert dict(headers) == {"custom": "Code point: â\x98\x83"}156 assert headers.encoding == "iso-8859-1"157158159def test_multiple_headers():160 """

Consistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e

line 161why, one step back
161 `Headers.get_list` should support both split_commas=False and split_commas=True.

Drop per-request 'cert'/'verify'/'trust_env', and 'stream=bool' arguments (#730)Tom Christiebefore adoptionc5037f0

lines 162–163why, one step back
162 """
163 h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])

Consistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e

line 164why, one step back
164 assert h.get_list("Set-Cookie") == ["a, b", "c"]

Drop per-request 'cert'/'verify'/'trust_env', and 'stream=bool' arguments (#730)Tom Christiebefore adoptionc5037f0

lines 165–166why, one step back
165
166 h = httpx.Headers([("vary", "a, b"), ("vary", "c")])

Consistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e

line 167why, one step back
167 assert h.get_list("Vary", split_commas=True) == ["a", "b", "c"]

Drop per-request 'cert'/'verify'/'trust_env', and 'stream=bool' arguments (#730)Tom Christiebefore adoptionc5037f0

lines 168–177why, one step back
168
169170@pytest.mark.parametrize("header", ["authorization", "proxy-authorization"])171def test_sensitive_headers(header):172 """173 Some headers should be obfuscated because they contain sensitive data.174 """175 value = "s3kr3t"176 h = httpx.Headers({header: value})177 assert repr(h) == "Headers({'%s': '[secure]'})" % header