test_api.py
This file
Path
tests/test_api.py
92 lines · 2.2 KiB · Python
Last changed by
Force HTTP/1.1 on short-lived connections (#284)Seth Michael Larsonbefore adoptione7aa6d6
History of this fileTake it
1import asyncio2import functools34import pytest56import httpx789def threadpool(func):10 """11 Our sync tests should run in separate 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 response = httpx.get("http://127.0.0.1:8000/")29 assert response.status_code == 20030 assert response.reason_phrase == "OK"31 assert response.text == "Hello, world!"32 assert response.http_version == "HTTP/1.1"333435@threadpool36def test_post(server):37 response = httpx.post("http://127.0.0.1:8000/", data=b"Hello, world!")38 assert response.status_code == 20039 assert response.reason_phrase == "OK"404142@threadpool43def test_post_byte_iterator(server):44 def data():45 yield b"Hello"46 yield b", "47 yield b"world!"4849 response = httpx.post("http://127.0.0.1:8000/", data=data())50 assert response.status_code == 20051 assert response.reason_phrase == "OK"525354@threadpool55def test_options(server):56 response = httpx.options("http://127.0.0.1:8000/")57 assert response.status_code == 20058 assert response.reason_phrase == "OK"596061@threadpool62def test_head(server):63 response = httpx.head("http://127.0.0.1:8000/")64 assert response.status_code == 20065 assert response.reason_phrase == "OK"666768@threadpool69def test_put(server):70 response = httpx.put("http://127.0.0.1:8000/", data=b"Hello, world!")71 assert response.status_code == 20072 assert response.reason_phrase == "OK"737475@threadpool76def test_patch(server):77 response = httpx.patch("http://127.0.0.1:8000/", data=b"Hello, world!")78 assert response.status_code == 20079 assert response.reason_phrase == "OK"808182@threadpool83def test_delete(server):84 response = httpx.delete("http://127.0.0.1:8000/")85 assert response.status_code == 20086 assert response.reason_phrase == "OK"878889@threadpool90def test_get_invalid_url(server):91 with pytest.raises(httpx.InvalidURL):92 httpx.get("invalid://example.org")