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
1import 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 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')])"23 assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])24 assert h != [("a", "123"), ("A", "456"), ("b", "789")]2526 h = httpx.Headers({"a": "123", "b": "789"})27 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():34 h = httpx.Headers()35 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():50 headers = httpx.Headers({"custom": "example"})51 headers_copy = httpx.Headers(headers)52 assert headers == headers_copy535455def test_headers_insert_retains_ordering():56 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})57 headers["b"] = "123"58 assert list(headers.values()) == ["a", "123", "c"]596061def test_headers_insert_appends_if_new():62 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})63 headers["d"] = "123"64 assert list(headers.values()) == ["a", "b", "c", "123"]656667def test_headers_insert_removes_all_existing():68 headers = httpx.Headers([("a", "123"), ("a", "456")])69 headers["a"] = "789"70 assert dict(headers) == {"a": "789"}717273def test_headers_delete_removes_all_existing():74 headers = httpx.Headers([("a", "123"), ("a", "456")])75 del headers["a"]76 assert dict(headers) == {}777879def test_headers_dict_repr():80 """81 Headers should display with a dict repr by default.82 """83 headers = httpx.Headers({"custom": "example"})84 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 """91 headers = httpx.Headers({b"custom": "example ☃".encode("utf-8")})92 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 """99 headers = httpx.Headers([("custom", "example 1"), ("custom", "example 2")])100 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")]110 headers = httpx.Headers(raw_headers)111 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"))]120 headers = httpx.Headers(raw_headers)121 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"))]130 headers = httpx.Headers(raw_headers)131 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"))]141 headers = httpx.Headers(raw_headers)142 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 """151 h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])152 h.getlist("Set-Cookie") == ["a, b", "b"]153154 h = httpx.Headers([("vary", "a, b"), ("vary", "c")])155 h.getlist("Vary") == ["a", "b", "c"]156157158@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