test_headers.py
This file
Path
tests/client/test_headers.py
157 lines · 4.4 KiB · Python
Last changed by
Drop sync (#544)Tom Christiebefore adoption206c537
History of this fileTake it
The walk stopped after 200 commits: lines older than c5248e6 point at it.
Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 1–4why, one step back1#!/usr/bin/env python323import json4Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b
lines 5–6why, one step back5import pytest6Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 7–9why, one step back7from httpx import (8 CertTypes,9 Client,Drop sync (#544)Tom Christiebefore adoption206c537
lines 10–12why, one step back10 Dispatcher,11 Request,12 Response,Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 13–15why, one step back13 TimeoutTypes,14 VerifyTypes,15 __version__,Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b
line 16why, one step back16 models,Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 17–19why, one step back17)1819Drop sync (#544)Tom Christiebefore adoption206c537
line 20why, one step back20class MockDispatch(Dispatcher):Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 21–22why, one step back21 async def send(22 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 23why, one step back23 request: Request,Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 24–26why, one step back24 verify: VerifyTypes = None,25 cert: CertTypes = None,26 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 27why, one step back27 ) -> Response:Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 28–30why, one step back28 if request.url.path.startswith("/echo_headers"):29 request_headers = dict(request.headers.items())30 body = json.dumps({"headers": request_headers}).encode()Drop sync (#544)Tom Christiebefore adoption206c537
line 31why, one step back31 return Response(200, content=body, request=request)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 32–33why, one step back3233Drop sync (#544)Tom Christiebefore adoption206c537
lines 34–35why, one step back34@pytest.mark.asyncio35async def test_client_header():Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 36–41why, one step back36 """37 Set a header in the Client.38 """39 url = "http://example.org/echo_headers"40 headers = {"Example-Header": "example-value"}41Drop sync (#544)Tom Christiebefore adoption206c537
lines 42–43why, one step back42 client = Client(dispatch=MockDispatch(), headers=headers)43 response = await client.get(url)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 44–57why, one step back4445 assert response.status_code == 20046 assert response.json() == {47 "headers": {48 "accept": "*/*",49 "accept-encoding": "gzip, deflate, br",50 "connection": "keep-alive",51 "example-header": "example-value",52 "host": "example.org",53 "user-agent": f"python-httpx/{__version__}",54 }55 }5657Drop sync (#544)Tom Christiebefore adoption206c537
lines 58–59why, one step back58@pytest.mark.asyncio59async def test_header_merge():Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 60–62why, one step back60 url = "http://example.org/echo_headers"61 client_headers = {"User-Agent": "python-myclient/0.2.1"}62 request_headers = {"X-Auth-Token": "FooBarBazToken"}Drop sync (#544)Tom Christiebefore adoption206c537
lines 63–64why, one step back63 client = Client(dispatch=MockDispatch(), headers=client_headers)64 response = await client.get(url, headers=request_headers)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 65–78why, one step back6566 assert response.status_code == 20067 assert response.json() == {68 "headers": {69 "accept": "*/*",70 "accept-encoding": "gzip, deflate, br",71 "connection": "keep-alive",72 "host": "example.org",73 "user-agent": "python-myclient/0.2.1",74 "x-auth-token": "FooBarBazToken",75 }76 }7778Drop sync (#544)Tom Christiebefore adoption206c537
lines 79–80why, one step back79@pytest.mark.asyncio80async def test_header_merge_conflicting_headers():Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 81–83why, one step back81 url = "http://example.org/echo_headers"82 client_headers = {"X-Auth-Token": "FooBar"}83 request_headers = {"X-Auth-Token": "BazToken"}Drop sync (#544)Tom Christiebefore adoption206c537
lines 84–85why, one step back84 client = Client(dispatch=MockDispatch(), headers=client_headers)85 response = await client.get(url, headers=request_headers)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 86–99why, one step back8687 assert response.status_code == 20088 assert response.json() == {89 "headers": {90 "accept": "*/*",91 "accept-encoding": "gzip, deflate, br",92 "connection": "keep-alive",93 "host": "example.org",94 "user-agent": f"python-httpx/{__version__}",95 "x-auth-token": "BazToken",96 }97 }9899Drop sync (#544)Tom Christiebefore adoption206c537
lines 100–101why, one step back100@pytest.mark.asyncio101async def test_header_update():Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 102why, one step back102 url = "http://example.org/echo_headers"Drop sync (#544)Tom Christiebefore adoption206c537
lines 103–108why, one step back103 client = Client(dispatch=MockDispatch())104 first_response = await client.get(url)105 client.headers.update(106 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}107 )108 second_response = await client.get(url)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 109–131why, one step back109110 assert first_response.status_code == 200111 assert first_response.json() == {112 "headers": {113 "accept": "*/*",114 "accept-encoding": "gzip, deflate, br",115 "connection": "keep-alive",116 "host": "example.org",117 "user-agent": f"python-httpx/{__version__}",118 }119 }120121 assert second_response.status_code == 200122 assert second_response.json() == {123 "headers": {124 "accept": "*/*",125 "accept-encoding": "gzip, deflate, br",126 "another-header": "AThing",127 "connection": "keep-alive",128 "host": "example.org",129 "user-agent": "python-myclient/0.2.1",130 }131 }Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b
lines 132–137why, one step back132133134def test_header_does_not_exist():135 headers = models.Headers({"foo": "bar"})136 with pytest.raises(KeyError):137 del headers["baz"]Don't include username/password components in `Host` header (#417)Can Sarıgölbefore adoptiondd3fbcc
lines 138–139why, one step back138139Drop sync (#544)Tom Christiebefore adoption206c537
lines 140–141why, one step back140@pytest.mark.asyncio141async def test_host_without_auth_in_header():Don't include username/password components in `Host` header (#417)Can Sarıgölbefore adoptiondd3fbcc
lines 142–143why, one step back142 url = "http://username:password@example.org:80/echo_headers"143Drop sync (#544)Tom Christiebefore adoption206c537
lines 144–145why, one step back144 client = Client(dispatch=MockDispatch())145 response = await client.get(url)Don't include username/password components in `Host` header (#417)Can Sarıgölbefore adoptiondd3fbcc
lines 146–157why, one step back146147 assert response.status_code == 200148 assert response.json() == {149 "headers": {150 "accept": "*/*",151 "accept-encoding": "gzip, deflate, br",152 "connection": "keep-alive",153 "host": "example.org:80",154 "user-agent": f"python-httpx/{__version__}",155 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",156 }157 }