How to use the httpx.Headers function in httpx

To help you get started, weā€™ve selected a few httpx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / httpx / tests / models / test_headers.py View on Github external
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')])"
    )
github encode / httpx / tests / models / test_headers.py View on Github external
def test_headers_insert_removes_all_existing():
    headers = httpx.Headers([("a", "123"), ("a", "456")])
    headers["a"] = "789"
    assert dict(headers) == {"a": "789"}
github encode / httpx / tests / models / test_headers.py View on Github external
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"]
github lundberg / respx / tests / test_api.py View on Github external
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)
github encode / httpx / tests / models / test_headers.py View on Github external
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'})"
github encode / httpx / tests / models / test_headers.py View on Github external
def test_copy_headers():
    headers = httpx.Headers({"custom": "example"})
    headers_copy = httpx.Headers(headers)
    assert headers == headers_copy
github encode / httpx / tests / models / test_headers.py View on Github external
def test_headers_delete_removes_all_existing():
    headers = httpx.Headers([("a", "123"), ("a", "456")])
    del headers["a"]
    assert dict(headers) == {}
github encode / httpx / tests / models / test_headers.py View on Github external
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')"
github msys2 / msys2-web / app / fetch.py View on Github external
    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
github lundberg / respx / respx / models.py View on Github external
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