test_api.py
This file
Path
tests/test_api.py
91 lines · 2.1 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
1import asyncio2import functools34import pytest56import httpx789def 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 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!"323334@threadpool35def test_post(server):36 response = httpx.post("http://127.0.0.1:8000/", data=b"Hello, world!")37 assert response.status_code == 20038 assert response.reason_phrase == "OK"394041@threadpool42def test_post_byte_iterator(server):43 def data():44 yield b"Hello"45 yield b", "46 yield b"world!"4748 response = httpx.post("http://127.0.0.1:8000/", data=data())49 assert response.status_code == 20050 assert response.reason_phrase == "OK"515253@threadpool54def test_options(server):55 response = httpx.options("http://127.0.0.1:8000/")56 assert response.status_code == 20057 assert response.reason_phrase == "OK"585960@threadpool61def test_head(server):62 response = httpx.head("http://127.0.0.1:8000/")63 assert response.status_code == 20064 assert response.reason_phrase == "OK"656667@threadpool68def test_put(server):69 response = httpx.put("http://127.0.0.1:8000/", data=b"Hello, world!")70 assert response.status_code == 20071 assert response.reason_phrase == "OK"727374@threadpool75def test_patch(server):76 response = httpx.patch("http://127.0.0.1:8000/", data=b"Hello, world!")77 assert response.status_code == 20078 assert response.reason_phrase == "OK"798081@threadpool82def test_delete(server):83 response = httpx.delete("http://127.0.0.1:8000/")84 assert response.status_code == 20085 assert response.reason_phrase == "OK"868788@threadpool89def test_get_invalid_url(server):90 with pytest.raises(httpx.InvalidURL):91 httpx.get("invalid://example.org")