test_threaded.py
This file
Path
tests/dispatch/test_threaded.py
112 lines · 3.3 KiB · Python
Last changed by
Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
History of this fileTake it
Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 1–2why, one step back1import json2Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 3why, one step back3from httpx import (Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
line 4why, one step back4 AsyncClient,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 5–42why, one step back5 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 """Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
line 43why, one step back43 Use a synchronous 'Dispatcher' class with the client.Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 44–48why, one step back44 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)Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 49–53why, one step back4950 assert response.status_code == 20051 assert response.json() == {"hello": "world"}5253Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 54why, one step back54async def test_async_threaded_dispatch(backend):Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 55–59why, one step back55 """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/"Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 60why, one step back60 async with AsyncClient(dispatch=MockDispatch(), backend=backend) as client:Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
line 61why, one step back61 response = await client.get(url)Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 62–112why, one step back6263 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 syncronous '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"}