test_client.py
This file
Path
tests/client/test_client.py
164 lines · 4.6 KiB · Python
Last changed by
Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a
History of this fileTake it
The walk stopped after 200 commits: lines older than a738327 point at it.
Tweak docstringTom Christiebefore adoptiona738327
lines 1–7why, one step back1from datetime import timedelta23import pytest45import httpx67Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 8why, one step back8def test_get(server):Tweak docstringTom Christiebefore adoptiona738327
line 9why, one step back9 url = server.urlReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 10–11why, one step back10 with httpx.Client() as http:11 response = http.get(url)Tweak docstringTom Christiebefore adoptiona738327
lines 12–24why, one step back12 assert response.status_code == 20013 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)2324Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 25why, one step back25def test_build_request(server):Tweak docstringTom Christiebefore adoptiona738327
lines 26–28why, one step back26 url = server.url.copy_with(path="/echo_headers")27 headers = {"Custom-header": "value"}28Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 29why, one step back29 with httpx.Client() as client:Tweak docstringTom Christiebefore adoptiona738327
lines 30–31why, one step back30 request = client.build_request("GET", url)31 request.headers.update(headers)Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 32why, one step back32 response = client.send(request)Tweak docstringTom Christiebefore adoptiona738327
lines 33–39why, one step back3334 assert response.status_code == 20035 assert response.url == url3637 assert response.json()["Custom-header"] == "value"3839Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a
lines 40–55why, one step back40def 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"5455Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 56–58why, one step back56def test_post(server):57 with httpx.Client() as client:58 response = client.post(server.url, data=b"Hello, world!")Tweak docstringTom Christiebefore adoptiona738327
lines 59–62why, one step back59 assert response.status_code == 20060 assert response.reason_phrase == "OK"6162Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 63–65why, one step back63def test_post_json(server):64 with httpx.Client() as client:65 response = client.post(server.url, json={"text": "Hello, world!"})Tweak docstringTom Christiebefore adoptiona738327
lines 66–69why, one step back66 assert response.status_code == 20067 assert response.reason_phrase == "OK"6869Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 70–73why, one step back70def test_stream_response(server):71 with httpx.Client() as client:72 with client.stream("GET", server.url) as response:73 content = response.read()Tweak docstringTom Christiebefore adoptiona738327
lines 74–77why, one step back74 assert response.status_code == 20075 assert content == b"Hello, world!"7677Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 78why, one step back78def test_stream_iterator(server):Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b
lines 79–80why, one step back79 body = b""80Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 81–83why, one step back81 with httpx.Client() as client:82 with client.stream("GET", server.url) as response:83 for chunk in response.iter_bytes():Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b
lines 84–85why, one step back84 body += chunk85Tweak docstringTom Christiebefore adoptiona738327
lines 86–89why, one step back86 assert response.status_code == 20087 assert body == b"Hello, world!"8889Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 90why, one step back90def test_raw_iterator(server):Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b
lines 91–92why, one step back91 body = b""92Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 93–95why, one step back93 with httpx.Client() as client:94 with client.stream("GET", server.url) as response:95 for chunk in response.iter_raw():Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b
lines 96–97why, one step back96 body += chunk97Tweak docstringTom Christiebefore adoptiona738327
lines 98–101why, one step back98 assert response.status_code == 20099 assert body == b"Hello, world!"100101Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 102–103why, one step back102def test_raise_for_status(server):103 with httpx.Client() as client:Tweak docstringTom Christiebefore adoptiona738327
line 104why, one step back104 for status_code in (200, 400, 404, 500, 505):Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 105why, one step back105 response = client.request(Tweak docstringTom Christiebefore adoptiona738327
lines 106–115why, one step back106 "GET", server.url.copy_with(path="/status/{}".format(status_code))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 == response112 else:113 assert response.raise_for_status() is None114115Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 116–118why, one step back116def test_options(server):117 with httpx.Client() as client:118 response = client.options(server.url)Tweak docstringTom Christiebefore adoptiona738327
lines 119–122why, one step back119 assert response.status_code == 200120 assert response.reason_phrase == "OK"121122Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 123–125why, one step back123def test_head(server):124 with httpx.Client() as client:125 response = client.head(server.url)Tweak docstringTom Christiebefore adoptiona738327
lines 126–129why, one step back126 assert response.status_code == 200127 assert response.reason_phrase == "OK"128129Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 130–132why, one step back130def test_put(server):131 with httpx.Client() as client:132 response = client.put(server.url, data=b"Hello, world!")Tweak docstringTom Christiebefore adoptiona738327
lines 133–136why, one step back133 assert response.status_code == 200134 assert response.reason_phrase == "OK"135136Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 137–139why, one step back137def test_patch(server):138 with httpx.Client() as client:139 response = client.patch(server.url, data=b"Hello, world!")Tweak docstringTom Christiebefore adoptiona738327
lines 140–143why, one step back140 assert response.status_code == 200141 assert response.reason_phrase == "OK"142143Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 144–146why, one step back144def test_delete(server):145 with httpx.Client() as client:146 response = client.delete(server.url)Tweak docstringTom Christiebefore adoptiona738327
lines 147–150why, one step back147 assert response.status_code == 200148 assert response.reason_phrase == "OK"149150Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 151why, one step back151def test_base_url(server):Tweak docstringTom Christiebefore adoptiona738327
line 152why, one step back152 base_url = server.urlReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 153–154why, one step back153 with httpx.Client(base_url=base_url) as client:154 response = client.get("/")Tweak docstringTom Christiebefore adoptiona738327
lines 155–159why, one step back155 assert response.status_code == 200156 assert response.url == base_url157158159def test_merge_url():Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 160why, one step back160 client = httpx.Client(base_url="https://www.paypal.com/")Tweak docstringTom Christiebefore adoptiona738327
lines 161–164why, one step back161 url = client.merge_url("http://www.paypal.com")162163 assert url.scheme == "https"164 assert url.is_ssl