test_client.py
This file
Path
tests/client/test_client.py
155 lines · 4.4 KiB · Python
Last changed by
Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
History of this fileTake it
1from datetime import timedelta23import pytest45import httpx678def test_get(server):9 url = server.url10 with httpx.Client() as http:11 response = http.get(url)12 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)232425def test_build_request(server):26 url = server.url.copy_with(path="/echo_headers")27 headers = {"Custom-header": "value"}2829 with httpx.Client() as client:30 request = client.build_request("GET", url)31 request.headers.update(headers)32 response = client.send(request)3334 assert response.status_code == 20035 assert response.url == url3637 assert response.json()["Custom-header"] == "value"383940def test_post(server):41 with httpx.Client() as client:42 response = client.post(server.url, data=b"Hello, world!")43 assert response.status_code == 20044 assert response.reason_phrase == "OK"454647def test_post_json(server):48 with httpx.Client() as client:49 response = client.post(server.url, json={"text": "Hello, world!"})50 assert response.status_code == 20051 assert response.reason_phrase == "OK"525354def test_stream_response(server):55 with httpx.Client() as client:56 with client.stream("GET", server.url) as response:57 content = response.read()58 assert response.status_code == 20059 assert content == b"Hello, world!"606162def test_stream_iterator(server):63 body = b""6465 with httpx.Client() as client:66 with client.stream("GET", server.url) as response:67 for chunk in response.iter_bytes():68 body += chunk6970 assert response.status_code == 20071 assert body == b"Hello, world!"727374def test_raw_iterator(server):75 body = b""7677 with httpx.Client() as client:78 with client.stream("GET", server.url) as response:79 for chunk in response.iter_raw():80 body += chunk8182 assert response.status_code == 20083 assert body == b"Hello, world!"848586def test_raise_for_status(server):87 with httpx.Client() as client:88 for status_code in (200, 400, 404, 500, 505):89 response = client.request(90 "GET", server.url.copy_with(path="/status/{}".format(status_code))91 )92 if 400 <= status_code < 600:93 with pytest.raises(httpx.HTTPError) as exc_info:94 response.raise_for_status()95 assert exc_info.value.response == response96 else:97 assert response.raise_for_status() is None9899100def test_options(server):101 with httpx.Client() as client:102 response = client.options(server.url)103 assert response.status_code == 200104 assert response.reason_phrase == "OK"105106107def test_head(server):108 with httpx.Client() as client:109 response = client.head(server.url)110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112113114def test_put(server):115 with httpx.Client() as client:116 response = client.put(server.url, data=b"Hello, world!")117 assert response.status_code == 200118 assert response.reason_phrase == "OK"119120121def test_patch(server):122 with httpx.Client() as client:123 response = client.patch(server.url, data=b"Hello, world!")124 assert response.status_code == 200125 assert response.reason_phrase == "OK"126127128def test_delete(server):129 with httpx.Client() as client:130 response = client.delete(server.url)131 assert response.status_code == 200132 assert response.reason_phrase == "OK"133134135def test_base_url(server):136 base_url = server.url137 with httpx.Client(base_url=base_url) as client:138 response = client.get("/")139 assert response.status_code == 200140 assert response.url == base_url141142143def test_merge_url():144 client = httpx.Client(base_url="https://www.paypal.com/")145 url = client.merge_url("http://www.paypal.com")146147 assert url.scheme == "https"148 assert url.is_ssl149150151def test_elapsed_delay(server):152 url = server.url.copy_with(path="/slow_response/100")153 with httpx.Client() as client:154 response = client.get(url)155 assert response.elapsed.total_seconds() > 0.0