test_client.py
This file
Path
tests/client/test_client.py
143 lines · 4.0 KiB · Python
Last changed by
Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
History of this fileTake it
1import pytest23import httpx456def test_get(server):7 url = server.url8 with httpx.Client() as http:9 response = http.get(url)10 assert response.status_code == 20011 assert response.url == url12 assert response.content == b"Hello, world!"13 assert response.text == "Hello, world!"14 assert response.http_version == "HTTP/1.1"15 assert response.encoding == "iso-8859-1"16 assert response.request.url == url17 assert response.headers18 assert response.is_redirect is False19 assert repr(response) == "<Response [200 OK]>"202122def test_post(server):23 with httpx.Client() as http:24 response = http.post(server.url, data=b"Hello, world!")25 assert response.status_code == 20026 assert response.reason_phrase == "OK"272829def test_post_json(server):30 with httpx.Client() as http:31 response = http.post(server.url, json={"text": "Hello, world!"})32 assert response.status_code == 20033 assert response.reason_phrase == "OK"343536def test_stream_response(server):37 with httpx.Client() as http:38 response = http.get(server.url, stream=True)39 assert response.status_code == 20040 content = response.read()41 assert content == b"Hello, world!"424344def test_stream_iterator(server):45 with httpx.Client() as http:46 response = http.get(server.url, stream=True)47 assert response.status_code == 20048 body = b""49 for chunk in response.stream():50 body += chunk51 assert body == b"Hello, world!"525354def test_raw_iterator(server):55 with httpx.Client() as http:56 response = http.get(server.url, stream=True)57 assert response.status_code == 20058 body = b""59 for chunk in response.raw():60 body += chunk61 assert body == b"Hello, world!"62 response.close() # TODO: should Response be available as context managers?636465def test_raise_for_status(server):66 with httpx.Client() as client:67 for status_code in (200, 400, 404, 500, 505):68 response = client.request(69 "GET", server.url.copy_with(path="/status/{}".format(status_code))70 )71 if 400 <= status_code < 600:72 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:73 response.raise_for_status()74 assert exc_info.value.response == response75 else:76 assert response.raise_for_status() is None777879def test_options(server):80 with httpx.Client() as http:81 response = http.options(server.url)82 assert response.status_code == 20083 assert response.reason_phrase == "OK"848586def test_head(server):87 with httpx.Client() as http:88 response = http.head(server.url)89 assert response.status_code == 20090 assert response.reason_phrase == "OK"919293def test_put(server):94 with httpx.Client() as http:95 response = http.put(server.url, data=b"Hello, world!")96 assert response.status_code == 20097 assert response.reason_phrase == "OK"9899100def test_patch(server):101 with httpx.Client() as http:102 response = http.patch(server.url, data=b"Hello, world!")103 assert response.status_code == 200104 assert response.reason_phrase == "OK"105106107def test_delete(server):108 with httpx.Client() as http:109 response = http.delete(server.url)110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112113114def test_base_url(server):115 base_url = server.url116 with httpx.Client(base_url=base_url) as http:117 response = http.get("/")118 assert response.status_code == 200119 assert response.url == base_url120121122def test_merge_url():123 client = httpx.Client(base_url="https://www.paypal.com/")124 url = client.merge_url("http://www.paypal.com")125126 assert url.scheme == "https"127 assert url.is_ssl128129130class DerivedFromAsyncioBackend(httpx.AsyncioBackend):131 pass132133134class AnyBackend:135 pass136137138def test_client_backend_must_be_asyncio_based():139 httpx.Client(backend=httpx.AsyncioBackend())140 httpx.Client(backend=DerivedFromAsyncioBackend())141142 with pytest.raises(ValueError):143 httpx.Client(backend=AnyBackend())