Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

149 lines · 4.5 KiB · Python

Last changed by

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

History of this file

Take it

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 10–11why, one step back
10
11

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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,

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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:

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 138–149why, one step back
138
139 assert response.status_code == 200140 assert response.json() == {141 "headers": {142 "accept": "*/*",143 "accept-encoding": "gzip, deflate, br",144 "connection": "keep-alive",145 "host": "example.org:80",146 "user-agent": f"python-httpx/{__version__}",147 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",148 }149 }