test_sync.py
This file
Path
tests/test_sync.py
71 lines · 1.8 KiB · Python
Last changed by
Truckin' onTom Christiebefore adoption6b60d95
History of this fileTake it
1import asyncio2import functools34import pytest56import httpcore789def 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 with httpcore.SyncClient() as http:29 response = http.get("http://127.0.0.1:8000/")30 assert response.status_code == 20031 assert response.content == b"Hello, world!"32 assert response.text == "Hello, world!"333435@threadpool36def test_post(server):37 with httpcore.SyncClient() as http:38 response = http.post("http://127.0.0.1:8000/", content=b"Hello, world!")39 assert response.status_code == 20040 assert response.reason_phrase == "OK"414243@threadpool44def test_stream_response(server):45 with httpcore.SyncClient() as http:46 response = http.get("http://127.0.0.1:8000/", stream=True)47 assert response.status_code == 20048 content = response.read()49 assert content == b"Hello, world!"505152@threadpool53def test_stream_iterator(server):54 with httpcore.SyncClient() as http:55 response = http.get("http://127.0.0.1:8000/", stream=True)56 assert response.status_code == 20057 body = b""58 for chunk in response.stream():59 body += chunk60 assert body == b"Hello, world!"616263@threadpool64def test_raw_iterator(server):65 with httpcore.SyncClient() as http:66 response = http.get("http://127.0.0.1:8000/", stream=True)67 assert response.status_code == 20068 body = b""69 for chunk in response.raw():70 body += chunk71 assert body == b"Hello, world!"