Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_headers_list_repr():
"""
Headers should display with a list repr if they include multiple identical keys.
"""
headers = httpx.Headers([("custom", "example 1"), ("custom", "example 2")])
assert (
repr(headers) == "Headers([('custom', 'example 1'), ('custom', 'example 2')])"
)
def test_headers_insert_removes_all_existing():
headers = httpx.Headers([("a", "123"), ("a", "456")])
headers["a"] = "789"
assert dict(headers) == {"a": "789"}
def test_multiple_headers():
"""
Most headers should split by commas for `getlist`, except 'Set-Cookie'.
"""
h = httpx.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])
h.getlist("Set-Cookie") == ["a, b", "b"]
h = httpx.Headers([("vary", "a, b"), ("vary", "c")])
h.getlist("Vary") == ["a", "b", "c"]
async def test_headers(client, headers, content_type, expected):
async with MockTransport() as respx_mock:
url = "https://foo.bar/"
request = respx_mock.get(url, content_type=content_type, headers=headers)
response = await client.get(url)
assert request.called is True
assert response.headers == httpx.Headers(expected)
assert "B" in h
assert "c" not in h
assert h["a"] == "123, 456"
assert h.get("a") == "123, 456"
assert h.get("nope", default=None) is None
assert h.getlist("a") == ["123", "456"]
assert h.keys() == ["a", "a", "b"]
assert h.values() == ["123", "456", "789"]
assert h.items() == [("a", "123"), ("a", "456"), ("b", "789")]
assert list(h) == ["a", "a", "b"]
assert dict(h) == {"a": "123, 456", "b": "789"}
assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"
assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])
assert h != [("a", "123"), ("A", "456"), ("b", "789")]
h = httpx.Headers({"a": "123", "b": "789"})
assert h["A"] == "123"
assert h["B"] == "789"
assert h.raw == [(b"a", b"123"), (b"b", b"789")]
assert repr(h) == "Headers({'a': '123', 'b': '789'})"
def test_copy_headers():
headers = httpx.Headers({"custom": "example"})
headers_copy = httpx.Headers(headers)
assert headers == headers_copy
def test_headers_delete_removes_all_existing():
headers = httpx.Headers([("a", "123"), ("a", "456")])
del headers["a"]
assert dict(headers) == {}
def test_headers_encoding_in_repr():
"""
Headers should display an encoding in the repr if required.
"""
headers = httpx.Headers({b"custom": "example ā".encode("utf-8")})
assert repr(headers) == "Headers({'custom': 'example ā'}, encoding='utf-8')"
async def get_headers(client: httpx.AsyncClient, *args: Any, **kwargs: Any) -> httpx.Headers:
async with client.stream('GET', *args, **kwargs) as r:
r.raise_for_status()
return r.headers
def __init__(
self,
status_code: Optional[int] = None,
headers: Optional[HeaderTypes] = None,
content: Optional[ContentDataTypes] = None,
content_type: Optional[str] = None,
context: Optional[Kwargs] = None,
) -> None:
self.http_version = 1.1
self.status_code = status_code or 200
self.context = context if context is not None else {}
self._content = content if content is not None else b""
self._headers = HTTPXHeaders(headers) if headers else HTTPXHeaders()
if content_type:
self._headers["Content-Type"] = content_type