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

The walk stopped after 200 commits: lines older than 444c6b8 point at it.

Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8

lines 1–2why, one step back
1import json
2

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 3why, one step back
3from httpx import (

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

line 4why, one step back
4 AsyncClient,

Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8

lines 5–42why, one step back
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 """

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

line 43why, one step back
43 Use a synchronous 'Dispatcher' class with the client.

Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8

lines 44–48why, one step back
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)

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 49–53why, one step back
49
50 assert response.status_code == 20051 assert response.json() == {"hello": "world"}5253

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 54why, one step back
54async def test_async_threaded_dispatch(backend):

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 55–59why, one step back
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/"

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 60why, one step back
60 async with AsyncClient(dispatch=MockDispatch(), backend=backend) as client:

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

line 61why, one step back
61 response = await client.get(url)

Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8

lines 62–104why, one step back
62
63 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 """

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

line 105why, one step back
105 Use a synchronous 'Dispatcher' class directly.

Honour 'local_flow_control_window' (#98)Tom Christiebefore adoption444c6b8

lines 106–112why, one step back
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"}