Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

182 lines · 5.5 KiB · Python

Last changed by

Httpcore interface (#804)Tom Christiebefore adoption3046e92

History of this file

Take it

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 1–2why, one step back
1#!/usr/bin/env python3
2

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 3why, one step back
3import typing

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 4why, one step back
4

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 5why, one step back
5import httpcore

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 8–9why, one step back
8from httpx import AsyncClient, Headers, __version__
9from httpx._content_streams import ContentStream, JSONStream

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 10–11why, one step back
10
11

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 12–13why, one step back
12class MockDispatch(httpcore.AsyncHTTPTransport):
13 async def request(

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 14why, one step back
14 self,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 15–27why, one step back
15 method: bytes,
16 url: typing.Tuple[bytes, bytes, int, bytes],17 headers: typing.List[typing.Tuple[bytes, bytes]],18 stream: ContentStream,19 timeout: typing.Dict[str, typing.Optional[float]] = None,20 ) -> typing.Tuple[21 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream22 ]:23 headers_dict = dict(24 [(key.decode("ascii"), value.decode("ascii")) for key, value in headers]25 )26 body = JSONStream({"headers": headers_dict})27 return b"HTTP/1.1", 200, b"OK", [], body

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 28–37why, one step back
28
2930@pytest.mark.asyncio31async def 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"}37

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

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 39–58why, one step back
39 response = await 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 }525354@pytest.mark.asyncio55async def test_header_merge():56 url = "http://example.org/echo_headers"57 client_headers = {"User-Agent": "python-myclient/0.2.1"}58 request_headers = {"X-Auth-Token": "FooBarBazToken"}

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

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 60–79why, one step back
60 response = await client.get(url, headers=request_headers)
6162 assert response.status_code == 20063 assert response.json() == {64 "headers": {65 "accept": "*/*",66 "accept-encoding": "gzip, deflate, br",67 "connection": "keep-alive",68 "host": "example.org",69 "user-agent": "python-myclient/0.2.1",70 "x-auth-token": "FooBarBazToken",71 }72 }737475@pytest.mark.asyncio76async def test_header_merge_conflicting_headers():77 url = "http://example.org/echo_headers"78 client_headers = {"X-Auth-Token": "FooBar"}79 request_headers = {"X-Auth-Token": "BazToken"}

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

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 81–98why, one step back
81 response = await client.get(url, headers=request_headers)
8283 assert response.status_code == 20084 assert response.json() == {85 "headers": {86 "accept": "*/*",87 "accept-encoding": "gzip, deflate, br",88 "connection": "keep-alive",89 "host": "example.org",90 "user-agent": f"python-httpx/{__version__}",91 "x-auth-token": "BazToken",92 }93 }949596@pytest.mark.asyncio97async def test_header_update():98 url = "http://example.org/echo_headers"

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

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 100–130why, one step back
100 first_response = await client.get(url)
101 client.headers.update(102 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}103 )104 second_response = await client.get(url)105106 assert first_response.status_code == 200107 assert first_response.json() == {108 "headers": {109 "accept": "*/*",110 "accept-encoding": "gzip, deflate, br",111 "connection": "keep-alive",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",124 "host": "example.org",125 "user-agent": "python-myclient/0.2.1",126 }127 }128129130def test_header_does_not_exist():

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

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 132–136why, one step back
132 with pytest.raises(KeyError):
133 del headers["baz"]134135136@pytest.mark.asyncio

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

lines 137–142why, one step back
137async def test_host_with_auth_and_port_in_url():
138 """139 The Host header should only include the hostname, or hostname:port140 (for non-default ports only). Any userinfo or default port should not141 be present.142 """

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

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

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 146–153why, one step back
146 response = await client.get(url)
147148 assert response.status_code == 200149 assert response.json() == {150 "headers": {151 "accept": "*/*",152 "accept-encoding": "gzip, deflate, br",153 "connection": "keep-alive",

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

lines 154–168why, one step back
154 "host": "example.org",
155 "user-agent": f"python-httpx/{__version__}",156 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",157 }158 }159160161@pytest.mark.asyncio162async def 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"168

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

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

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

lines 170–178why, one step back
170 response = await 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",

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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