Skip to content
Myr

test_sync.py

This file

Path

tests/test_sync.py

59 lines · 1.5 KiB · Python

Last changed by

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

History of this file

Take 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.body == b"Hello, world!"323334@threadpool35def test_post(server):36    with httpcore.SyncConnectionPool() as http:37        response = http.request("POST", "http://127.0.0.1:8000/", body=b"Hello, world!")38    assert response.status_code == 200394041@threadpool42def test_stream_response(server):43    with httpcore.SyncConnectionPool() as http:44        response = http.request("GET", "http://127.0.0.1:8000/", stream=True)45    assert response.status_code == 20046    assert not hasattr(response, "body")47    body = response.read()48    assert body == b"Hello, world!"495051@threadpool52def test_stream_iterator(server):53    with httpcore.SyncConnectionPool() as http:54        response = http.request("GET", "http://127.0.0.1:8000/", stream=True)55    assert response.status_code == 20056    body = b''57    for chunk in response.stream():58        body += chunk59    assert body == b"Hello, world!"