Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

192 lines · 5.4 KiB · Python

Last changed by

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

History of this file

Take it

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

Tweak docstringTom Christiebefore adoptiona738327

lines 1–2why, one step back
1from datetime import timedelta
2

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

line 3why, one step back
3import httpcore

Tweak docstringTom Christiebefore adoptiona738327

lines 4–6why, one step back
4import pytest
56import httpx

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

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

Tweak docstringTom Christiebefore adoptiona738327

lines 8–9why, one step back
8
9

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 14–26why, one step back
14 assert response.status_code == 200
15 assert response.url == url16 assert response.content == b"Hello, world!"17 assert response.text == "Hello, world!"18 assert response.http_version == "HTTP/1.1"19 assert response.encoding == "iso-8859-1"20 assert response.request.url == url21 assert response.headers22 assert response.is_redirect is False23 assert repr(response) == "<Response [200 OK]>"24 assert response.elapsed > timedelta(0)2526

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 35–41why, one step back
35
36 assert response.status_code == 20037 assert response.url == url3839 assert response.json()["Custom-header"] == "value"4041

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

lines 42–57why, one step back
42def test_build_post_request(server):
43 url = server.url.copy_with(path="/echo_headers")44 headers = {"Custom-header": "value"}4546 with httpx.Client() as client:47 request = client.build_request("POST", url)48 request.headers.update(headers)49 response = client.send(request)5051 assert response.status_code == 20052 assert response.url == url5354 assert response.json()["Content-length"] == "0"55 assert response.json()["Custom-header"] == "value"5657

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 61–64why, one step back
61 assert response.status_code == 200
62 assert response.reason_phrase == "OK"6364

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 68–71why, one step back
68 assert response.status_code == 200
69 assert response.reason_phrase == "OK"7071

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 76–79why, one step back
76 assert response.status_code == 200
77 assert content == b"Hello, world!"7879

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 81–82why, one step back
81 body = b""
82

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 86–87why, one step back
86 body += chunk
87

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

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

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 98–99why, one step back
98 body += chunk
99

Tweak docstringTom Christiebefore adoptiona738327

lines 100–103why, one step back
100 assert response.status_code == 200
101 assert body == b"Hello, world!"102103

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

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

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

Tweak docstringTom Christiebefore adoptiona738327

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

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

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

Tweak docstringTom Christiebefore adoptiona738327

line 115why, one step back
115 else:

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

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

Tweak docstringTom Christiebefore adoptiona738327

lines 117–118why, one step back
117
118

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 122–125why, one step back
122 assert response.status_code == 200
123 assert response.reason_phrase == "OK"124125

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 129–132why, one step back
129 assert response.status_code == 200
130 assert response.reason_phrase == "OK"131132

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 136–139why, one step back
136 assert response.status_code == 200
137 assert response.reason_phrase == "OK"138139

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 143–146why, one step back
143 assert response.status_code == 200
144 assert response.reason_phrase == "OK"145146

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 147–149why, one step back
147def test_delete(server):
148 with httpx.Client() as client:149 response = client.delete(server.url)

Tweak docstringTom Christiebefore adoptiona738327

lines 150–153why, one step back
150 assert response.status_code == 200
151 assert response.reason_phrase == "OK"152153

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 158–162why, one step back
158 assert response.status_code == 200
159 assert response.url == base_url160161162def test_merge_url():

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

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

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

lines 168–192why, one step back
168
169170def test_dispatch_deprecated():171 dispatch = httpcore.SyncHTTPTransport()172173 with pytest.warns(DeprecationWarning) as record:174 client = httpx.Client(dispatch=dispatch)175176 assert client.transport is dispatch177 assert len(record) == 1178 assert record[0].message.args[0] == (179 "The dispatch argument is deprecated since v0.13 and will be "180 "removed in a future release, please use 'transport'"181 )182183184def test_wsgi_dispatch_deprecated():185 with pytest.warns(DeprecationWarning) as record:186 WSGIDispatch(None)187188 assert len(record) == 1189 assert (190 record[0].message.args[0]191 == "WSGIDispatch is deprecated, please use WSGITransport"192 )