Skip to content
Myr

test_headers.py

This file

Path

tests/models/test_headers.py

172 lines · 5.2 KiB · Python

Last changed by

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

History of this file

Take it

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

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

lines 1–48why, 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.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')])"23 assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])24 assert h != [("a", "123"), ("A", "456"), ("b", "789")]2526 h = httpx.Headers({"a": "123", "b": "789"})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():34 h = httpx.Headers()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")]4748

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

lines 49–56why, one step back
49def test_copy_headers_method():
50 headers = httpx.Headers({"custom": "example"})51 headers_copy = headers.copy()52 assert headers == headers_copy53 assert headers is not headers_copy545556def test_copy_headers_init():

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

lines 57–172why, one step back
57 headers = httpx.Headers({"custom": "example"})
58 headers_copy = httpx.Headers(headers)59 assert headers == headers_copy606162def test_headers_insert_retains_ordering():63 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})64 headers["b"] = "123"65 assert list(headers.values()) == ["a", "123", "c"]666768def test_headers_insert_appends_if_new():69 headers = httpx.Headers({"a": "a", "b": "b", "c": "c"})70 headers["d"] = "123"71 assert list(headers.values()) == ["a", "b", "c", "123"]727374def test_headers_insert_removes_all_existing():75 headers = httpx.Headers([("a", "123"), ("a", "456")])76 headers["a"] = "789"77 assert dict(headers) == {"a": "789"}787980def test_headers_delete_removes_all_existing():81 headers = httpx.Headers([("a", "123"), ("a", "456")])82 del headers["a"]83 assert dict(headers) == {}848586def test_headers_dict_repr():87 """88 Headers should display with a dict repr by default.89 """90 headers = httpx.Headers({"custom": "example"})91 assert repr(headers) == "Headers({'custom': 'example'})"929394def test_headers_encoding_in_repr():95 """96 Headers should display an encoding in the repr if required.97 """98 headers = httpx.Headers({b"custom": "example ☃".encode("utf-8")})99 assert repr(headers) == "Headers({'custom': 'example ☃'}, encoding='utf-8')"100101102def test_headers_list_repr():103 """104 Headers should display with a list repr if they include multiple identical keys.105 """106 headers = httpx.Headers([("custom", "example 1"), ("custom", "example 2")])107 assert (108 repr(headers) == "Headers([('custom', 'example 1'), ('custom', 'example 2')])"109 )110111112def test_headers_decode_ascii():113 """114 Headers should decode as ascii by default.115 """116 raw_headers = [(b"Custom", b"Example")]117 headers = httpx.Headers(raw_headers)118 assert dict(headers) == {"custom": "Example"}119 assert headers.encoding == "ascii"120121122def test_headers_decode_utf_8():123 """124 Headers containing non-ascii codepoints should default to decoding as utf-8.125 """126 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]127 headers = httpx.Headers(raw_headers)128 assert dict(headers) == {"custom": "Code point: ☃"}129 assert headers.encoding == "utf-8"130131132def test_headers_decode_iso_8859_1():133 """134 Headers containing non-UTF-8 codepoints should default to decoding as iso-8859-1.135 """136 raw_headers = [(b"Custom", "Code point: ÿ".encode("iso-8859-1"))]137 headers = httpx.Headers(raw_headers)138 assert dict(headers) == {"custom": "Code point: ÿ"}139 assert headers.encoding == "iso-8859-1"140141142def test_headers_decode_explicit_encoding():143 """144 An explicit encoding may be set on headers in order to force a145 particular decoding.146 """147 raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]148 headers = httpx.Headers(raw_headers)149 headers.encoding = "iso-8859-1"150 assert dict(headers) == {"custom": "Code point: â\x98\x83"}151 assert headers.encoding == "iso-8859-1"152153154def test_multiple_headers():155 """156 Most headers should split by commas for `getlist`, except 'Set-Cookie'.157 """158 h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])159 h.getlist("Set-Cookie") == ["a, b", "b"]160161 h = httpx.Headers([("vary", "a, b"), ("vary", "c")])162 h.getlist("Vary") == ["a", "b", "c"]163164165@pytest.mark.parametrize("header", ["authorization", "proxy-authorization"])166def test_sensitive_headers(header):167 """168 Some headers should be obfuscated because they contain sensitive data.169 """170 value = "s3kr3t"171 h = httpx.Headers({header: value})172 assert repr(h) == "Headers({'%s': '[secure]'})" % header