Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

157 lines · 4.4 KiB · Python

Last changed by

Drop sync (#544)Tom Christiebefore adoption206c537

History of this file

Take it

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 1–3why, one step back
1#!/usr/bin/env python3
23import json

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

line 4why, one step back
4

Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b

lines 5–6why, one step back
5import pytest
6

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 7–8why, one step back
7from httpx import (
8 CertTypes,

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

line 9why, one step back
9 Client,

Drop sync (#544)Tom Christiebefore adoption206c537

lines 10–12why, one step back
10 Dispatcher,
11 Request,12 Response,

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

line 13why, one step back
13 TimeoutTypes,

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

lines 14–15why, one step back
14 VerifyTypes,
15 __version__,

Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b

line 16why, one step back
16 models,

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 17–19why, one step back
17)
1819

Drop sync (#544)Tom Christiebefore adoption206c537

line 20why, one step back
20class MockDispatch(Dispatcher):

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 21–22why, one step back
21 async def send(
22 self,

Drop sync (#544)Tom Christiebefore adoption206c537

line 23why, one step back
23 request: Request,

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 24–26why, one step back
24 verify: VerifyTypes = None,
25 cert: CertTypes = None,26 timeout: TimeoutTypes = None,

Drop sync (#544)Tom Christiebefore adoption206c537

line 27why, one step back
27 ) -> Response:

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 28–30why, one step back
28 if request.url.path.startswith("/echo_headers"):
29 request_headers = dict(request.headers.items())30 body = json.dumps({"headers": request_headers}).encode()

Drop sync (#544)Tom Christiebefore adoption206c537

line 31why, one step back
31 return Response(200, content=body, request=request)

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 32–33why, one step back
32
33

Drop sync (#544)Tom Christiebefore adoption206c537

lines 34–35why, one step back
34@pytest.mark.asyncio
35async def test_client_header():

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 36–41why, one step back
36 """
37 Set a header in the Client.38 """39 url = "http://example.org/echo_headers"40 headers = {"Example-Header": "example-value"}41

Drop sync (#544)Tom Christiebefore adoption206c537

lines 42–43why, one step back
42 client = Client(dispatch=MockDispatch(), headers=headers)
43 response = await client.get(url)

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 44–57why, one step back
44
45 assert response.status_code == 20046 assert response.json() == {47 "headers": {48 "accept": "*/*",49 "accept-encoding": "gzip, deflate, br",50 "connection": "keep-alive",51 "example-header": "example-value",52 "host": "example.org",53 "user-agent": f"python-httpx/{__version__}",54 }55 }5657

Drop sync (#544)Tom Christiebefore adoption206c537

lines 58–59why, one step back
58@pytest.mark.asyncio
59async def test_header_merge():

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 60–62why, one step back
60 url = "http://example.org/echo_headers"
61 client_headers = {"User-Agent": "python-myclient/0.2.1"}62 request_headers = {"X-Auth-Token": "FooBarBazToken"}

Drop sync (#544)Tom Christiebefore adoption206c537

lines 63–64why, one step back
63 client = Client(dispatch=MockDispatch(), headers=client_headers)
64 response = await client.get(url, headers=request_headers)

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 65–78why, one step back
65
66 assert response.status_code == 20067 assert response.json() == {68 "headers": {69 "accept": "*/*",70 "accept-encoding": "gzip, deflate, br",71 "connection": "keep-alive",72 "host": "example.org",73 "user-agent": "python-myclient/0.2.1",74 "x-auth-token": "FooBarBazToken",75 }76 }7778

Drop sync (#544)Tom Christiebefore adoption206c537

lines 79–80why, one step back
79@pytest.mark.asyncio
80async def test_header_merge_conflicting_headers():

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 81–83why, one step back
81 url = "http://example.org/echo_headers"
82 client_headers = {"X-Auth-Token": "FooBar"}83 request_headers = {"X-Auth-Token": "BazToken"}

Drop sync (#544)Tom Christiebefore adoption206c537

lines 84–85why, one step back
84 client = Client(dispatch=MockDispatch(), headers=client_headers)
85 response = await client.get(url, headers=request_headers)

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 86–99why, one step back
86
87 assert response.status_code == 20088 assert response.json() == {89 "headers": {90 "accept": "*/*",91 "accept-encoding": "gzip, deflate, br",92 "connection": "keep-alive",93 "host": "example.org",94 "user-agent": f"python-httpx/{__version__}",95 "x-auth-token": "BazToken",96 }97 }9899

Drop sync (#544)Tom Christiebefore adoption206c537

lines 100–101why, one step back
100@pytest.mark.asyncio
101async def test_header_update():

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

line 102why, one step back
102 url = "http://example.org/echo_headers"

Drop sync (#544)Tom Christiebefore adoption206c537

lines 103–108why, one step back
103 client = Client(dispatch=MockDispatch())
104 first_response = await client.get(url)105 client.headers.update(106 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}107 )108 second_response = await client.get(url)

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 109–131why, one step back
109
110 assert first_response.status_code == 200111 assert first_response.json() == {112 "headers": {113 "accept": "*/*",114 "accept-encoding": "gzip, deflate, br",115 "connection": "keep-alive",116 "host": "example.org",117 "user-agent": f"python-httpx/{__version__}",118 }119 }120121 assert second_response.status_code == 200122 assert second_response.json() == {123 "headers": {124 "accept": "*/*",125 "accept-encoding": "gzip, deflate, br",126 "another-header": "AThing",127 "connection": "keep-alive",128 "host": "example.org",129 "user-agent": "python-myclient/0.2.1",130 }131 }

Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b

lines 132–137why, one step back
132
133134def test_header_does_not_exist():135 headers = models.Headers({"foo": "bar"})136 with pytest.raises(KeyError):137 del headers["baz"]

Don't include username/password components in `Host` header (#417)Can Sarıgölbefore adoptiondd3fbcc

lines 138–139why, one step back
138
139

Drop sync (#544)Tom Christiebefore adoption206c537

lines 140–141why, one step back
140@pytest.mark.asyncio
141async def test_host_without_auth_in_header():

Don't include username/password components in `Host` header (#417)Can Sarıgölbefore adoptiondd3fbcc

lines 142–143why, one step back
142 url = "http://username:password@example.org:80/echo_headers"
143

Drop sync (#544)Tom Christiebefore adoption206c537

lines 144–145why, one step back
144 client = Client(dispatch=MockDispatch())
145 response = await client.get(url)

Don't include username/password components in `Host` header (#417)Can Sarıgölbefore adoptiondd3fbcc

lines 146–157why, one step back
146
147 assert response.status_code == 200148 assert response.json() == {149 "headers": {150 "accept": "*/*",151 "accept-encoding": "gzip, deflate, br",152 "connection": "keep-alive",153 "host": "example.org:80",154 "user-agent": f"python-httpx/{__version__}",155 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",156 }157 }