test_threaded.py
This file
Path
tests/dispatch/test_threaded.py
115 lines · 3.3 KiB · Python
Last changed by
Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
History of this fileTake it
Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 1–2why, one step back1import json2Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 3–4why, one step back3import pytest4Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 5why, one step back5from httpx import (Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
line 6why, one step back6 AsyncClient,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 7–44why, one step back7 CertTypes,8 Client,9 Dispatcher,10 Request,11 Response,12 TimeoutTypes,13 VerifyTypes,14)151617def streaming_body():18 for part in [b"Hello", b", ", b"world!"]:19 yield part202122class MockDispatch(Dispatcher):23 def send(24 self,25 request: Request,26 verify: VerifyTypes = None,27 cert: CertTypes = None,28 timeout: TimeoutTypes = None,29 ) -> Response:30 if request.url.path == "/streaming_response":31 return Response(200, content=streaming_body(), request=request)32 elif request.url.path == "/echo_request_body":33 content = request.read()34 return Response(200, content=content, request=request)35 elif request.url.path == "/echo_request_body_streaming":36 content = b"".join([part for part in request.stream()])37 return Response(200, content=content, request=request)38 else:39 body = json.dumps({"hello": "world"}).encode()40 return Response(200, content=body, request=request)414243def test_threaded_dispatch():44 """Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
line 45why, one step back45 Use a synchronous 'Dispatcher' class with the client.Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 46–50why, one step back46 Calls to the dispatcher will end up running within a thread pool.47 """48 url = "https://example.org/"49 with Client(dispatch=MockDispatch()) as client:50 response = client.get(url)Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 51–64why, one step back5152 assert response.status_code == 20053 assert response.json() == {"hello": "world"}545556@pytest.mark.asyncio57async def test_async_threaded_dispatch():58 """59 Use a synchronous 'Dispatcher' class with the async client.60 Calls to the dispatcher will end up running within a thread pool.61 """62 url = "https://example.org/"63 async with AsyncClient(dispatch=MockDispatch()) as client:64 response = await client.get(url)Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 65–115why, one step back6566 assert response.status_code == 20067 assert response.json() == {"hello": "world"}686970def test_threaded_streaming_response():71 url = "https://example.org/streaming_response"72 with Client(dispatch=MockDispatch()) as client:73 response = client.get(url)7475 assert response.status_code == 20076 assert response.text == "Hello, world!"777879def test_threaded_streaming_request():80 url = "https://example.org/echo_request_body"81 with Client(dispatch=MockDispatch()) as client:82 response = client.post(url, data=streaming_body())8384 assert response.status_code == 20085 assert response.text == "Hello, world!"868788def test_threaded_request_body():89 url = "https://example.org/echo_request_body"90 with Client(dispatch=MockDispatch()) as client:91 response = client.post(url, data=b"Hello, world!")9293 assert response.status_code == 20094 assert response.text == "Hello, world!"959697def test_threaded_request_body_streaming():98 url = "https://example.org/echo_request_body_streaming"99 with Client(dispatch=MockDispatch()) as client:100 response = client.post(url, data=b"Hello, world!")101102 assert response.status_code == 200103 assert response.text == "Hello, world!"104105106def test_dispatch_class():107 """108 Use a syncronous 'Dispatcher' class directly.109 """110 url = "https://example.org/"111 with MockDispatch() as dispatcher:112 response = dispatcher.request("GET", url)113114 assert response.status_code == 200115 assert response.json() == {"hello": "world"}