Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

165 lines · 4.7 KiB · Python

Last changed by

Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74

History of this file

Take it

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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):

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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)

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 33–39why, one step back
33
34 assert response.status_code == 20035 assert response.url == url3637 assert response.json()["Custom-header"] == "value"3839

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

lines 40–55why, one step back
40def test_build_post_request(server):
41 url = server.url.copy_with(path="/echo_headers")42 headers = {"Custom-header": "value"}4344 with httpx.Client() as client:45 request = client.build_request("POST", url)46 request.headers.update(headers)47 response = client.send(request)4849 assert response.status_code == 20050 assert response.url == url5152 assert response.json()["Content-length"] == "0"53 assert response.json()["Custom-header"] == "value"5455

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 59–62why, one step back
59 assert response.status_code == 200
60 assert response.reason_phrase == "OK"6162

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 66–69why, one step back
66 assert response.status_code == 200
67 assert response.reason_phrase == "OK"6869

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 70–73why, one step back
70def test_stream_response(server):
71 with httpx.Client() as client:72 with client.stream("GET", server.url) as response:73 content = response.read()

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 74–77why, one step back
74 assert response.status_code == 200
75 assert content == b"Hello, world!"7677

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 79–80why, one step back
79 body = b""
80

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 84–89why, one step back
84 body += chunk
8586 assert response.status_code == 20087 assert body == b"Hello, world!"8889

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 91–92why, one step back
91 body = b""
92

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 96–101why, one step back
96 body += chunk
9798 assert response.status_code == 20099 assert body == b"Hello, world!"100101

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

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

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 107–111why, one step back
107 )
108 if 400 <= status_code < 600:109 with pytest.raises(httpx.HTTPError) as exc_info:110 response.raise_for_status()111 assert exc_info.value.response == response

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

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 113why, one step back
113 else:

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

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 115–116why, one step back
115
116

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 117–119why, one step back
117def test_options(server):
118 with httpx.Client() as client:119 response = client.options(server.url)

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 120–123why, one step back
120 assert response.status_code == 200
121 assert response.reason_phrase == "OK"122123

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 124–126why, one step back
124def test_head(server):
125 with httpx.Client() as client:126 response = client.head(server.url)

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 127–130why, one step back
127 assert response.status_code == 200
128 assert response.reason_phrase == "OK"129130

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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_patch(server):
139 with httpx.Client() as client:140 response = client.patch(server.url, data=b"Hello, world!")

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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_delete(server):
146 with httpx.Client() as client:147 response = client.delete(server.url)

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 156–160why, one step back
156 assert response.status_code == 200
157 assert response.url == base_url158159160def test_merge_url():

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 162–165why, one step back
162 url = client.merge_url("http://www.paypal.com")
163164 assert url.scheme == "https"165 assert url.is_ssl