test_client.py
This file
Path
tests/client/test_client.py
176 lines · 5.0 KiB · Python
Last changed by
Make HTTPError importable from the top-level (#421)Josep Cugatbefore adoptiona028256
History of this fileTake it
1from datetime import timedelta2from time import sleep34import pytest56import httpx789def test_get(server):10 url = server.url11 with httpx.Client() as http:12 response = http.get(url)13 assert response.status_code == 20014 assert response.url == url15 assert response.content == b"Hello, world!"16 assert response.text == "Hello, world!"17 assert response.http_version == "HTTP/1.1"18 assert response.encoding == "iso-8859-1"19 assert response.request.url == url20 assert response.headers21 assert response.is_redirect is False22 assert repr(response) == "<Response [200 OK]>"23 assert response.elapsed > timedelta(0)242526def test_build_request(server):27 url = server.url.copy_with(path="/echo_headers")28 headers = {"Custom-header": "value"}2930 with httpx.Client() as http:31 request = http.build_request("GET", url)32 request.headers.update(headers)33 response = http.send(request)3435 assert response.status_code == 20036 assert response.url == url3738 assert response.json()["Custom-header"] == "value"394041def test_post(server):42 with httpx.Client() as http:43 response = http.post(server.url, data=b"Hello, world!")44 assert response.status_code == 20045 assert response.reason_phrase == "OK"464748def test_post_json(server):49 with httpx.Client() as http:50 response = http.post(server.url, json={"text": "Hello, world!"})51 assert response.status_code == 20052 assert response.reason_phrase == "OK"535455def test_stream_response(server):56 with httpx.Client() as http:57 response = http.get(server.url, stream=True)58 assert response.status_code == 20059 content = response.read()60 assert content == b"Hello, world!"616263def test_stream_iterator(server):64 with httpx.Client() as http:65 response = http.get(server.url, stream=True)66 assert response.status_code == 20067 body = b""68 for chunk in response.stream():69 body += chunk70 assert body == b"Hello, world!"717273def test_raw_iterator(server):74 with httpx.Client() as http:75 response = http.get(server.url, stream=True)76 assert response.status_code == 20077 body = b""78 for chunk in response.raw():79 body += chunk80 assert body == b"Hello, world!"81 response.close() # TODO: should Response be available as context managers?828384def test_raise_for_status(server):85 with httpx.Client() as client:86 for status_code in (200, 400, 404, 500, 505):87 response = client.request(88 "GET", server.url.copy_with(path="/status/{}".format(status_code))89 )90 if 400 <= status_code < 600:91 with pytest.raises(httpx.HTTPError) as exc_info:92 response.raise_for_status()93 assert exc_info.value.response == response94 else:95 assert response.raise_for_status() is None969798def test_options(server):99 with httpx.Client() as http:100 response = http.options(server.url)101 assert response.status_code == 200102 assert response.reason_phrase == "OK"103104105def test_head(server):106 with httpx.Client() as http:107 response = http.head(server.url)108 assert response.status_code == 200109 assert response.reason_phrase == "OK"110111112def test_put(server):113 with httpx.Client() as http:114 response = http.put(server.url, data=b"Hello, world!")115 assert response.status_code == 200116 assert response.reason_phrase == "OK"117118119def test_patch(server):120 with httpx.Client() as http:121 response = http.patch(server.url, data=b"Hello, world!")122 assert response.status_code == 200123 assert response.reason_phrase == "OK"124125126def test_delete(server):127 with httpx.Client() as http:128 response = http.delete(server.url)129 assert response.status_code == 200130 assert response.reason_phrase == "OK"131132133def test_base_url(server):134 base_url = server.url135 with httpx.Client(base_url=base_url) as http:136 response = http.get("/")137 assert response.status_code == 200138 assert response.url == base_url139140141def test_merge_url():142 client = httpx.Client(base_url="https://www.paypal.com/")143 url = client.merge_url("http://www.paypal.com")144145 assert url.scheme == "https"146 assert url.is_ssl147148149class DerivedFromAsyncioBackend(httpx.AsyncioBackend):150 pass151152153class AnyBackend:154 pass155156157def test_client_backend_must_be_asyncio_based():158 httpx.Client(backend=httpx.AsyncioBackend())159 httpx.Client(backend=DerivedFromAsyncioBackend())160161 with pytest.raises(ValueError):162 httpx.Client(backend=AnyBackend())163164165def test_elapsed_delay(server):166 with httpx.Client() as http:167 response = http.get(server.url.copy_with(path="/slow_response/100"))168 assert response.elapsed.total_seconds() == pytest.approx(0.1, abs=0.01)169170171def test_elapsed_delay_ignores_read_time(server):172 with httpx.Client() as http:173 response = http.get(server.url.copy_with(path="/slow_response/50"), stream=True)174 sleep(0.1)175 response.read()176 assert response.elapsed.total_seconds() == pytest.approx(0.05, abs=0.01)