Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

193 lines · 5.5 KiB · Python

Last changed by

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

History of this file

Take it

The walk stopped after 200 commits: lines older than 869714f point at it.

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 1–7why, one step back
1from datetime import timedelta
23import pytest45import httpx67

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 8why, one step back
8def test_get(server):

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

line 9why, one step back
9 url = server.url

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 10–11why, one step back
10 with httpx.Client() as http:
11 response = http.get(url)

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 12–24why, one step back
12 assert response.status_code == 200
13 assert response.url == url14 assert response.content == b"Hello, world!"15 assert response.text == "Hello, world!"16 assert response.http_version == "HTTP/1.1"17 assert response.encoding == "iso-8859-1"18 assert response.request.url == url19 assert response.headers20 assert response.is_redirect is False21 assert repr(response) == "<Response [200 OK]>"22 assert response.elapsed > timedelta(0)2324

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

lines 25–33why, one step back
25@pytest.mark.parametrize(
26 "url",27 [28 pytest.param("invalid://example.org", id="scheme-not-http(s)"),29 pytest.param("://example.org", id="no-scheme"),30 pytest.param("http://", id="no-host"),31 ],32)33def test_get_invalid_url(server, url):

Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351

lines 34–35why, one step back
34 with httpx.Client() as client:
35 with pytest.raises(httpx.InvalidURL):

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

line 36why, one step back
36 client.get(url)

Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351

lines 37–38why, one step back
37
38

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 39why, one step back
39def test_build_request(server):

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 40–42why, one step back
40 url = server.url.copy_with(path="/echo_headers")
41 headers = {"Custom-header": "value"}42

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 43why, one step back
43 with httpx.Client() as client:

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 44–45why, one step back
44 request = client.build_request("GET", url)
45 request.headers.update(headers)

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 46why, one step back
46 response = client.send(request)

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 47–53why, one step back
47
48 assert response.status_code == 20049 assert response.url == url5051 assert response.json()["Custom-header"] == "value"5253

Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a

lines 54–69why, one step back
54def test_build_post_request(server):
55 url = server.url.copy_with(path="/echo_headers")56 headers = {"Custom-header": "value"}5758 with httpx.Client() as client:59 request = client.build_request("POST", url)60 request.headers.update(headers)61 response = client.send(request)6263 assert response.status_code == 20064 assert response.url == url6566 assert response.json()["Content-length"] == "0"67 assert response.json()["Custom-header"] == "value"6869

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 70–72why, one step back
70def test_post(server):
71 with httpx.Client() as client:72 response = client.post(server.url, data=b"Hello, world!")

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 73–76why, one step back
73 assert response.status_code == 200
74 assert response.reason_phrase == "OK"7576

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 77–79why, one step back
77def test_post_json(server):
78 with httpx.Client() as client:79 response = client.post(server.url, json={"text": "Hello, world!"})

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 80–83why, one step back
80 assert response.status_code == 200
81 assert response.reason_phrase == "OK"8283

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 84–87why, one step back
84def test_stream_response(server):
85 with httpx.Client() as client:86 with client.stream("GET", server.url) as response:87 content = response.read()

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 88–91why, one step back
88 assert response.status_code == 200
89 assert content == b"Hello, world!"9091

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 92why, one step back
92def test_stream_iterator(server):

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 93–94why, one step back
93 body = b""
94

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 95–97why, one step back
95 with httpx.Client() as client:
96 with client.stream("GET", server.url) as response:97 for chunk in response.iter_bytes():

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 98–103why, one step back
98 body += chunk
99100 assert response.status_code == 200101 assert body == b"Hello, world!"102103

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 104why, one step back
104def test_raw_iterator(server):

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 105–106why, one step back
105 body = b""
106

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 107–109why, one step back
107 with httpx.Client() as client:
108 with client.stream("GET", server.url) as response:109 for chunk in response.iter_raw():

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 110–115why, one step back
110 body += chunk
111112 assert response.status_code == 200113 assert body == b"Hello, world!"114115

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 116–117why, one step back
116def test_raise_for_status(server):
117 with httpx.Client() as client:

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

line 118why, one step back
118 for status_code in (200, 400, 404, 500, 505):

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 119why, one step back
119 response = client.request(

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

line 120why, one step back
120 "GET", server.url.copy_with(path=f"/status/{status_code}")

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 121–122why, one step back
121 )
122 if 400 <= status_code < 600:

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 123why, one step back
123 with pytest.raises(httpx.HTTPStatusError) as exc_info:

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 124–125why, one step back
124 response.raise_for_status()
125 assert exc_info.value.response == response

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

line 126why, one step back
126 assert exc_info.value.request.url.path == f"/status/{status_code}"

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

line 127why, one step back
127 else:

Fixed mypy errors in test_async_client.py and test_client.py (#985)Hasan Ramezanibefore adoption21d7e16

line 128why, one step back
128 assert response.raise_for_status() is None # type: ignore

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 129–130why, one step back
129
130

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 131–133why, one step back
131def test_options(server):
132 with httpx.Client() as client:133 response = client.options(server.url)

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 134–137why, one step back
134 assert response.status_code == 200
135 assert response.reason_phrase == "OK"136137

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 138–140why, one step back
138def test_head(server):
139 with httpx.Client() as client:140 response = client.head(server.url)

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 141–144why, one step back
141 assert response.status_code == 200
142 assert response.reason_phrase == "OK"143144

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 145–147why, one step back
145def test_put(server):
146 with httpx.Client() as client:147 response = client.put(server.url, data=b"Hello, world!")

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 148–151why, one step back
148 assert response.status_code == 200
149 assert response.reason_phrase == "OK"150151

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 152–154why, one step back
152def test_patch(server):
153 with httpx.Client() as client:154 response = client.patch(server.url, data=b"Hello, world!")

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 155–158why, one step back
155 assert response.status_code == 200
156 assert response.reason_phrase == "OK"157158

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 159–161why, one step back
159def test_delete(server):
160 with httpx.Client() as client:161 response = client.delete(server.url)

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 162–165why, one step back
162 assert response.status_code == 200
163 assert response.reason_phrase == "OK"164165

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 166why, one step back
166def test_base_url(server):

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

line 167why, one step back
167 base_url = server.url

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 168–169why, one step back
168 with httpx.Client(base_url=base_url) as client:
169 response = client.get("/")

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 170–174why, one step back
170 assert response.status_code == 200
171 assert response.url == base_url172173174def test_merge_url():

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 175why, one step back
175 client = httpx.Client(base_url="https://www.paypal.com/")

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 176–178why, one step back
176 request = client.build_request("GET", "http://www.paypal.com")
177 assert request.url.scheme == "https"178 assert request.url.is_ssl

Skip HSTS preloading on single-label domains (#1074)François Voronbefore adoption0641606

lines 179–193why, one step back
179
180181@pytest.mark.parametrize(182 "url,scheme,is_ssl",183 [184 ("http://www.paypal.com", "https", True),185 ("http://app", "http", False),186 ("http://192.168.1.42", "http", False),187 ],188)189def test_merge_url_hsts(url: str, scheme: str, is_ssl: bool):190 client = httpx.Client()191 request = client.build_request("GET", url)192 assert request.url.scheme == scheme193 assert request.url.is_ssl == is_ssl