Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

178 lines · 5.3 KiB · Python

Last changed by

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

History of this file

Take it

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

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

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

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

lines 7–9why, one step back
7from httpx import Client, Headers, Request, Response, __version__
8from httpx.config import CertTypes, TimeoutTypes, VerifyTypes9from httpx.dispatch.base import Dispatcher

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 10–11why, one step back
10
11

Drop sync (#544)Tom Christiebefore adoption206c537

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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,

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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:

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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)

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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():

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 36–49why, 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",43 "example-header": "example-value",44 "host": "example.org",45 "user-agent": f"python-httpx/{__version__}",46 }47 }4849

Drop sync (#544)Tom Christiebefore adoption206c537

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 57–70why, one step back
57
58 assert response.status_code == 20059 assert response.json() == {60 "headers": {61 "accept": "*/*",62 "accept-encoding": "gzip, deflate, br",63 "connection": "keep-alive",64 "host": "example.org",65 "user-agent": "python-myclient/0.2.1",66 "x-auth-token": "FooBarBazToken",67 }68 }6970

Drop sync (#544)Tom Christiebefore adoption206c537

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 78–91why, one step back
78
79 assert response.status_code == 20080 assert response.json() == {81 "headers": {82 "accept": "*/*",83 "accept-encoding": "gzip, deflate, br",84 "connection": "keep-alive",85 "host": "example.org",86 "user-agent": f"python-httpx/{__version__}",87 "x-auth-token": "BazToken",88 }89 }9091

Drop sync (#544)Tom Christiebefore adoption206c537

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Drop sync (#544)Tom Christiebefore adoption206c537

lines 95–100why, one step back
95 client = Client(dispatch=MockDispatch())
96 first_response = await client.get(url)97 client.headers.update(98 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}99 )100 second_response = await client.get(url)

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 101–123why, one step back
101
102 assert first_response.status_code == 200103 assert first_response.json() == {104 "headers": {105 "accept": "*/*",106 "accept-encoding": "gzip, deflate, br",107 "connection": "keep-alive",108 "host": "example.org",109 "user-agent": f"python-httpx/{__version__}",110 }111 }112113 assert second_response.status_code == 200114 assert second_response.json() == {115 "headers": {116 "accept": "*/*",117 "accept-encoding": "gzip, deflate, br",118 "another-header": "AThing",119 "connection": "keep-alive",120 "host": "example.org",121 "user-agent": "python-myclient/0.2.1",122 }123 }

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

lines 124–126why, one step back
124
125126def test_header_does_not_exist():

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

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

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

lines 128–129why, one step back
128 with pytest.raises(KeyError):
129 del headers["baz"]

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

lines 130–131why, one step back
130
131

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 133–138why, one step back
133async def test_host_with_auth_and_port_in_url():
134 """135 The Host header should only include the hostname, or hostname:port136 (for non-default ports only). Any userinfo or default port should not137 be present.138 """

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 143–149why, one step back
143
144 assert response.status_code == 200145 assert response.json() == {146 "headers": {147 "accept": "*/*",148 "accept-encoding": "gzip, deflate, br",149 "connection": "keep-alive",

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

lines 150–174why, one step back
150 "host": "example.org",
151 "user-agent": f"python-httpx/{__version__}",152 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",153 }154 }155156157@pytest.mark.asyncio158async def test_host_with_non_default_port_in_url():159 """160 If the URL includes a non-default port, then it should be included in161 the Host header.162 """163 url = "http://username:password@example.org:123/echo_headers"164165 client = Client(dispatch=MockDispatch())166 response = await client.get(url)167168 assert response.status_code == 200169 assert response.json() == {170 "headers": {171 "accept": "*/*",172 "accept-encoding": "gzip, deflate, br",173 "connection": "keep-alive",174 "host": "example.org:123",

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

lines 175–178why, one step back
175 "user-agent": f"python-httpx/{__version__}",
176 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",177 }178 }