Skip to content
Myr

test_headers.py

This file

Path

tests/models/test_headers.py

165 lines · 5.0 KiB · Python

Last changed by

Add debug logs to ASGIDispatch (#371)Florimond Mancabefore adoptione62e5c3

History of this file

Take it

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

Add debug logs to ASGIDispatch (#371)Florimond Mancabefore adoptione62e5c3

lines 1–2why, one step back
1import pytest
2

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 3why, one step back
3import httpx

Update README.mdTom Christiebefore adoption53a3e55

lines 4–6why, one step back
4
56def test_headers():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 8–22why, one step back
8 assert "a" in h
9 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.getlist("a") == ["123", "456"]17 assert h.keys() == ["a", "a", "b"]18 assert h.values() == ["123", "456", "789"]19 assert h.items() == [("a", "123"), ("a", "456"), ("b", "789")]20 assert list(h) == ["a", "a", "b"]21 assert dict(h) == {"a": "123, 456", "b": "789"}22 assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 27–33why, one step back
27 assert h["A"] == "123"
28 assert h["B"] == "789"29 assert h.raw == [(b"a", b"123"), (b"b", b"789")]30 assert repr(h) == "Headers({'a': '123', 'b': '789'})"313233def test_header_mutations():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 35–49why, one step back
35 assert dict(h) == {}
36 h["a"] = "1"37 assert dict(h) == {"a": "1"}38 h["a"] = "2"39 assert dict(h) == {"a": "2"}40 h.setdefault("a", "3")41 assert dict(h) == {"a": "2"}42 h.setdefault("b", "4")43 assert dict(h) == {"a": "2", "b": "4"}44 del h["a"]45 assert dict(h) == {"b": "4"}46 assert h.raw == [(b"b", b"4")]474849def test_copy_headers():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 52–55why, one step back
52 assert headers == headers_copy
535455def test_headers_insert_retains_ordering():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 57–61why, one step back
57 headers["b"] = "123"
58 assert list(headers.values()) == ["a", "123", "c"]596061def test_headers_insert_appends_if_new():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 63–67why, one step back
63 headers["d"] = "123"
64 assert list(headers.values()) == ["a", "b", "c", "123"]656667def test_headers_insert_removes_all_existing():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 69–73why, one step back
69 headers["a"] = "789"
70 assert dict(headers) == {"a": "789"}717273def test_headers_delete_removes_all_existing():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 75–82why, one step back
75 del headers["a"]
76 assert dict(headers) == {}777879def test_headers_dict_repr():80 """81 Headers should display with a dict repr by default.82 """

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 84–90why, one step back
84 assert repr(headers) == "Headers({'custom': 'example'})"
858687def test_headers_encoding_in_repr():88 """89 Headers should display an encoding in the repr if required.90 """

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 92–98why, one step back
92 assert repr(headers) == "Headers({'custom': 'example ☃'}, encoding='utf-8')"
939495def test_headers_list_repr():96 """97 Headers should display with a list repr if they include multiple identical keys.98 """

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 100–109why, one step back
100 assert (
101 repr(headers) == "Headers([('custom', 'example 1'), ('custom', 'example 2')])"102 )103104105def test_headers_decode_ascii():106 """107 Headers should decode as ascii by default.108 """109 raw_headers = [(b"Custom", b"Example")]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 111–119why, one step back
111 assert dict(headers) == {"custom": "Example"}
112 assert headers.encoding == "ascii"113114115def test_headers_decode_utf_8():116 """117 Headers containing non-ascii codepoints should default to decoding as utf-8.118 """119 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 121–129why, one step back
121 assert dict(headers) == {"custom": "Code point: ☃"}
122 assert headers.encoding == "utf-8"123124125def test_headers_decode_iso_8859_1():126 """127 Headers containing non-UTF-8 codepoints should default to decoding as iso-8859-1.128 """129 raw_headers = [(b"Custom", "Code point: ÿ".encode("iso-8859-1"))]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 131–140why, one step back
131 assert dict(headers) == {"custom": "Code point: ÿ"}
132 assert headers.encoding == "iso-8859-1"133134135def test_headers_decode_explicit_encoding():136 """137 An explicit encoding may be set on headers in order to force a138 particular decoding.139 """140 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

lines 142–150why, one step back
142 headers.encoding = "iso-8859-1"
143 assert dict(headers) == {"custom": "Code point: â\x98\x83"}144 assert headers.encoding == "iso-8859-1"145146147def test_multiple_headers():148 """149 Most headers should split by commas for `getlist`, except 'Set-Cookie'.150 """

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update README.mdTom Christiebefore adoption53a3e55

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

Add debug logs to ASGIDispatch (#371)Florimond Mancabefore adoptione62e5c3

lines 156–165why, one step back
156
157158@pytest.mark.parametrize("header", ["authorization", "proxy-authorization"])159def test_sensitive_headers(header):160 """161 Some headers should be obfuscated because they contain sensitive data.162 """163 value = "s3kr3t"164 h = httpx.Headers({header: value})165 assert repr(h) == "Headers({'%s': '[secure]'})" % header