Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

183 lines · 5.6 KiB · Python

Last changed by

Type check tests (#1054)Florimond Mancabefore adoption0296c2b

History of this file

Take it

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

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

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

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

line 4why, one step back
4

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 5why, one step back
5import httpcore

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

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

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 10–11why, one step back
10
11

Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100

line 12why, one step back
12class MockTransport(httpcore.AsyncHTTPTransport):

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 13why, one step back
13 async def request(

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

line 14why, one step back
14 self,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 15why, one step back
15 method: bytes,

Type check tests (#1054)Florimond Mancabefore adoption0296c2b

lines 16–18why, one step back
16 url: typing.Tuple[bytes, bytes, typing.Optional[int], bytes],
17 headers: typing.List[typing.Tuple[bytes, bytes]] = None,18 stream: httpcore.AsyncByteStream = None,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 19–22why, one step back
19 timeout: typing.Dict[str, typing.Optional[float]] = None,
20 ) -> typing.Tuple[21 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream22 ]:

Type check tests (#1054)Florimond Mancabefore adoption0296c2b

line 23why, one step back
23 assert headers is not None

Improve list/dict comprehensions (#1036)Taneli Hukkinenbefore adoption6cf9039

lines 24–26why, one step back
24 headers_dict = {
25 key.decode("ascii"): value.decode("ascii") for key, value in headers26 }

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 27–28why, one step back
27 body = JSONStream({"headers": headers_dict})
28 return b"HTTP/1.1", 200, b"OK", [], body

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 29–38why, one step back
29
3031@pytest.mark.asyncio32async def test_client_header():33 """34 Set a header in the Client.35 """36 url = "http://example.org/echo_headers"37 headers = {"Example-Header": "example-value"}38

Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100

line 39why, one step back
39 client = AsyncClient(transport=MockTransport(), headers=headers)

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 40–59why, one step back
40 response = await client.get(url)
4142 assert response.status_code == 20043 assert response.json() == {44 "headers": {45 "accept": "*/*",46 "accept-encoding": "gzip, deflate, br",47 "connection": "keep-alive",48 "example-header": "example-value",49 "host": "example.org",50 "user-agent": f"python-httpx/{__version__}",51 }52 }535455@pytest.mark.asyncio56async def test_header_merge():57 url = "http://example.org/echo_headers"58 client_headers = {"User-Agent": "python-myclient/0.2.1"}59 request_headers = {"X-Auth-Token": "FooBarBazToken"}

Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100

line 60why, one step back
60 client = AsyncClient(transport=MockTransport(), headers=client_headers)

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

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

Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100

line 81why, one step back
81 client = AsyncClient(transport=MockTransport(), headers=client_headers)

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

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

Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100

line 100why, one step back
100 client = AsyncClient(transport=MockTransport())

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 101–145why, one step back
101 first_response = await client.get(url)
102 client.headers.update(103 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}104 )105 second_response = await client.get(url)106107 assert first_response.status_code == 200108 assert first_response.json() == {109 "headers": {110 "accept": "*/*",111 "accept-encoding": "gzip, deflate, br",112 "connection": "keep-alive",113 "host": "example.org",114 "user-agent": f"python-httpx/{__version__}",115 }116 }117118 assert second_response.status_code == 200119 assert second_response.json() == {120 "headers": {121 "accept": "*/*",122 "accept-encoding": "gzip, deflate, br",123 "another-header": "AThing",124 "connection": "keep-alive",125 "host": "example.org",126 "user-agent": "python-myclient/0.2.1",127 }128 }129130131def test_header_does_not_exist():132 headers = Headers({"foo": "bar"})133 with pytest.raises(KeyError):134 del headers["baz"]135136137@pytest.mark.asyncio138async 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 """144 url = "http://username:password@example.org:80/echo_headers"145

Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100

line 146why, one step back
146 client = AsyncClient(transport=MockTransport())

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 147–169why, one step back
147 response = await 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/{__version__}",157 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",158 }159 }160161162@pytest.mark.asyncio163async def test_host_with_non_default_port_in_url():164 """165 If the URL includes a non-default port, then it should be included in166 the Host header.167 """168 url = "http://username:password@example.org:123/echo_headers"169

Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100

line 170why, one step back
170 client = AsyncClient(transport=MockTransport())

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 171–183why, one step back
171 response = await client.get(url)
172173 assert response.status_code == 200174 assert response.json() == {175 "headers": {176 "accept": "*/*",177 "accept-encoding": "gzip, deflate, br",178 "connection": "keep-alive",179 "host": "example.org:123",180 "user-agent": f"python-httpx/{__version__}",181 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",182 }183 }