test_headers.py
This file
Path
tests/models/test_headers.py
173 lines · 5.3 KiB · Python
Last changed by
Consistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e
History of this fileTake it
The walk stopped after 200 commits: lines older than d5da743 point at it.
Rename 'close' to 'aclose' on Client (#675)Florimond Mancabefore adoptiond5da743
lines 1–15why, one step back1import pytest23import 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 NoneConsistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e
lines 16–21why, one step back16 assert h.get_list("a") == ["123", "456"]17 assert h.keys() == ["a", "b"]18 assert h.values() == ["123, 456", "789"]19 assert h.items() == [("a", "123, 456"), ("b", "789")]20 assert h.multi_items() == [("a", "123"), ("a", "456"), ("b", "789")]21 assert list(h) == ["a", "b"]Rename 'close' to 'aclose' on Client (#675)Florimond Mancabefore adoptiond5da743
lines 22–49why, one step back22 assert dict(h) == {"a": "123, 456", "b": "789"}23 assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"24 assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])25 assert h != [("a", "123"), ("A", "456"), ("b", "789")]2627 h = httpx.Headers({"a": "123", "b": "789"})28 assert h["A"] == "123"29 assert h["B"] == "789"30 assert h.raw == [(b"a", b"123"), (b"b", b"789")]31 assert repr(h) == "Headers({'a': '123', 'b': '789'})"323334def test_header_mutations():35 h = httpx.Headers()36 assert dict(h) == {}37 h["a"] = "1"38 assert dict(h) == {"a": "1"}39 h["a"] = "2"40 assert dict(h) == {"a": "2"}41 h.setdefault("a", "3")42 assert dict(h) == {"a": "2"}43 h.setdefault("b", "4")44 assert dict(h) == {"a": "2", "b": "4"}45 del h["a"]46 assert dict(h) == {"b": "4"}47 assert h.raw == [(b"b", b"4")]4849Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
lines 50–57why, one step back50def test_copy_headers_method():51 headers = httpx.Headers({"custom": "example"})52 headers_copy = headers.copy()53 assert headers == headers_copy54 assert headers is not headers_copy555657def test_copy_headers_init():Rename 'close' to 'aclose' on Client (#675)Florimond Mancabefore adoptiond5da743
lines 58–156why, one step back58 headers = httpx.Headers({"custom": "example"})59 headers_copy = httpx.Headers(headers)60 assert headers == headers_copy616263def test_headers_insert_retains_ordering():64 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})65 headers["b"] = "123"66 assert list(headers.values()) == ["a", "123", "c"]676869def test_headers_insert_appends_if_new():70 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})71 headers["d"] = "123"72 assert list(headers.values()) == ["a", "b", "c", "123"]737475def test_headers_insert_removes_all_existing():76 headers = httpx.Headers([("a", "123"), ("a", "456")])77 headers["a"] = "789"78 assert dict(headers) == {"a": "789"}798081def test_headers_delete_removes_all_existing():82 headers = httpx.Headers([("a", "123"), ("a", "456")])83 del headers["a"]84 assert dict(headers) == {}858687def test_headers_dict_repr():88 """89 Headers should display with a dict repr by default.90 """91 headers = httpx.Headers({"custom": "example"})92 assert repr(headers) == "Headers({'custom': 'example'})"939495def test_headers_encoding_in_repr():96 """97 Headers should display an encoding in the repr if required.98 """99 headers = httpx.Headers({b"custom": "example ☃".encode("utf-8")})100 assert repr(headers) == "Headers({'custom': 'example ☃'}, encoding='utf-8')"101102103def test_headers_list_repr():104 """105 Headers should display with a list repr if they include multiple identical keys.106 """107 headers = httpx.Headers([("custom", "example 1"), ("custom", "example 2")])108 assert (109 repr(headers) == "Headers([('custom', 'example 1'), ('custom', 'example 2')])"110 )111112113def test_headers_decode_ascii():114 """115 Headers should decode as ascii by default.116 """117 raw_headers = [(b"Custom", b"Example")]118 headers = httpx.Headers(raw_headers)119 assert dict(headers) == {"custom": "Example"}120 assert headers.encoding == "ascii"121122123def test_headers_decode_utf_8():124 """125 Headers containing non-ascii codepoints should default to decoding as utf-8.126 """127 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]128 headers = httpx.Headers(raw_headers)129 assert dict(headers) == {"custom": "Code point: ☃"}130 assert headers.encoding == "utf-8"131132133def test_headers_decode_iso_8859_1():134 """135 Headers containing non-UTF-8 codepoints should default to decoding as iso-8859-1.136 """137 raw_headers = [(b"Custom", "Code point: ÿ".encode("iso-8859-1"))]138 headers = httpx.Headers(raw_headers)139 assert dict(headers) == {"custom": "Code point: ÿ"}140 assert headers.encoding == "iso-8859-1"141142143def test_headers_decode_explicit_encoding():144 """145 An explicit encoding may be set on headers in order to force a146 particular decoding.147 """148 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]149 headers = httpx.Headers(raw_headers)150 headers.encoding = "iso-8859-1"151 assert dict(headers) == {"custom": "Code point: â\x98\x83"}152 assert headers.encoding == "iso-8859-1"153154155def test_multiple_headers():156 """Consistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e
line 157why, one step back157 `Headers.get_list` should support both split_commas=False and split_commas=True.Rename 'close' to 'aclose' on Client (#675)Florimond Mancabefore adoptiond5da743
lines 158–159why, one step back158 """159 h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])Consistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e
line 160why, one step back160 assert h.get_list("Set-Cookie") == ["a, b", "c"]Rename 'close' to 'aclose' on Client (#675)Florimond Mancabefore adoptiond5da743
lines 161–162why, one step back161162 h = httpx.Headers([("vary", "a, b"), ("vary", "c")])Consistent multidict methods (#1089)Tom Christiebefore adoption2ba9c1e
line 163why, one step back163 assert h.get_list("Vary", split_commas=True) == ["a", "b", "c"]Rename 'close' to 'aclose' on Client (#675)Florimond Mancabefore adoptiond5da743
lines 164–173why, one step back164165166@pytest.mark.parametrize("header", ["authorization", "proxy-authorization"])167def test_sensitive_headers(header):168 """169 Some headers should be obfuscated because they contain sensitive data.170 """171 value = "s3kr3t"172 h = httpx.Headers({header: value})173 assert repr(h) == "Headers({'%s': '[secure]'})" % header