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

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.get_list("a") == ["123", "456"]1718    with pytest.warns(DeprecationWarning):19        assert h.getlist("a") == ["123", "456"]2021    assert list(h.keys()) == ["a", "b"]22    assert list(h.values()) == ["123, 456", "789"]23    assert list(h.items()) == [("a", "123, 456"), ("b", "789")]24    assert h.multi_items() == [("a", "123"), ("a", "456"), ("b", "789")]25    assert list(h) == ["a", "b"]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")]525354def 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():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    """161    `Headers.get_list` should support both split_commas=False and split_commas=True.162    """163    h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])164    assert h.get_list("Set-Cookie") == ["a, b", "c"]165166    h = httpx.Headers([("vary", "a, b"), ("vary", "c")])167    assert h.get_list("Vary", split_commas=True) == ["a", "b", "c"]168169170@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