Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

152 lines · 4.4 KiB · Python

Last changed by

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

History of this file

Take it

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 1–3why, one step back
1#!/usr/bin/env python3
23import json

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

line 4why, one step back
4

Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b

lines 5–6why, one step back
5import pytest
6

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

line 7why, one step back
7from httpx import (

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

line 8why, one step back
8 AsyncDispatcher,

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 9–11why, one step back
9 AsyncRequest,
10 AsyncResponse,11 CertTypes,

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

line 12why, one step back
12 Client,

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

line 13why, one step back
13 TimeoutTypes,

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

lines 14–15why, one step back
14 VerifyTypes,
15 __version__,

Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b

line 16why, one step back
16 models,

Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47

lines 17–127why, one step back
17)
181920class MockDispatch(AsyncDispatcher):21 async def send(22 self,23 request: AsyncRequest,24 verify: VerifyTypes = None,25 cert: CertTypes = None,26 timeout: TimeoutTypes = None,27 ) -> AsyncResponse:28 if request.url.path.startswith("/echo_headers"):29 request_headers = dict(request.headers.items())30 body = json.dumps({"headers": request_headers}).encode()31 return AsyncResponse(200, content=body, request=request)323334def test_client_header():35 """36 Set a header in the Client.37 """38 url = "http://example.org/echo_headers"39 headers = {"Example-Header": "example-value"}4041 with Client(dispatch=MockDispatch(), headers=headers) as client:42 response = client.get(url)4344 assert response.status_code == 20045 assert response.json() == {46 "headers": {47 "accept": "*/*",48 "accept-encoding": "gzip, deflate, br",49 "connection": "keep-alive",50 "example-header": "example-value",51 "host": "example.org",52 "user-agent": f"python-httpx/{__version__}",53 }54 }555657def test_header_merge():58 url = "http://example.org/echo_headers"59 client_headers = {"User-Agent": "python-myclient/0.2.1"}60 request_headers = {"X-Auth-Token": "FooBarBazToken"}61 with Client(dispatch=MockDispatch(), headers=client_headers) as client:62 response = client.get(url, headers=request_headers)6364 assert response.status_code == 20065 assert response.json() == {66 "headers": {67 "accept": "*/*",68 "accept-encoding": "gzip, deflate, br",69 "connection": "keep-alive",70 "host": "example.org",71 "user-agent": "python-myclient/0.2.1",72 "x-auth-token": "FooBarBazToken",73 }74 }757677def test_header_merge_conflicting_headers():78 url = "http://example.org/echo_headers"79 client_headers = {"X-Auth-Token": "FooBar"}80 request_headers = {"X-Auth-Token": "BazToken"}81 with Client(dispatch=MockDispatch(), headers=client_headers) as client:82 response = client.get(url, headers=request_headers)8384 assert response.status_code == 20085 assert response.json() == {86 "headers": {87 "accept": "*/*",88 "accept-encoding": "gzip, deflate, br",89 "connection": "keep-alive",90 "host": "example.org",91 "user-agent": f"python-httpx/{__version__}",92 "x-auth-token": "BazToken",93 }94 }959697def test_header_update():98 url = "http://example.org/echo_headers"99 with Client(dispatch=MockDispatch()) as client:100 first_response = client.get(url)101 client.headers.update(102 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}103 )104 second_response = client.get(url)105106 assert first_response.status_code == 200107 assert first_response.json() == {108 "headers": {109 "accept": "*/*",110 "accept-encoding": "gzip, deflate, br",111 "connection": "keep-alive",112 "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",124 "host": "example.org",125 "user-agent": "python-myclient/0.2.1",126 }127 }

Raise KeyError if header isn't found in Headers (#324)Gaurav Dhameejabefore adoption0f34f3b

lines 128–133why, one step back
128
129130def test_header_does_not_exist():131 headers = models.Headers({"foo": "bar"})132 with pytest.raises(KeyError):133 del headers["baz"]

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

lines 134–152why, one step back
134
135136def test_host_without_auth_in_header():137 url = "http://username:password@example.org:80/echo_headers"138139 with Client(dispatch=MockDispatch()) as client:140 response = client.get(url)141142 assert response.status_code == 200143 assert response.json() == {144 "headers": {145 "accept": "*/*",146 "accept-encoding": "gzip, deflate, br",147 "connection": "keep-alive",148 "host": "example.org:80",149 "user-agent": f"python-httpx/{__version__}",150 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",151 }152 }