Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

185 lines · 5.6 KiB · Python

Last changed by

Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce

History of this file

Take it

The walk stopped after 200 commits: lines older than 1ce3cc3 point at it.

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 1–6why, one step back
1#!/usr/bin/env python3
23import json45import pytest6

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 7why, one step back
7from httpx import AsyncClient, Headers, Request, Response, __version__

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 8–9why, one step back
8from httpx._config import CertTypes, TimeoutTypes, VerifyTypes
9from httpx._dispatch.base import AsyncDispatcher

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 10–11why, one step back
10
11

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

line 12why, one step back
12class MockDispatch(AsyncDispatcher):

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 13–14why, one step back
13 async def send(
14 self,

Drop sync (#544)Tom Christiebefore adoption206c537

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

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 16–18why, one step back
16 verify: VerifyTypes = None,
17 cert: CertTypes = None,18 timeout: TimeoutTypes = None,

Drop sync (#544)Tom Christiebefore adoption206c537

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

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 24–25why, one step back
24
25

Drop sync (#544)Tom Christiebefore adoption206c537

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

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 28–33why, one step back
28 """
29 Set a header in the Client.30 """31 url = "http://example.org/echo_headers"32 headers = {"Example-Header": "example-value"}33

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 34why, one step back
34 client = AsyncClient(dispatch=MockDispatch(), headers=headers)

Drop sync (#544)Tom Christiebefore adoption206c537

line 35why, one step back
35 response = await client.get(url)

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 36–42why, one step back
36
37 assert response.status_code == 20038 assert response.json() == {39 "headers": {40 "accept": "*/*",41 "accept-encoding": "gzip, deflate, br",42 "connection": "keep-alive",

Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce

line 43why, one step back
43 "content-length": "0",

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 44–50why, one step back
44 "example-header": "example-value",
45 "host": "example.org",46 "user-agent": f"python-httpx/{__version__}",47 }48 }4950

Drop sync (#544)Tom Christiebefore adoption206c537

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

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 56why, one step back
56 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)

Drop sync (#544)Tom Christiebefore adoption206c537

line 57why, one step back
57 response = await client.get(url, headers=request_headers)

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 58–64why, one step back
58
59 assert response.status_code == 20060 assert response.json() == {61 "headers": {62 "accept": "*/*",63 "accept-encoding": "gzip, deflate, br",64 "connection": "keep-alive",

Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce

line 65why, one step back
65 "content-length": "0",

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 66–72why, one step back
66 "host": "example.org",
67 "user-agent": "python-myclient/0.2.1",68 "x-auth-token": "FooBarBazToken",69 }70 }7172

Drop sync (#544)Tom Christiebefore adoption206c537

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

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 78why, one step back
78 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)

Drop sync (#544)Tom Christiebefore adoption206c537

line 79why, one step back
79 response = await client.get(url, headers=request_headers)

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 80–86why, one step back
80
81 assert response.status_code == 20082 assert response.json() == {83 "headers": {84 "accept": "*/*",85 "accept-encoding": "gzip, deflate, br",86 "connection": "keep-alive",

Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce

line 87why, one step back
87 "content-length": "0",

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 88–94why, one step back
88 "host": "example.org",
89 "user-agent": f"python-httpx/{__version__}",90 "x-auth-token": "BazToken",91 }92 }9394

Drop sync (#544)Tom Christiebefore adoption206c537

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

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 98why, one step back
98 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

lines 99–103why, one step back
99 first_response = await client.get(url)
100 client.headers.update(101 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}102 )103 second_response = await client.get(url)

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 104–110why, one step back
104
105 assert first_response.status_code == 200106 assert first_response.json() == {107 "headers": {108 "accept": "*/*",109 "accept-encoding": "gzip, deflate, br",110 "connection": "keep-alive",

Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce

line 111why, one step back
111 "content-length": "0",

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 112–123why, one step back
112 "host": "example.org",
113 "user-agent": f"python-httpx/{__version__}",114 }115 }116117 assert second_response.status_code == 200118 assert second_response.json() == {119 "headers": {120 "accept": "*/*",121 "accept-encoding": "gzip, deflate, br",122 "another-header": "AThing",123 "connection": "keep-alive",

Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce

line 124why, one step back
124 "content-length": "0",

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 125–131why, one step back
125 "host": "example.org",
126 "user-agent": "python-myclient/0.2.1",127 }128 }129130131def test_header_does_not_exist():

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 132why, one step back
132 headers = Headers({"foo": "bar"})

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 133–136why, one step back
133 with pytest.raises(KeyError):
134 del headers["baz"]135136

Drop sync (#544)Tom Christiebefore adoption206c537

line 137why, one step back
137@pytest.mark.asyncio

Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d

lines 138–143why, one step back
138async def 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 """

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 146why, one step back
146 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 147why, one step back
147 response = await client.get(url)

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 148–154why, one step back
148
149 assert response.status_code == 200150 assert response.json() == {151 "headers": {152 "accept": "*/*",153 "accept-encoding": "gzip, deflate, br",154 "connection": "keep-alive",

Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce

line 155why, one step back
155 "content-length": "0",

Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d

lines 156–170why, one step back
156 "host": "example.org",
157 "user-agent": f"python-httpx/{__version__}",158 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",159 }160 }161162163@pytest.mark.asyncio164async def test_host_with_non_default_port_in_url():165 """166 If the URL includes a non-default port, then it should be included in167 the Host header.168 """169 url = "http://username:password@example.org:123/echo_headers"170

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 171why, one step back
171 client = AsyncClient(dispatch=MockDispatch())

Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d

lines 172–179why, one step back
172 response = await client.get(url)
173174 assert response.status_code == 200175 assert response.json() == {176 "headers": {177 "accept": "*/*",178 "accept-encoding": "gzip, deflate, br",179 "connection": "keep-alive",

Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce

line 180why, one step back
180 "content-length": "0",

Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d

line 181why, one step back
181 "host": "example.org:123",

First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3

lines 182–185why, one step back
182 "user-agent": f"python-httpx/{__version__}",
183 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",184 }185 }