test_headers.py
This file
Path
tests/models/test_headers.py
153 lines · 4.7 KiB · Python
Last changed by
http3 (#86)Tom Christiebefore adoptionc9747aa
History of this fileTake it
http3 (#86)Tom Christiebefore adoptionc9747aa
line 1why, one step back1import http3Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 2–4why, one step back234def test_headers():http3 (#86)Tom Christiebefore adoptionc9747aa
line 5why, one step back5 h = http3.Headers([("a", "123"), ("a", "456"), ("b", "789")])Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 6–20why, one step back6 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')])"http3 (#86)Tom Christiebefore adoptionc9747aa
line 21why, one step back21 assert h == http3.Headers([("a", "123"), ("b", "789"), ("a", "456")])Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 22–23why, one step back22 assert h != [("a", "123"), ("A", "456"), ("b", "789")]23http3 (#86)Tom Christiebefore adoptionc9747aa
line 24why, one step back24 h = http3.Headers({"a": "123", "b": "789"})Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 25–31why, one step back25 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():http3 (#86)Tom Christiebefore adoptionc9747aa
line 32why, one step back32 h = http3.Headers()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 33–47why, one step back33 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():http3 (#86)Tom Christiebefore adoptionc9747aa
lines 48–49why, one step back48 headers = http3.Headers({"custom": "example"})49 headers_copy = http3.Headers(headers)Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 50–53why, one step back50 assert headers == headers_copy515253def test_headers_insert_retains_ordering():http3 (#86)Tom Christiebefore adoptionc9747aa
line 54why, one step back54 headers = http3.Headers({"a": "a", "b": "b", "c": "c"})Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 55–59why, one step back55 headers["b"] = "123"56 assert list(headers.values()) == ["a", "123", "c"]575859def test_headers_insert_appends_if_new():http3 (#86)Tom Christiebefore adoptionc9747aa
line 60why, one step back60 headers = http3.Headers({"a": "a", "b": "b", "c": "c"})Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 61–65why, one step back61 headers["d"] = "123"62 assert list(headers.values()) == ["a", "b", "c", "123"]636465def test_headers_insert_removes_all_existing():http3 (#86)Tom Christiebefore adoptionc9747aa
line 66why, one step back66 headers = http3.Headers([("a", "123"), ("a", "456")])Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 67–71why, one step back67 headers["a"] = "789"68 assert dict(headers) == {"a": "789"}697071def test_headers_delete_removes_all_existing():http3 (#86)Tom Christiebefore adoptionc9747aa
line 72why, one step back72 headers = http3.Headers([("a", "123"), ("a", "456")])Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 73–80why, one step back73 del headers["a"]74 assert dict(headers) == {}757677def test_headers_dict_repr():78 """79 Headers should display with a dict repr by default.80 """http3 (#86)Tom Christiebefore adoptionc9747aa
line 81why, one step back81 headers = http3.Headers({"custom": "example"})Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 82–88why, one step back82 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 """http3 (#86)Tom Christiebefore adoptionc9747aa
line 89why, one step back89 headers = http3.Headers({b"custom": "example ☃".encode("utf-8")})Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 90–96why, one step back90 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 """http3 (#86)Tom Christiebefore adoptionc9747aa
line 97why, one step back97 headers = http3.Headers([("custom", "example 1"), ("custom", "example 2")])Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 98–107why, one step back98 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")]http3 (#86)Tom Christiebefore adoptionc9747aa
line 108why, one step back108 headers = http3.Headers(raw_headers)Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 109–117why, one step back109 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"))]http3 (#86)Tom Christiebefore adoptionc9747aa
line 118why, one step back118 headers = http3.Headers(raw_headers)Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 119–127why, one step back119 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"))]http3 (#86)Tom Christiebefore adoptionc9747aa
line 128why, one step back128 headers = http3.Headers(raw_headers)Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 129–138why, one step back129 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"))]http3 (#86)Tom Christiebefore adoptionc9747aa
line 139why, one step back139 headers = http3.Headers(raw_headers)Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 140–148why, one step back140 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 """http3 (#86)Tom Christiebefore adoptionc9747aa
line 149why, one step back149 h = http3.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])Response.contentTom Christiebefore adoption4c55113
line 150why, one step back150 h.getlist("Set-Cookie") == ["a, b", "b"]Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 151why, one step back151http3 (#86)Tom Christiebefore adoptionc9747aa
line 152why, one step back152 h = http3.Headers([("vary", "a, b"), ("vary", "c")])Response.contentTom Christiebefore adoption4c55113
line 153why, one step back153 h.getlist("Vary") == ["a", "b", "c"]