test_headers.py
This file
Path
tests/models/test_headers.py
165 lines · 5.0 KiB · Python
Last changed by
Add debug logs to ASGIDispatch (#371)Florimond Mancabefore adoptione62e5c3
History of this fileTake it
The walk stopped after 200 commits: lines older than 444c6b8 point at it.
Add debug logs to ASGIDispatch (#371)Florimond Mancabefore adoptione62e5c3
lines 1–2why, one step back1import pytest2Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 3why, one step back3import httpxHonour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 4–6why, one step back456def test_headers():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 7why, one step back7 h = httpx.Headers([("a", "123"), ("a", "456"), ("b", "789")])Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 8–22why, one step back8 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 None16 assert h.getlist("a") == ["123", "456"]17 assert h.keys() == ["a", "a", "b"]18 assert h.values() == ["123", "456", "789"]19 assert h.items() == [("a", "123"), ("a", "456"), ("b", "789")]20 assert list(h) == ["a", "a", "b"]21 assert dict(h) == {"a": "123, 456", "b": "789"}22 assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 23why, one step back23 assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 24–25why, one step back24 assert h != [("a", "123"), ("A", "456"), ("b", "789")]25Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 26why, one step back26 h = httpx.Headers({"a": "123", "b": "789"})Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 27–33why, one step back27 assert h["A"] == "123"28 assert h["B"] == "789"29 assert h.raw == [(b"a", b"123"), (b"b", b"789")]30 assert repr(h) == "Headers({'a': '123', 'b': '789'})"313233def test_header_mutations():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 34why, one step back34 h = httpx.Headers()Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 35–49why, one step back35 assert dict(h) == {}36 h["a"] = "1"37 assert dict(h) == {"a": "1"}38 h["a"] = "2"39 assert dict(h) == {"a": "2"}40 h.setdefault("a", "3")41 assert dict(h) == {"a": "2"}42 h.setdefault("b", "4")43 assert dict(h) == {"a": "2", "b": "4"}44 del h["a"]45 assert dict(h) == {"b": "4"}46 assert h.raw == [(b"b", b"4")]474849def test_copy_headers():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 50–51why, one step back50 headers = httpx.Headers({"custom": "example"})51 headers_copy = httpx.Headers(headers)Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 52–55why, one step back52 assert headers == headers_copy535455def test_headers_insert_retains_ordering():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 56why, one step back56 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 57–61why, one step back57 headers["b"] = "123"58 assert list(headers.values()) == ["a", "123", "c"]596061def test_headers_insert_appends_if_new():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 62why, one step back62 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 63–67why, one step back63 headers["d"] = "123"64 assert list(headers.values()) == ["a", "b", "c", "123"]656667def test_headers_insert_removes_all_existing():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 68why, one step back68 headers = httpx.Headers([("a", "123"), ("a", "456")])Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 69–73why, one step back69 headers["a"] = "789"70 assert dict(headers) == {"a": "789"}717273def test_headers_delete_removes_all_existing():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 74why, one step back74 headers = httpx.Headers([("a", "123"), ("a", "456")])Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 75–82why, one step back75 del headers["a"]76 assert dict(headers) == {}777879def test_headers_dict_repr():80 """81 Headers should display with a dict repr by default.82 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 83why, one step back83 headers = httpx.Headers({"custom": "example"})Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 84–90why, one step back84 assert repr(headers) == "Headers({'custom': 'example'})"858687def test_headers_encoding_in_repr():88 """89 Headers should display an encoding in the repr if required.90 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 91why, one step back91 headers = httpx.Headers({b"custom": "example ☃".encode("utf-8")})Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 92–98why, one step back92 assert repr(headers) == "Headers({'custom': 'example ☃'}, encoding='utf-8')"939495def test_headers_list_repr():96 """97 Headers should display with a list repr if they include multiple identical keys.98 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 99why, one step back99 headers = httpx.Headers([("custom", "example 1"), ("custom", "example 2")])Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 100–109why, one step back100 assert (101 repr(headers) == "Headers([('custom', 'example 1'), ('custom', 'example 2')])"102 )103104105def test_headers_decode_ascii():106 """107 Headers should decode as ascii by default.108 """109 raw_headers = [(b"Custom", b"Example")]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 110why, one step back110 headers = httpx.Headers(raw_headers)Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 111–119why, one step back111 assert dict(headers) == {"custom": "Example"}112 assert headers.encoding == "ascii"113114115def test_headers_decode_utf_8():116 """117 Headers containing non-ascii codepoints should default to decoding as utf-8.118 """119 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 120why, one step back120 headers = httpx.Headers(raw_headers)Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 121–129why, one step back121 assert dict(headers) == {"custom": "Code point: ☃"}122 assert headers.encoding == "utf-8"123124125def test_headers_decode_iso_8859_1():126 """127 Headers containing non-UTF-8 codepoints should default to decoding as iso-8859-1.128 """129 raw_headers = [(b"Custom", "Code point: ÿ".encode("iso-8859-1"))]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 130why, one step back130 headers = httpx.Headers(raw_headers)Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 131–140why, one step back131 assert dict(headers) == {"custom": "Code point: ÿ"}132 assert headers.encoding == "iso-8859-1"133134135def test_headers_decode_explicit_encoding():136 """137 An explicit encoding may be set on headers in order to force a138 particular decoding.139 """140 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 141why, one step back141 headers = httpx.Headers(raw_headers)Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 142–150why, one step back142 headers.encoding = "iso-8859-1"143 assert dict(headers) == {"custom": "Code point: â\x98\x83"}144 assert headers.encoding == "iso-8859-1"145146147def test_multiple_headers():148 """149 Most headers should split by commas for `getlist`, except 'Set-Cookie'.150 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 151why, one step back151 h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
lines 152–153why, one step back152 h.getlist("Set-Cookie") == ["a, b", "b"]153Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 154why, one step back154 h = httpx.Headers([("vary", "a, b"), ("vary", "c")])Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8
line 155why, one step back155 h.getlist("Vary") == ["a", "b", "c"]Add debug logs to ASGIDispatch (#371)Florimond Mancabefore adoptione62e5c3
lines 156–165why, one step back156157158@pytest.mark.parametrize("header", ["authorization", "proxy-authorization"])159def test_sensitive_headers(header):160 """161 Some headers should be obfuscated because they contain sensitive data.162 """163 value = "s3kr3t"164 h = httpx.Headers({header: value})165 assert repr(h) == "Headers({'%s': '[secure]'})" % header