Skip to content
Myr

test_headers.py

This file

Path

tests/models/test_headers.py

153 lines · 4.7 KiB · Python

Last changed by

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

History of this file

Take it

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 1why, one step back
1import httpx

Update README.mdTom Christiebefore adoption84b9691

lines 2–4why, one step back
2
34def test_headers():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 5why, one step back
5 h = httpx.Headers([("a", "123"), ("a", "456"), ("b", "789")])

Update README.mdTom Christiebefore adoption84b9691

lines 6–20why, one step back
6 assert "a" in h
7 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')])"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 21why, one step back
21 assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])

Update README.mdTom Christiebefore adoption84b9691

lines 22–23why, one step back
22 assert h != [("a", "123"), ("A", "456"), ("b", "789")]
23

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 24why, one step back
24 h = httpx.Headers({"a": "123", "b": "789"})

Update README.mdTom Christiebefore adoption84b9691

lines 25–31why, one step back
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():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 32why, one step back
32 h = httpx.Headers()

Update README.mdTom Christiebefore adoption84b9691

lines 33–47why, one step back
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():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

lines 48–49why, one step back
48 headers = httpx.Headers({"custom": "example"})
49 headers_copy = httpx.Headers(headers)

Update README.mdTom Christiebefore adoption84b9691

lines 50–53why, one step back
50 assert headers == headers_copy
515253def test_headers_insert_retains_ordering():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 54why, one step back
54 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})

Update README.mdTom Christiebefore adoption84b9691

lines 55–59why, one step back
55 headers["b"] = "123"
56 assert list(headers.values()) == ["a", "123", "c"]575859def test_headers_insert_appends_if_new():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 60why, one step back
60 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})

Update README.mdTom Christiebefore adoption84b9691

lines 61–65why, one step back
61 headers["d"] = "123"
62 assert list(headers.values()) == ["a", "b", "c", "123"]636465def test_headers_insert_removes_all_existing():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 66why, one step back
66 headers = httpx.Headers([("a", "123"), ("a", "456")])

Update README.mdTom Christiebefore adoption84b9691

lines 67–71why, one step back
67 headers["a"] = "789"
68 assert dict(headers) == {"a": "789"}697071def test_headers_delete_removes_all_existing():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 72why, one step back
72 headers = httpx.Headers([("a", "123"), ("a", "456")])

Update README.mdTom Christiebefore adoption84b9691

lines 73–80why, one step back
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 """

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 81why, one step back
81 headers = httpx.Headers({"custom": "example"})

Update README.mdTom Christiebefore adoption84b9691

lines 82–88why, one step back
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 """

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 89why, one step back
89 headers = httpx.Headers({b"custom": "example ☃".encode("utf-8")})

Update README.mdTom Christiebefore adoption84b9691

lines 90–96why, one step back
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 """

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 97why, one step back
97 headers = httpx.Headers([("custom", "example 1"), ("custom", "example 2")])

Update README.mdTom Christiebefore adoption84b9691

lines 98–107why, one step back
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")]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 108why, one step back
108 headers = httpx.Headers(raw_headers)

Update README.mdTom Christiebefore adoption84b9691

lines 109–117why, one step back
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"))]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 118why, one step back
118 headers = httpx.Headers(raw_headers)

Update README.mdTom Christiebefore adoption84b9691

lines 119–127why, one step back
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"))]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 128why, one step back
128 headers = httpx.Headers(raw_headers)

Update README.mdTom Christiebefore adoption84b9691

lines 129–138why, one step back
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"))]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 139why, one step back
139 headers = httpx.Headers(raw_headers)

Update README.mdTom Christiebefore adoption84b9691

lines 140–148why, one step back
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 """

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 149why, one step back
149 h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])

Update README.mdTom Christiebefore adoption84b9691

lines 150–151why, one step back
150 h.getlist("Set-Cookie") == ["a, b", "b"]
151

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 152why, one step back
152 h = httpx.Headers([("vary", "a, b"), ("vary", "c")])

Update README.mdTom Christiebefore adoption84b9691

line 153why, one step back
153 h.getlist("Vary") == ["a", "b", "c"]