Skip to content
Myr

test_headers.py

This file

Path

tests/models/test_headers.py

174 lines · 5.3 KiB · Python

Last changed by

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

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    assert list(h.keys()) == ["a", "b"]19    assert list(h.values()) == ["123, 456", "789"]20    assert list(h.items()) == [("a", "123, 456"), ("b", "789")]21    assert h.multi_items() == [("a", "123"), ("a", "456"), ("b", "789")]22    assert list(h) == ["a", "b"]23    assert dict(h) == {"a": "123, 456", "b": "789"}24    assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"25    assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])26    assert h != [("a", "123"), ("A", "456"), ("b", "789")]2728    h = httpx.Headers({"a": "123", "b": "789"})29    assert h["A"] == "123"30    assert h["B"] == "789"31    assert h.raw == [(b"a", b"123"), (b"b", b"789")]32    assert repr(h) == "Headers({'a': '123', 'b': '789'})"333435def test_header_mutations():36    h = httpx.Headers()37    assert dict(h) == {}38    h["a"] = "1"39    assert dict(h) == {"a": "1"}40    h["a"] = "2"41    assert dict(h) == {"a": "2"}42    h.setdefault("a", "3")43    assert dict(h) == {"a": "2"}44    h.setdefault("b", "4")45    assert dict(h) == {"a": "2", "b": "4"}46    del h["a"]47    assert dict(h) == {"b": "4"}48    assert h.raw == [(b"b", b"4")]495051def test_copy_headers_method():52    headers = httpx.Headers({"custom": "example"})53    headers_copy = headers.copy()54    assert headers == headers_copy55    assert headers is not headers_copy565758def test_copy_headers_init():59    headers = httpx.Headers({"custom": "example"})60    headers_copy = httpx.Headers(headers)61    assert headers == headers_copy626364def test_headers_insert_retains_ordering():65    headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})66    headers["b"] = "123"67    assert list(headers.values()) == ["a", "123", "c"]686970def test_headers_insert_appends_if_new():71    headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})72    headers["d"] = "123"73    assert list(headers.values()) == ["a", "b", "c", "123"]747576def test_headers_insert_removes_all_existing():77    headers = httpx.Headers([("a", "123"), ("a", "456")])78    headers["a"] = "789"79    assert dict(headers) == {"a": "789"}808182def test_headers_delete_removes_all_existing():83    headers = httpx.Headers([("a", "123"), ("a", "456")])84    del headers["a"]85    assert dict(headers) == {}868788def test_headers_dict_repr():89    """90    Headers should display with a dict repr by default.91    """92    headers = httpx.Headers({"custom": "example"})93    assert repr(headers) == "Headers({'custom': 'example'})"949596def test_headers_encoding_in_repr():97    """98    Headers should display an encoding in the repr if required.99    """100    headers = httpx.Headers({b"custom": "example ☃".encode("utf-8")})101    assert repr(headers) == "Headers({'custom': 'example ☃'}, encoding='utf-8')"102103104def test_headers_list_repr():105    """106    Headers should display with a list repr if they include multiple identical keys.107    """108    headers = httpx.Headers([("custom", "example 1"), ("custom", "example 2")])109    assert (110        repr(headers) == "Headers([('custom', 'example 1'), ('custom', 'example 2')])"111    )112113114def test_headers_decode_ascii():115    """116    Headers should decode as ascii by default.117    """118    raw_headers = [(b"Custom", b"Example")]119    headers = httpx.Headers(raw_headers)120    assert dict(headers) == {"custom": "Example"}121    assert headers.encoding == "ascii"122123124def test_headers_decode_utf_8():125    """126    Headers containing non-ascii codepoints should default to decoding as utf-8.127    """128    raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]129    headers = httpx.Headers(raw_headers)130    assert dict(headers) == {"custom": "Code point: ☃"}131    assert headers.encoding == "utf-8"132133134def test_headers_decode_iso_8859_1():135    """136    Headers containing non-UTF-8 codepoints should default to decoding as iso-8859-1.137    """138    raw_headers = [(b"Custom", "Code point: ÿ".encode("iso-8859-1"))]139    headers = httpx.Headers(raw_headers)140    assert dict(headers) == {"custom": "Code point: ÿ"}141    assert headers.encoding == "iso-8859-1"142143144def test_headers_decode_explicit_encoding():145    """146    An explicit encoding may be set on headers in order to force a147    particular decoding.148    """149    raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]150    headers = httpx.Headers(raw_headers)151    headers.encoding = "iso-8859-1"152    assert dict(headers) == {"custom": "Code point: â\x98\x83"}153    assert headers.encoding == "iso-8859-1"154155156def test_multiple_headers():157    """158    `Headers.get_list` should support both split_commas=False and split_commas=True.159    """160    h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])161    assert h.get_list("Set-Cookie") == ["a, b", "c"]162163    h = httpx.Headers([("vary", "a, b"), ("vary", "c")])164    assert h.get_list("Vary", split_commas=True) == ["a", "b", "c"]165166167@pytest.mark.parametrize("header", ["authorization", "proxy-authorization"])168def test_sensitive_headers(header):169    """170    Some headers should be obfuscated because they contain sensitive data.171    """172    value = "s3kr3t"173    h = httpx.Headers({header: value})174    assert repr(h) == "Headers({'%s': '[secure]'})" % header