Skip to content
Myr

test_headers.py

This file

Path

tests/models/test_headers.py

176 lines · 5.4 KiB · Python

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than c52e7d2 point at it.

Added missing Request __init__ parameters (#1456)Simon Willisonbefore adoptionc52e7d2

lines 1–176why, one step back
1import pytest
23import 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 == [("a", "123"), ("b", "789"), ("a", "456")]26 assert h == [("a", "123"), ("A", "456"), ("b", "789")]27 assert h == {"a": "123", "A": "456", "b": "789"}28 assert h != "a: 123\nA: 456\nb: 789"2930 h = httpx.Headers({"a": "123", "b": "789"})31 assert h["A"] == "123"32 assert h["B"] == "789"33 assert h.raw == [(b"a", b"123"), (b"b", b"789")]34 assert repr(h) == "Headers({'a': '123', 'b': '789'})"353637def test_header_mutations():38 h = httpx.Headers()39 assert dict(h) == {}40 h["a"] = "1"41 assert dict(h) == {"a": "1"}42 h["a"] = "2"43 assert dict(h) == {"a": "2"}44 h.setdefault("a", "3")45 assert dict(h) == {"a": "2"}46 h.setdefault("b", "4")47 assert dict(h) == {"a": "2", "b": "4"}48 del h["a"]49 assert dict(h) == {"b": "4"}50 assert h.raw == [(b"b", b"4")]515253def test_copy_headers_method():54 headers = httpx.Headers({"custom": "example"})55 headers_copy = headers.copy()56 assert headers == headers_copy57 assert headers is not headers_copy585960def test_copy_headers_init():61 headers = httpx.Headers({"custom": "example"})62 headers_copy = httpx.Headers(headers)63 assert headers == headers_copy646566def test_headers_insert_retains_ordering():67 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})68 headers["b"] = "123"69 assert list(headers.values()) == ["a", "123", "c"]707172def test_headers_insert_appends_if_new():73 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})74 headers["d"] = "123"75 assert list(headers.values()) == ["a", "b", "c", "123"]767778def test_headers_insert_removes_all_existing():79 headers = httpx.Headers([("a", "123"), ("a", "456")])80 headers["a"] = "789"81 assert dict(headers) == {"a": "789"}828384def test_headers_delete_removes_all_existing():85 headers = httpx.Headers([("a", "123"), ("a", "456")])86 del headers["a"]87 assert dict(headers) == {}888990def test_headers_dict_repr():91 """92 Headers should display with a dict repr by default.93 """94 headers = httpx.Headers({"custom": "example"})95 assert repr(headers) == "Headers({'custom': 'example'})"969798def test_headers_encoding_in_repr():99 """100 Headers should display an encoding in the repr if required.101 """102 headers = httpx.Headers({b"custom": "example ☃".encode("utf-8")})103 assert repr(headers) == "Headers({'custom': 'example ☃'}, encoding='utf-8')"104105106def test_headers_list_repr():107 """108 Headers should display with a list repr if they include multiple identical keys.109 """110 headers = httpx.Headers([("custom", "example 1"), ("custom", "example 2")])111 assert (112 repr(headers) == "Headers([('custom', 'example 1'), ('custom', 'example 2')])"113 )114115116def test_headers_decode_ascii():117 """118 Headers should decode as ascii by default.119 """120 raw_headers = [(b"Custom", b"Example")]121 headers = httpx.Headers(raw_headers)122 assert dict(headers) == {"custom": "Example"}123 assert headers.encoding == "ascii"124125126def test_headers_decode_utf_8():127 """128 Headers containing non-ascii codepoints should default to decoding as utf-8.129 """130 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]131 headers = httpx.Headers(raw_headers)132 assert dict(headers) == {"custom": "Code point: ☃"}133 assert headers.encoding == "utf-8"134135136def test_headers_decode_iso_8859_1():137 """138 Headers containing non-UTF-8 codepoints should default to decoding as iso-8859-1.139 """140 raw_headers = [(b"Custom", "Code point: ÿ".encode("iso-8859-1"))]141 headers = httpx.Headers(raw_headers)142 assert dict(headers) == {"custom": "Code point: ÿ"}143 assert headers.encoding == "iso-8859-1"144145146def test_headers_decode_explicit_encoding():147 """148 An explicit encoding may be set on headers in order to force a149 particular decoding.150 """151 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]152 headers = httpx.Headers(raw_headers)153 headers.encoding = "iso-8859-1"154 assert dict(headers) == {"custom": "Code point: â\x98\x83"}155 assert headers.encoding == "iso-8859-1"156157158def test_multiple_headers():159 """160 `Headers.get_list` should support both split_commas=False and split_commas=True.161 """162 h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])163 assert h.get_list("Set-Cookie") == ["a, b", "c"]164165 h = httpx.Headers([("vary", "a, b"), ("vary", "c")])166 assert h.get_list("Vary", split_commas=True) == ["a", "b", "c"]167168169@pytest.mark.parametrize("header", ["authorization", "proxy-authorization"])170def test_sensitive_headers(header):171 """172 Some headers should be obfuscated because they contain sensitive data.173 """174 value = "s3kr3t"175 h = httpx.Headers({header: value})176 assert repr(h) == "Headers({'%s': '[secure]'})" % header