test_headers.py
This file
Path
tests/client/test_headers.py
185 lines · 5.6 KiB · Python
Last changed by
Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce
History of this fileTake it
The walk stopped after 200 commits: lines older than 1ce3cc3 point at it.
First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
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__Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3
lines 8–9why, one step back8from httpx._config import CertTypes, TimeoutTypes, VerifyTypes9from httpx._dispatch.base import AsyncDispatcherFirst pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 10–11why, one step back1011Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b
line 12why, one step back12class MockDispatch(AsyncDispatcher):First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 13–14why, one step back13 async def send(14 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 15why, one step back15 request: Request,First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
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:First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
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)First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 24–25why, one step back2425Drop sync (#544)Tom Christiebefore adoption206c537
lines 26–27why, one step back26@pytest.mark.asyncio27async def test_client_header():First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
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)First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 36–42why, 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",Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce
line 43why, one step back43 "content-length": "0",First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 44–50why, one step back44 "example-header": "example-value",45 "host": "example.org",46 "user-agent": f"python-httpx/{__version__}",47 }48 }4950Drop sync (#544)Tom Christiebefore adoption206c537
lines 51–52why, one step back51@pytest.mark.asyncio52async def test_header_merge():First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 53–55why, one step back53 url = "http://example.org/echo_headers"54 client_headers = {"User-Agent": "python-myclient/0.2.1"}55 request_headers = {"X-Auth-Token": "FooBarBazToken"}Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 56why, one step back56 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)Drop sync (#544)Tom Christiebefore adoption206c537
line 57why, one step back57 response = await client.get(url, headers=request_headers)First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 58–64why, one step back5859 assert response.status_code == 20060 assert response.json() == {61 "headers": {62 "accept": "*/*",63 "accept-encoding": "gzip, deflate, br",64 "connection": "keep-alive",Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce
line 65why, one step back65 "content-length": "0",First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 66–72why, one step back66 "host": "example.org",67 "user-agent": "python-myclient/0.2.1",68 "x-auth-token": "FooBarBazToken",69 }70 }7172Drop sync (#544)Tom Christiebefore adoption206c537
lines 73–74why, one step back73@pytest.mark.asyncio74async def test_header_merge_conflicting_headers():First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 75–77why, one step back75 url = "http://example.org/echo_headers"76 client_headers = {"X-Auth-Token": "FooBar"}77 request_headers = {"X-Auth-Token": "BazToken"}Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 78why, one step back78 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)Drop sync (#544)Tom Christiebefore adoption206c537
line 79why, one step back79 response = await client.get(url, headers=request_headers)First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 80–86why, one step back8081 assert response.status_code == 20082 assert response.json() == {83 "headers": {84 "accept": "*/*",85 "accept-encoding": "gzip, deflate, br",86 "connection": "keep-alive",Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce
line 87why, one step back87 "content-length": "0",First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 88–94why, one step back88 "host": "example.org",89 "user-agent": f"python-httpx/{__version__}",90 "x-auth-token": "BazToken",91 }92 }9394Drop sync (#544)Tom Christiebefore adoption206c537
lines 95–96why, one step back95@pytest.mark.asyncio96async def test_header_update():First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
line 97why, one step back97 url = "http://example.org/echo_headers"Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 98why, one step back98 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
lines 99–103why, one step back99 first_response = await client.get(url)100 client.headers.update(101 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}102 )103 second_response = await client.get(url)First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 104–110why, one step back104105 assert first_response.status_code == 200106 assert first_response.json() == {107 "headers": {108 "accept": "*/*",109 "accept-encoding": "gzip, deflate, br",110 "connection": "keep-alive",Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce
line 111why, one step back111 "content-length": "0",First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 112–123why, one step back112 "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",Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce
line 124why, one step back124 "content-length": "0",First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 125–131why, one step back125 "host": "example.org",126 "user-agent": "python-myclient/0.2.1",127 }128 }129130131def test_header_does_not_exist():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 132why, one step back132 headers = Headers({"foo": "bar"})First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 133–136why, one step back133 with pytest.raises(KeyError):134 del headers["baz"]135136Drop sync (#544)Tom Christiebefore adoption206c537
line 137why, one step back137@pytest.mark.asyncioFix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d
lines 138–143why, one step back138async 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 """First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 144–145why, one step back144 url = "http://username:password@example.org:80/echo_headers"145Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 146why, one step back146 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 147why, one step back147 response = await client.get(url)First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 148–154why, one step back148149 assert response.status_code == 200150 assert response.json() == {151 "headers": {152 "accept": "*/*",153 "accept-encoding": "gzip, deflate, br",154 "connection": "keep-alive",Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce
line 155why, one step back155 "content-length": "0",Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d
lines 156–170why, one step back156 "host": "example.org",157 "user-agent": f"python-httpx/{__version__}",158 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",159 }160 }161162163@pytest.mark.asyncio164async def test_host_with_non_default_port_in_url():165 """166 If the URL includes a non-default port, then it should be included in167 the Host header.168 """169 url = "http://username:password@example.org:123/echo_headers"170Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 171why, one step back171 client = AsyncClient(dispatch=MockDispatch())Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d
lines 172–179why, one step back172 response = await client.get(url)173174 assert response.status_code == 200175 assert response.json() == {176 "headers": {177 "accept": "*/*",178 "accept-encoding": "gzip, deflate, br",179 "connection": "keep-alive",Add content-length header for empty bytestream (#866)Dima Bogerbefore adoption939f3ce
line 180why, one step back180 "content-length": "0",Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d
line 181why, one step back181 "host": "example.org:123",First pass at autodoc support (#464)Tom Christiebefore adoption1ce3cc3
lines 182–185why, one step back182 "user-agent": f"python-httpx/{__version__}",183 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",184 }185 }