test_client.py
This file
Path
tests/client/test_client.py
164 lines · 4.7 KiB · Python
Last changed by
Tighten public client methods (#997)Tom Christiebefore adoptione107e0f
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_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"545556def test_post(server):57 with httpx.Client() as client:58 response = client.post(server.url, data=b"Hello, world!")59 assert response.status_code == 20060 assert response.reason_phrase == "OK"616263def test_post_json(server):64 with httpx.Client() as client:65 response = client.post(server.url, json={"text": "Hello, world!"})66 assert response.status_code == 20067 assert response.reason_phrase == "OK"686970def test_stream_response(server):71 with httpx.Client() as client:72 with client.stream("GET", server.url) as response:73 content = response.read()74 assert response.status_code == 20075 assert content == b"Hello, world!"767778def test_stream_iterator(server):79 body = b""8081 with httpx.Client() as client:82 with client.stream("GET", server.url) as response:83 for chunk in response.iter_bytes():84 body += chunk8586 assert response.status_code == 20087 assert body == b"Hello, world!"888990def test_raw_iterator(server):91 body = b""9293 with httpx.Client() as client:94 with client.stream("GET", server.url) as response:95 for chunk in response.iter_raw():96 body += chunk9798 assert response.status_code == 20099 assert body == b"Hello, world!"100101102def test_raise_for_status(server):103 with httpx.Client() as client:104 for status_code in (200, 400, 404, 500, 505):105 response = client.request(106 "GET", server.url.copy_with(path=f"/status/{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 assert exc_info.value.request.url.path == f"/status/{status_code}"113 else:114 assert response.raise_for_status() is None # type: ignore115116117def test_options(server):118 with httpx.Client() as client:119 response = client.options(server.url)120 assert response.status_code == 200121 assert response.reason_phrase == "OK"122123124def test_head(server):125 with httpx.Client() as client:126 response = client.head(server.url)127 assert response.status_code == 200128 assert response.reason_phrase == "OK"129130131def test_put(server):132 with httpx.Client() as client:133 response = client.put(server.url, data=b"Hello, world!")134 assert response.status_code == 200135 assert response.reason_phrase == "OK"136137138def test_patch(server):139 with httpx.Client() as client:140 response = client.patch(server.url, data=b"Hello, world!")141 assert response.status_code == 200142 assert response.reason_phrase == "OK"143144145def test_delete(server):146 with httpx.Client() as client:147 response = client.delete(server.url)148 assert response.status_code == 200149 assert response.reason_phrase == "OK"150151152def test_base_url(server):153 base_url = server.url154 with httpx.Client(base_url=base_url) as client:155 response = client.get("/")156 assert response.status_code == 200157 assert response.url == base_url158159160def test_merge_url():161 client = httpx.Client(base_url="https://www.paypal.com/")162 request = client.build_request("GET", "http://www.paypal.com")163 assert request.url.scheme == "https"164 assert request.url.is_ssl