test_headers.py
This file
Path
tests/models/test_headers.py
153 lines · 4.8 KiB · Python
Last changed by
Merge pull request #45 from taoufik07/patch-1Tom Christiebefore adoptionfc7a79a
History of this fileTake it
Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 1–148why, one step back1import httpcore234def test_headers():5 h = httpcore.Headers([("a", "123"), ("a", "456"), ("b", "789")])6 assert "a" in h7 assert "A" in h8 assert "b" in h9 assert "B" in h10 assert "c" not in h11 assert h["a"] == "123, 456"12 assert h.get("a") == "123, 456"13 assert h.get("nope", default=None) is None14 assert h.getlist("a") == ["123", "456"]15 assert h.keys() == ["a", "a", "b"]16 assert h.values() == ["123", "456", "789"]17 assert h.items() == [("a", "123"), ("a", "456"), ("b", "789")]18 assert list(h) == ["a", "a", "b"]19 assert dict(h) == {"a": "123, 456", "b": "789"}20 assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"21 assert h == httpcore.Headers([("a", "123"), ("b", "789"), ("a", "456")])22 assert h != [("a", "123"), ("A", "456"), ("b", "789")]2324 h = httpcore.Headers({"a": "123", "b": "789"})25 assert h["A"] == "123"26 assert h["B"] == "789"27 assert h.raw == [(b"a", b"123"), (b"b", b"789")]28 assert repr(h) == "Headers({'a': '123', 'b': '789'})"293031def test_header_mutations():32 h = httpcore.Headers()33 assert dict(h) == {}34 h["a"] = "1"35 assert dict(h) == {"a": "1"}36 h["a"] = "2"37 assert dict(h) == {"a": "2"}38 h.setdefault("a", "3")39 assert dict(h) == {"a": "2"}40 h.setdefault("b", "4")41 assert dict(h) == {"a": "2", "b": "4"}42 del h["a"]43 assert dict(h) == {"b": "4"}44 assert h.raw == [(b"b", b"4")]454647def test_copy_headers():48 headers = httpcore.Headers({"custom": "example"})49 headers_copy = httpcore.Headers(headers)50 assert headers == headers_copy515253def test_headers_insert_retains_ordering():54 headers = httpcore.Headers({"a": "a", "b": "b", "c": "c"})55 headers["b"] = "123"56 assert list(headers.values()) == ["a", "123", "c"]575859def test_headers_insert_appends_if_new():60 headers = httpcore.Headers({"a": "a", "b": "b", "c": "c"})61 headers["d"] = "123"62 assert list(headers.values()) == ["a", "b", "c", "123"]636465def test_headers_insert_removes_all_existing():66 headers = httpcore.Headers([("a", "123"), ("a", "456")])67 headers["a"] = "789"68 assert dict(headers) == {"a": "789"}697071def test_headers_delete_removes_all_existing():72 headers = httpcore.Headers([("a", "123"), ("a", "456")])73 del headers["a"]74 assert dict(headers) == {}757677def test_headers_dict_repr():78 """79 Headers should display with a dict repr by default.80 """81 headers = httpcore.Headers({"custom": "example"})82 assert repr(headers) == "Headers({'custom': 'example'})"838485def test_headers_encoding_in_repr():86 """87 Headers should display an encoding in the repr if required.88 """89 headers = httpcore.Headers({b"custom": "example ☃".encode("utf-8")})90 assert repr(headers) == "Headers({'custom': 'example ☃'}, encoding='utf-8')"919293def test_headers_list_repr():94 """95 Headers should display with a list repr if they include multiple identical keys.96 """97 headers = httpcore.Headers([("custom", "example 1"), ("custom", "example 2")])98 assert (99 repr(headers) == "Headers([('custom', 'example 1'), ('custom', 'example 2')])"100 )101102103def test_headers_decode_ascii():104 """105 Headers should decode as ascii by default.106 """107 raw_headers = [(b"Custom", b"Example")]108 headers = httpcore.Headers(raw_headers)109 assert dict(headers) == {"custom": "Example"}110 assert headers.encoding == "ascii"111112113def test_headers_decode_utf_8():114 """115 Headers containing non-ascii codepoints should default to decoding as utf-8.116 """117 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]118 headers = httpcore.Headers(raw_headers)119 assert dict(headers) == {"custom": "Code point: ☃"}120 assert headers.encoding == "utf-8"121122123def test_headers_decode_iso_8859_1():124 """125 Headers containing non-UTF-8 codepoints should default to decoding as iso-8859-1.126 """127 raw_headers = [(b"Custom", "Code point: ÿ".encode("iso-8859-1"))]128 headers = httpcore.Headers(raw_headers)129 assert dict(headers) == {"custom": "Code point: ÿ"}130 assert headers.encoding == "iso-8859-1"131132133def test_headers_decode_explicit_encoding():134 """135 An explicit encoding may be set on headers in order to force a136 particular decoding.137 """138 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]139 headers = httpcore.Headers(raw_headers)140 headers.encoding = "iso-8859-1"141 assert dict(headers) == {"custom": "Code point: â\x98\x83"}142 assert headers.encoding == "iso-8859-1"143144145def test_multiple_headers():146 """147 Most headers should split by commas for `getlist`, except 'Set-Cookie'.148 """Response.contentTom Christiebefore adoption4c55113
lines 149–150why, one step back149 h = httpcore.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])150 h.getlist("Set-Cookie") == ["a, b", "b"]Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 151why, one step back151Response.contentTom Christiebefore adoption4c55113
lines 152–153why, one step back152 h = httpcore.Headers([("vary", "a, b"), ("vary", "c")])153 h.getlist("Vary") == ["a", "b", "c"]