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 fileTake it
The walk stopped after 200 commits: lines older than a738327 point at it.
Tweak docstringTom Christiebefore adoptiona738327
lines 1–2why, one step back1from datetime import timedelta2Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 3why, one step back3import httpcoreTweak docstringTom Christiebefore adoptiona738327
lines 4–6why, one step back4import pytest56import httpxIncreased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 7why, one step back7from httpx import WSGIDispatchTweak docstringTom Christiebefore adoptiona738327
lines 8–9why, one step back89Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 10why, one step back10def test_get(server):Tweak docstringTom Christiebefore adoptiona738327
line 11why, one step back11 url = server.urlReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 12–13why, one step back12 with httpx.Client() as http:13 response = http.get(url)Tweak docstringTom Christiebefore adoptiona738327
lines 14–26why, one step back14 assert response.status_code == 20015 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)2526Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 27why, one step back27def test_build_request(server):Tweak docstringTom Christiebefore adoptiona738327
lines 28–30why, one step back28 url = server.url.copy_with(path="/echo_headers")29 headers = {"Custom-header": "value"}30Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 31why, one step back31 with httpx.Client() as client:Tweak docstringTom Christiebefore adoptiona738327
lines 32–33why, one step back32 request = client.build_request("GET", url)33 request.headers.update(headers)Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 34why, one step back34 response = client.send(request)Tweak docstringTom Christiebefore adoptiona738327
lines 35–41why, one step back3536 assert response.status_code == 20037 assert response.url == url3839 assert response.json()["Custom-header"] == "value"4041Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a
lines 42–57why, one step back42def 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"5657Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 58–60why, one step back58def 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 back61 assert response.status_code == 20062 assert response.reason_phrase == "OK"6364Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 65–67why, one step back65def 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 back68 assert response.status_code == 20069 assert response.reason_phrase == "OK"7071Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 72–75why, one step back72def 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 back76 assert response.status_code == 20077 assert content == b"Hello, world!"7879Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 80why, one step back80def test_stream_iterator(server):Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b
lines 81–82why, one step back81 body = b""82Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 83–85why, one step back83 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 back86 body += chunk87Tweak docstringTom Christiebefore adoptiona738327
lines 88–91why, one step back88 assert response.status_code == 20089 assert body == b"Hello, world!"9091Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 92why, one step back92def test_raw_iterator(server):Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b
lines 93–94why, one step back93 body = b""94Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 95–97why, one step back95 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 back98 body += chunk99Tweak docstringTom Christiebefore adoptiona738327
lines 100–103why, one step back100 assert response.status_code == 200101 assert body == b"Hello, world!"102103Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 104–105why, one step back104def test_raise_for_status(server):105 with httpx.Client() as client:Tweak docstringTom Christiebefore adoptiona738327
line 106why, one step back106 for status_code in (200, 400, 404, 500, 505):Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 107why, one step back107 response = client.request(Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 108why, one step back108 "GET", server.url.copy_with(path=f"/status/{status_code}")Tweak docstringTom Christiebefore adoptiona738327
lines 109–113why, one step back109 )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 == responseIncreased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 114why, one step back114 assert exc_info.value.request.url.path == f"/status/{status_code}"Tweak docstringTom Christiebefore adoptiona738327
line 115why, one step back115 else:Fixed mypy errors in test_async_client.py and test_client.py (#985)Hasan Ramezanibefore adoption21d7e16
line 116why, one step back116 assert response.raise_for_status() is None # type: ignoreTweak docstringTom Christiebefore adoptiona738327
lines 117–118why, one step back117118Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 119–121why, one step back119def test_options(server):120 with httpx.Client() as client:121 response = client.options(server.url)Tweak docstringTom Christiebefore adoptiona738327
lines 122–125why, one step back122 assert response.status_code == 200123 assert response.reason_phrase == "OK"124125Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 126–128why, one step back126def test_head(server):127 with httpx.Client() as client:128 response = client.head(server.url)Tweak docstringTom Christiebefore adoptiona738327
lines 129–132why, one step back129 assert response.status_code == 200130 assert response.reason_phrase == "OK"131132Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 133–135why, one step back133def 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 back136 assert response.status_code == 200137 assert response.reason_phrase == "OK"138139Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 140–142why, one step back140def 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 back143 assert response.status_code == 200144 assert response.reason_phrase == "OK"145146Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 147–149why, one step back147def test_delete(server):148 with httpx.Client() as client:149 response = client.delete(server.url)Tweak docstringTom Christiebefore adoptiona738327
lines 150–153why, one step back150 assert response.status_code == 200151 assert response.reason_phrase == "OK"152153Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 154why, one step back154def test_base_url(server):Tweak docstringTom Christiebefore adoptiona738327
line 155why, one step back155 base_url = server.urlReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 156–157why, one step back156 with httpx.Client(base_url=base_url) as client:157 response = client.get("/")Tweak docstringTom Christiebefore adoptiona738327
lines 158–162why, one step back158 assert response.status_code == 200159 assert response.url == base_url160161162def test_merge_url():Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 163why, one step back163 client = httpx.Client(base_url="https://www.paypal.com/")Tweak docstringTom Christiebefore adoptiona738327
lines 164–167why, one step back164 url = client.merge_url("http://www.paypal.com")165166 assert url.scheme == "https"167 assert url.is_sslIncreased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
lines 168–192why, one step back168169170def 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 )