test_sync.py
This file
Path
tests/test_sync.py
60 lines · 1.6 KiB · Python
Last changed by
Add idna support. More tests.Tom Christiebefore adoptionb948798
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.SyncConnectionPool() as http:29 response = http.request("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.SyncConnectionPool() as http:38 response = http.request("POST", "http://127.0.0.1:8000/", body=b"Hello, world!")39 assert response.status_code == 20040 assert response.reason_phrase == "OK"414243@threadpool44def test_stream_response(server):45 with httpcore.SyncConnectionPool() as http:46 response = http.request("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.SyncConnectionPool() as http:55 response = http.request("GET", "http://127.0.0.1:8000/", stream=True)56 assert response.status_code == 20057 content = b""58 for chunk in response.stream():59 content += chunk60 assert content == b"Hello, world!"