test_client.py
This file
Path
tests/client/test_client.py
143 lines · 3.9 KiB · Python
Last changed by
Int status codes (#92)Tom Christiebefore adoption56a7943
History of this fileTake it
1import asyncio2import functools34import pytest56import http3789def threadpool(func):10 """11 Our sync tests should run in seperate thread to the uvicorn server.12 """1314 @functools.wraps(func)15 async def wrapped(*args, **kwargs):16 nonlocal func1718 loop = asyncio.get_event_loop()19 if kwargs:20 func = functools.partial(func, **kwargs)21 await loop.run_in_executor(None, func, *args)2223 return pytest.mark.asyncio(wrapped)242526@threadpool27def test_get(server):28 url = "http://127.0.0.1:8000/"29 with http3.Client() as http:30 response = http.get(url)31 assert response.status_code == 20032 assert response.url == http3.URL(url)33 assert response.content == b"Hello, world!"34 assert response.text == "Hello, world!"35 assert response.protocol == "HTTP/1.1"36 assert response.encoding == "iso-8859-1"37 assert response.request.url == http3.URL(url)38 assert response.headers39 assert response.is_redirect is False40 assert repr(response) == "<Response [200 OK])>"414243@threadpool44def test_post(server):45 with http3.Client() as http:46 response = http.post("http://127.0.0.1:8000/", data=b"Hello, world!")47 assert response.status_code == 20048 assert response.reason_phrase == "OK"495051@threadpool52def test_post_json(server):53 with http3.Client() as http:54 response = http.post("http://127.0.0.1:8000/", json={"text": "Hello, world!"})55 assert response.status_code == 20056 assert response.reason_phrase == "OK"575859@threadpool60def test_stream_response(server):61 with http3.Client() as http:62 response = http.get("http://127.0.0.1:8000/", stream=True)63 assert response.status_code == 20064 content = response.read()65 assert content == b"Hello, world!"666768@threadpool69def test_stream_iterator(server):70 with http3.Client() as http:71 response = http.get("http://127.0.0.1:8000/", stream=True)72 assert response.status_code == 20073 body = b""74 for chunk in response.stream():75 body += chunk76 assert body == b"Hello, world!"777879@threadpool80def test_raw_iterator(server):81 with http3.Client() as http:82 response = http.get("http://127.0.0.1:8000/", stream=True)83 assert response.status_code == 20084 body = b""85 for chunk in response.raw():86 body += chunk87 assert body == b"Hello, world!"88 response.close() # TODO: should Response be available as context managers?899091@threadpool92def test_raise_for_status(server):93 with http3.Client() as client:94 for status_code in (200, 400, 404, 500, 505):95 response = client.request(96 "GET", "http://127.0.0.1:8000/status/{}".format(status_code)97 )9899 if 400 <= status_code < 600:100 with pytest.raises(http3.exceptions.HttpError):101 response.raise_for_status()102 else:103 assert response.raise_for_status() is None104105106@threadpool107def test_options(server):108 with http3.Client() as http:109 response = http.options("http://127.0.0.1:8000/")110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112113114@threadpool115def test_head(server):116 with http3.Client() as http:117 response = http.head("http://127.0.0.1:8000/")118 assert response.status_code == 200119 assert response.reason_phrase == "OK"120121122@threadpool123def test_put(server):124 with http3.Client() as http:125 response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")126 assert response.status_code == 200127 assert response.reason_phrase == "OK"128129130@threadpool131def test_patch(server):132 with http3.Client() as http:133 response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")134 assert response.status_code == 200135 assert response.reason_phrase == "OK"136137138@threadpool139def test_delete(server):140 with http3.Client() as http:141 response = http.delete("http://127.0.0.1:8000/")142 assert response.status_code == 200143 assert response.reason_phrase == "OK"