Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

190 lines · 5.6 KiB · Python

Last changed by

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

History of this file

Take it

1#!/usr/bin/env python323import json45import pytest67import httpx8from tests.utils import MockTransport91011def echo_headers(request: httpx.Request) -> httpx.Response:12    data = {"headers": dict(request.headers)}13    content = json.dumps(data).encode("utf-8")14    return httpx.Response(200, content=content)151617def test_client_header():18    """19    Set a header in the Client.20    """21    url = "http://example.org/echo_headers"22    headers = {"Example-Header": "example-value"}2324    client = httpx.Client(transport=MockTransport(echo_headers), headers=headers)25    response = client.get(url)2627    assert response.status_code == 20028    assert response.json() == {29        "headers": {30            "accept": "*/*",31            "accept-encoding": "gzip, deflate, br",32            "connection": "keep-alive",33            "example-header": "example-value",34            "host": "example.org",35            "user-agent": f"python-httpx/{httpx.__version__}",36        }37    }383940def test_header_merge():41    url = "http://example.org/echo_headers"42    client_headers = {"User-Agent": "python-myclient/0.2.1"}43    request_headers = {"X-Auth-Token": "FooBarBazToken"}44    client = httpx.Client(transport=MockTransport(echo_headers), headers=client_headers)45    response = client.get(url, headers=request_headers)4647    assert response.status_code == 20048    assert response.json() == {49        "headers": {50            "accept": "*/*",51            "accept-encoding": "gzip, deflate, br",52            "connection": "keep-alive",53            "host": "example.org",54            "user-agent": "python-myclient/0.2.1",55            "x-auth-token": "FooBarBazToken",56        }57    }585960def test_header_merge_conflicting_headers():61    url = "http://example.org/echo_headers"62    client_headers = {"X-Auth-Token": "FooBar"}63    request_headers = {"X-Auth-Token": "BazToken"}64    client = httpx.Client(transport=MockTransport(echo_headers), headers=client_headers)65    response = client.get(url, headers=request_headers)6667    assert response.status_code == 20068    assert response.json() == {69        "headers": {70            "accept": "*/*",71            "accept-encoding": "gzip, deflate, br",72            "connection": "keep-alive",73            "host": "example.org",74            "user-agent": f"python-httpx/{httpx.__version__}",75            "x-auth-token": "BazToken",76        }77    }787980def test_header_update():81    url = "http://example.org/echo_headers"82    client = httpx.Client(transport=MockTransport(echo_headers))83    first_response = client.get(url)84    client.headers.update(85        {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}86    )87    second_response = client.get(url)8889    assert first_response.status_code == 20090    assert first_response.json() == {91        "headers": {92            "accept": "*/*",93            "accept-encoding": "gzip, deflate, br",94            "connection": "keep-alive",95            "host": "example.org",96            "user-agent": f"python-httpx/{httpx.__version__}",97        }98    }99100    assert second_response.status_code == 200101    assert second_response.json() == {102        "headers": {103            "accept": "*/*",104            "accept-encoding": "gzip, deflate, br",105            "another-header": "AThing",106            "connection": "keep-alive",107            "host": "example.org",108            "user-agent": "python-myclient/0.2.1",109        }110    }111112113def test_remove_default_header():114    """115    Remove a default header from the Client.116    """117    url = "http://example.org/echo_headers"118119    client = httpx.Client(transport=MockTransport(echo_headers))120    del client.headers["User-Agent"]121122    response = client.get(url)123124    assert response.status_code == 200125    assert response.json() == {126        "headers": {127            "accept": "*/*",128            "accept-encoding": "gzip, deflate, br",129            "connection": "keep-alive",130            "host": "example.org",131        }132    }133134135def test_header_does_not_exist():136    headers = httpx.Headers({"foo": "bar"})137    with pytest.raises(KeyError):138        del headers["baz"]139140141def test_host_with_auth_and_port_in_url():142    """143    The Host header should only include the hostname, or hostname:port144    (for non-default ports only). Any userinfo or default port should not145    be present.146    """147    url = "http://username:password@example.org:80/echo_headers"148149    client = httpx.Client(transport=MockTransport(echo_headers))150    response = client.get(url)151152    assert response.status_code == 200153    assert response.json() == {154        "headers": {155            "accept": "*/*",156            "accept-encoding": "gzip, deflate, br",157            "connection": "keep-alive",158            "host": "example.org",159            "user-agent": f"python-httpx/{httpx.__version__}",160            "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",161        }162    }163164165def test_host_with_non_default_port_in_url():166    """167    If the URL includes a non-default port, then it should be included in168    the Host header.169    """170    url = "http://username:password@example.org:123/echo_headers"171172    client = httpx.Client(transport=MockTransport(echo_headers))173    response = client.get(url)174175    assert response.status_code == 200176    assert response.json() == {177        "headers": {178            "accept": "*/*",179            "accept-encoding": "gzip, deflate, br",180            "connection": "keep-alive",181            "host": "example.org:123",182            "user-agent": f"python-httpx/{httpx.__version__}",183            "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",184        }185    }186187188def test_request_auto_headers():189    request = httpx.Request("GET", "https://www.example.org/")190    assert "host" in request.headers