Skip to content
Myr

test_threaded.py

This file

Path

tests/dispatch/test_threaded.py

112 lines · 3.3 KiB · Python

Last changed by

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

History of this file

Take it

1import json23from httpx import (4    AsyncClient,5    CertTypes,6    Client,7    Dispatcher,8    Request,9    Response,10    TimeoutTypes,11    VerifyTypes,12)131415def streaming_body():16    for part in [b"Hello", b", ", b"world!"]:17        yield part181920class MockDispatch(Dispatcher):21    def send(22        self,23        request: Request,24        verify: VerifyTypes = None,25        cert: CertTypes = None,26        timeout: TimeoutTypes = None,27    ) -> Response:28        if request.url.path == "/streaming_response":29            return Response(200, content=streaming_body(), request=request)30        elif request.url.path == "/echo_request_body":31            content = request.read()32            return Response(200, content=content, request=request)33        elif request.url.path == "/echo_request_body_streaming":34            content = b"".join([part for part in request.stream()])35            return Response(200, content=content, request=request)36        else:37            body = json.dumps({"hello": "world"}).encode()38            return Response(200, content=body, request=request)394041def test_threaded_dispatch():42    """43    Use a synchronous 'Dispatcher' class with the client.44    Calls to the dispatcher will end up running within a thread pool.45    """46    url = "https://example.org/"47    with Client(dispatch=MockDispatch()) as client:48        response = client.get(url)4950    assert response.status_code == 20051    assert response.json() == {"hello": "world"}525354async def test_async_threaded_dispatch(backend):55    """56    Use a synchronous 'Dispatcher' class with the async client.57    Calls to the dispatcher will end up running within a thread pool.58    """59    url = "https://example.org/"60    async with AsyncClient(dispatch=MockDispatch(), backend=backend) as client:61        response = await client.get(url)6263    assert response.status_code == 20064    assert response.json() == {"hello": "world"}656667def test_threaded_streaming_response():68    url = "https://example.org/streaming_response"69    with Client(dispatch=MockDispatch()) as client:70        response = client.get(url)7172    assert response.status_code == 20073    assert response.text == "Hello, world!"747576def test_threaded_streaming_request():77    url = "https://example.org/echo_request_body"78    with Client(dispatch=MockDispatch()) as client:79        response = client.post(url, data=streaming_body())8081    assert response.status_code == 20082    assert response.text == "Hello, world!"838485def test_threaded_request_body():86    url = "https://example.org/echo_request_body"87    with Client(dispatch=MockDispatch()) as client:88        response = client.post(url, data=b"Hello, world!")8990    assert response.status_code == 20091    assert response.text == "Hello, world!"929394def test_threaded_request_body_streaming():95    url = "https://example.org/echo_request_body_streaming"96    with Client(dispatch=MockDispatch()) as client:97        response = client.post(url, data=b"Hello, world!")9899    assert response.status_code == 200100    assert response.text == "Hello, world!"101102103def test_dispatch_class():104    """105    Use a synchronous 'Dispatcher' class directly.106    """107    url = "https://example.org/"108    with MockDispatch() as dispatcher:109        response = dispatcher.request("GET", url)110111    assert response.status_code == 200112    assert response.json() == {"hello": "world"}