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
1from datetime import timedelta23import httpcore4import pytest56import httpx7from httpx import WSGIDispatch8910def test_get(server):11 url = server.url12 with httpx.Client() as http:13 response = http.get(url)14 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)252627def test_build_request(server):28 url = server.url.copy_with(path="/echo_headers")29 headers = {"Custom-header": "value"}3031 with httpx.Client() as client:32 request = client.build_request("GET", url)33 request.headers.update(headers)34 response = client.send(request)3536 assert response.status_code == 20037 assert response.url == url3839 assert response.json()["Custom-header"] == "value"404142def 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"565758def test_post(server):59 with httpx.Client() as client:60 response = client.post(server.url, data=b"Hello, world!")61 assert response.status_code == 20062 assert response.reason_phrase == "OK"636465def test_post_json(server):66 with httpx.Client() as client:67 response = client.post(server.url, json={"text": "Hello, world!"})68 assert response.status_code == 20069 assert response.reason_phrase == "OK"707172def test_stream_response(server):73 with httpx.Client() as client:74 with client.stream("GET", server.url) as response:75 content = response.read()76 assert response.status_code == 20077 assert content == b"Hello, world!"787980def test_stream_iterator(server):81 body = b""8283 with httpx.Client() as client:84 with client.stream("GET", server.url) as response:85 for chunk in response.iter_bytes():86 body += chunk8788 assert response.status_code == 20089 assert body == b"Hello, world!"909192def test_raw_iterator(server):93 body = b""9495 with httpx.Client() as client:96 with client.stream("GET", server.url) as response:97 for chunk in response.iter_raw():98 body += chunk99100 assert response.status_code == 200101 assert body == b"Hello, world!"102103104def test_raise_for_status(server):105 with httpx.Client() as client:106 for status_code in (200, 400, 404, 500, 505):107 response = client.request(108 "GET", server.url.copy_with(path=f"/status/{status_code}")109 )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 == response114 assert exc_info.value.request.url.path == f"/status/{status_code}"115 else:116 assert response.raise_for_status() is None # type: ignore117118119def test_options(server):120 with httpx.Client() as client:121 response = client.options(server.url)122 assert response.status_code == 200123 assert response.reason_phrase == "OK"124125126def test_head(server):127 with httpx.Client() as client:128 response = client.head(server.url)129 assert response.status_code == 200130 assert response.reason_phrase == "OK"131132133def test_put(server):134 with httpx.Client() as client:135 response = client.put(server.url, data=b"Hello, world!")136 assert response.status_code == 200137 assert response.reason_phrase == "OK"138139140def test_patch(server):141 with httpx.Client() as client:142 response = client.patch(server.url, data=b"Hello, world!")143 assert response.status_code == 200144 assert response.reason_phrase == "OK"145146147def test_delete(server):148 with httpx.Client() as client:149 response = client.delete(server.url)150 assert response.status_code == 200151 assert response.reason_phrase == "OK"152153154def test_base_url(server):155 base_url = server.url156 with httpx.Client(base_url=base_url) as client:157 response = client.get("/")158 assert response.status_code == 200159 assert response.url == base_url160161162def test_merge_url():163 client = httpx.Client(base_url="https://www.paypal.com/")164 url = client.merge_url("http://www.paypal.com")165166 assert url.scheme == "https"167 assert url.is_ssl168169170def 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 )