test_headers.py
This file
Path
tests/client/test_headers.py
178 lines · 5.4 KiB · Python
Last changed by
Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b
History of this fileTake it
The walk stopped after 200 commits: lines older than 5df822c point at it.
Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 1–6why, one step back1#!/usr/bin/env python323import json45import pytest6Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 7why, one step back7from httpx import AsyncClient, Headers, Request, Response, __version__Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 8why, one step back8from httpx.config import CertTypes, TimeoutTypes, VerifyTypesDispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b
line 9why, one step back9from httpx.dispatch.base import AsyncDispatcherFix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 10–11why, one step back1011Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b
line 12why, one step back12class MockDispatch(AsyncDispatcher):Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 13–14why, one step back13 async def send(14 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 15why, one step back15 request: Request,Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 16–18why, one step back16 verify: VerifyTypes = None,17 cert: CertTypes = None,18 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 19why, one step back19 ) -> Response:Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 20–22why, one step back20 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 back23 return Response(200, content=body, request=request)Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 24–25why, one step back2425Drop sync (#544)Tom Christiebefore adoption206c537
lines 26–27why, one step back26@pytest.mark.asyncio27async def test_client_header():Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 28–33why, one step back28 """29 Set a header in the Client.30 """31 url = "http://example.org/echo_headers"32 headers = {"Example-Header": "example-value"}33Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 34why, one step back34 client = AsyncClient(dispatch=MockDispatch(), headers=headers)Drop sync (#544)Tom Christiebefore adoption206c537
line 35why, one step back35 response = await client.get(url)Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 36–49why, one step back3637 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 }4849Drop sync (#544)Tom Christiebefore adoption206c537
lines 50–51why, one step back50@pytest.mark.asyncio51async def test_header_merge():Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 52–54why, one step back52 url = "http://example.org/echo_headers"53 client_headers = {"User-Agent": "python-myclient/0.2.1"}54 request_headers = {"X-Auth-Token": "FooBarBazToken"}Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 55why, one step back55 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)Drop sync (#544)Tom Christiebefore adoption206c537
line 56why, one step back56 response = await client.get(url, headers=request_headers)Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 57–70why, one step back5758 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 }6970Drop sync (#544)Tom Christiebefore adoption206c537
lines 71–72why, one step back71@pytest.mark.asyncio72async def test_header_merge_conflicting_headers():Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 73–75why, one step back73 url = "http://example.org/echo_headers"74 client_headers = {"X-Auth-Token": "FooBar"}75 request_headers = {"X-Auth-Token": "BazToken"}Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 76why, one step back76 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)Drop sync (#544)Tom Christiebefore adoption206c537
line 77why, one step back77 response = await client.get(url, headers=request_headers)Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 78–91why, one step back7879 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 }9091Drop sync (#544)Tom Christiebefore adoption206c537
lines 92–93why, one step back92@pytest.mark.asyncio93async def test_header_update():Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
line 94why, one step back94 url = "http://example.org/echo_headers"Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 95why, one step back95 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
lines 96–100why, one step back96 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)Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 101–126why, one step back101102 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 }124125126def test_header_does_not_exist():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 127why, one step back127 headers = Headers({"foo": "bar"})Fix broken docs (#415)Dustin Ingrambefore adoption5df822c
lines 128–129why, one step back128 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 back130131Drop sync (#544)Tom Christiebefore adoption206c537
line 132why, one step back132@pytest.mark.asyncioFix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d
lines 133–138why, one step back133async 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 back139 url = "http://username:password@example.org:80/echo_headers"140Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 141why, one step back141 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 142why, one step back142 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 back143144 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–164why, one step back150 "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"164Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 165why, one step back165 client = AsyncClient(dispatch=MockDispatch())Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d
lines 166–174why, one step back166 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 back175 "user-agent": f"python-httpx/{__version__}",176 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",177 }178 }