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 fileTake 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 back1#!/usr/bin/env python323import json4Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b
lines 5–6why, one step back5import pytest6Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
lines 7–9why, one step back7from httpx import Client, Headers, Request, Response, __version__8from httpx.config import CertTypes, TimeoutTypes, VerifyTypes9from httpx.dispatch.base import DispatcherAdded links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 10–11why, one step back1011Drop sync (#544)Tom Christiebefore adoption206c537
line 12why, one step back12class MockDispatch(Dispatcher):Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 13–14why, one step back13 async def send(14 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 15why, one step back15 request: Request,Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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:Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 24–25why, one step back2425Drop sync (#544)Tom Christiebefore adoption206c537
lines 26–27why, one step back26@pytest.mark.asyncio27async def test_client_header():Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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"}33Drop sync (#544)Tom Christiebefore adoption206c537
lines 34–35why, one step back34 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 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():Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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"}Drop sync (#544)Tom Christiebefore adoption206c537
lines 55–56why, one step back55 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 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():Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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"}Drop sync (#544)Tom Christiebefore adoption206c537
lines 76–77why, one step back76 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 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():Added links property to BaseResponse (#211)euri10before adoptioncad9c03
line 94why, one step back94 url = "http://example.org/echo_headers"Drop sync (#544)Tom Christiebefore adoption206c537
lines 95–100why, one step back95 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 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 }Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b
lines 124–126why, one step back124125126def 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"})Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b
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
lines 132–133why, one step back132@pytest.mark.asyncio133async 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 back134 url = "http://username:password@example.org:80/echo_headers"135Drop sync (#544)Tom Christiebefore adoption206c537
lines 136–137why, one step back136 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 back138139 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 }