Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

187 lines · 5.5 KiB · Python

Last changed by

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

History of this file

Take it

1#!/usr/bin/env python323import pytest45import httpx6from tests.utils import MockTransport789def echo_headers(request: httpx.Request) -> httpx.Response:10    data = {"headers": dict(request.headers)}11    return httpx.Response(200, json=data)121314def test_client_header():15    """16    Set a header in the Client.17    """18    url = "http://example.org/echo_headers"19    headers = {"Example-Header": "example-value"}2021    client = httpx.Client(transport=MockTransport(echo_headers), headers=headers)22    response = client.get(url)2324    assert response.status_code == 20025    assert response.json() == {26        "headers": {27            "accept": "*/*",28            "accept-encoding": "gzip, deflate, br",29            "connection": "keep-alive",30            "example-header": "example-value",31            "host": "example.org",32            "user-agent": f"python-httpx/{httpx.__version__}",33        }34    }353637def test_header_merge():38    url = "http://example.org/echo_headers"39    client_headers = {"User-Agent": "python-myclient/0.2.1"}40    request_headers = {"X-Auth-Token": "FooBarBazToken"}41    client = httpx.Client(transport=MockTransport(echo_headers), headers=client_headers)42    response = client.get(url, headers=request_headers)4344    assert response.status_code == 20045    assert response.json() == {46        "headers": {47            "accept": "*/*",48            "accept-encoding": "gzip, deflate, br",49            "connection": "keep-alive",50            "host": "example.org",51            "user-agent": "python-myclient/0.2.1",52            "x-auth-token": "FooBarBazToken",53        }54    }555657def test_header_merge_conflicting_headers():58    url = "http://example.org/echo_headers"59    client_headers = {"X-Auth-Token": "FooBar"}60    request_headers = {"X-Auth-Token": "BazToken"}61    client = httpx.Client(transport=MockTransport(echo_headers), headers=client_headers)62    response = client.get(url, headers=request_headers)6364    assert response.status_code == 20065    assert response.json() == {66        "headers": {67            "accept": "*/*",68            "accept-encoding": "gzip, deflate, br",69            "connection": "keep-alive",70            "host": "example.org",71            "user-agent": f"python-httpx/{httpx.__version__}",72            "x-auth-token": "BazToken",73        }74    }757677def test_header_update():78    url = "http://example.org/echo_headers"79    client = httpx.Client(transport=MockTransport(echo_headers))80    first_response = client.get(url)81    client.headers.update(82        {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}83    )84    second_response = client.get(url)8586    assert first_response.status_code == 20087    assert first_response.json() == {88        "headers": {89            "accept": "*/*",90            "accept-encoding": "gzip, deflate, br",91            "connection": "keep-alive",92            "host": "example.org",93            "user-agent": f"python-httpx/{httpx.__version__}",94        }95    }9697    assert second_response.status_code == 20098    assert second_response.json() == {99        "headers": {100            "accept": "*/*",101            "accept-encoding": "gzip, deflate, br",102            "another-header": "AThing",103            "connection": "keep-alive",104            "host": "example.org",105            "user-agent": "python-myclient/0.2.1",106        }107    }108109110def test_remove_default_header():111    """112    Remove a default header from the Client.113    """114    url = "http://example.org/echo_headers"115116    client = httpx.Client(transport=MockTransport(echo_headers))117    del client.headers["User-Agent"]118119    response = client.get(url)120121    assert response.status_code == 200122    assert response.json() == {123        "headers": {124            "accept": "*/*",125            "accept-encoding": "gzip, deflate, br",126            "connection": "keep-alive",127            "host": "example.org",128        }129    }130131132def test_header_does_not_exist():133    headers = httpx.Headers({"foo": "bar"})134    with pytest.raises(KeyError):135        del headers["baz"]136137138def test_host_with_auth_and_port_in_url():139    """140    The Host header should only include the hostname, or hostname:port141    (for non-default ports only). Any userinfo or default port should not142    be present.143    """144    url = "http://username:password@example.org:80/echo_headers"145146    client = httpx.Client(transport=MockTransport(echo_headers))147    response = client.get(url)148149    assert response.status_code == 200150    assert response.json() == {151        "headers": {152            "accept": "*/*",153            "accept-encoding": "gzip, deflate, br",154            "connection": "keep-alive",155            "host": "example.org",156            "user-agent": f"python-httpx/{httpx.__version__}",157            "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",158        }159    }160161162def test_host_with_non_default_port_in_url():163    """164    If the URL includes a non-default port, then it should be included in165    the Host header.166    """167    url = "http://username:password@example.org:123/echo_headers"168169    client = httpx.Client(transport=MockTransport(echo_headers))170    response = client.get(url)171172    assert response.status_code == 200173    assert response.json() == {174        "headers": {175            "accept": "*/*",176            "accept-encoding": "gzip, deflate, br",177            "connection": "keep-alive",178            "host": "example.org:123",179            "user-agent": f"python-httpx/{httpx.__version__}",180            "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",181        }182    }183184185def test_request_auto_headers():186    request = httpx.Request("GET", "https://www.example.org/")187    assert "host" in request.headers