test_headers.py
This file
Path
tests/client/test_headers.py
124 lines · 3.6 KiB · Python
Last changed by
Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24
History of this fileTake it
Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47
lines 1–3why, one step back1#!/usr/bin/env python323import jsonRemove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24
line 4why, one step back4Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47
line 5why, one step back5from httpx import (Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24
line 6why, one step back6 AsyncDispatcher,Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47
lines 7–9why, one step back7 AsyncRequest,8 AsyncResponse,9 CertTypes,Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24
line 10why, one step back10 Client,Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47
line 11why, one step back11 TimeoutTypes,Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24
lines 12–13why, one step back12 VerifyTypes,13 __version__,Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47
lines 14–124why, one step back14)151617class MockDispatch(AsyncDispatcher):18 async def send(19 self,20 request: AsyncRequest,21 verify: VerifyTypes = None,22 cert: CertTypes = None,23 timeout: TimeoutTypes = None,24 ) -> AsyncResponse:25 if request.url.path.startswith("/echo_headers"):26 request_headers = dict(request.headers.items())27 body = json.dumps({"headers": request_headers}).encode()28 return AsyncResponse(200, content=body, request=request)293031def test_client_header():32 """33 Set a header in the Client.34 """35 url = "http://example.org/echo_headers"36 headers = {"Example-Header": "example-value"}3738 with Client(dispatch=MockDispatch(), headers=headers) as client:39 response = client.get(url)4041 assert response.status_code == 20042 assert response.json() == {43 "headers": {44 "accept": "*/*",45 "accept-encoding": "gzip, deflate, br",46 "connection": "keep-alive",47 "example-header": "example-value",48 "host": "example.org",49 "user-agent": f"python-httpx/{__version__}",50 }51 }525354def test_header_merge():55 url = "http://example.org/echo_headers"56 client_headers = {"User-Agent": "python-myclient/0.2.1"}57 request_headers = {"X-Auth-Token": "FooBarBazToken"}58 with Client(dispatch=MockDispatch(), headers=client_headers) as client:59 response = client.get(url, headers=request_headers)6061 assert response.status_code == 20062 assert response.json() == {63 "headers": {64 "accept": "*/*",65 "accept-encoding": "gzip, deflate, br",66 "connection": "keep-alive",67 "host": "example.org",68 "user-agent": "python-myclient/0.2.1",69 "x-auth-token": "FooBarBazToken",70 }71 }727374def test_header_merge_conflicting_headers():75 url = "http://example.org/echo_headers"76 client_headers = {"X-Auth-Token": "FooBar"}77 request_headers = {"X-Auth-Token": "BazToken"}78 with Client(dispatch=MockDispatch(), headers=client_headers) as client:79 response = client.get(url, headers=request_headers)8081 assert response.status_code == 20082 assert response.json() == {83 "headers": {84 "accept": "*/*",85 "accept-encoding": "gzip, deflate, br",86 "connection": "keep-alive",87 "host": "example.org",88 "user-agent": f"python-httpx/{__version__}",89 "x-auth-token": "BazToken",90 }91 }929394def test_header_update():95 url = "http://example.org/echo_headers"96 with Client(dispatch=MockDispatch()) as client:97 first_response = client.get(url)98 client.headers.update(99 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}100 )101 second_response = client.get(url)102103 assert first_response.status_code == 200104 assert first_response.json() == {105 "headers": {106 "accept": "*/*",107 "accept-encoding": "gzip, deflate, br",108 "connection": "keep-alive",109 "host": "example.org",110 "user-agent": f"python-httpx/{__version__}",111 }112 }113114 assert second_response.status_code == 200115 assert second_response.json() == {116 "headers": {117 "accept": "*/*",118 "accept-encoding": "gzip, deflate, br",119 "another-header": "AThing",120 "connection": "keep-alive",121 "host": "example.org",122 "user-agent": "python-myclient/0.2.1",123 }124 }