test_threaded.py
This file
Path
tests/dispatch/test_threaded.py
98 lines · 2.8 KiB · Python
Last changed by
Start using flake8, fix lint issues (#126)Seth Michael Larsonbefore adoptionc1044ca
History of this fileTake it
Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 1–2why, one step back1import json2http3 (#86)Tom Christiebefore adoptionc9747aa
line 3why, one step back3from http3 import (Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 4–98why, one step back4 CertTypes,5 Client,6 Dispatcher,7 Request,8 Response,9 TimeoutTypes,10 VerifyTypes,11)121314def streaming_body():15 for part in [b"Hello", b", ", b"world!"]:16 yield part171819class MockDispatch(Dispatcher):20 def send(21 self,22 request: Request,23 verify: VerifyTypes = None,24 cert: CertTypes = None,25 timeout: TimeoutTypes = None,26 ) -> Response:27 if request.url.path == "/streaming_response":28 return Response(200, content=streaming_body(), request=request)29 elif request.url.path == "/echo_request_body":30 content = request.read()31 return Response(200, content=content, request=request)32 elif request.url.path == "/echo_request_body_streaming":33 content = b"".join([part for part in request.stream()])34 return Response(200, content=content, request=request)35 else:36 body = json.dumps({"hello": "world"}).encode()37 return Response(200, content=body, request=request)383940def test_threaded_dispatch():41 """42 Use a syncronous 'Dispatcher' class with the client.43 Calls to the dispatcher will end up running within a thread pool.44 """45 url = "https://example.org/"46 with Client(dispatch=MockDispatch()) as client:47 response = client.get(url)4849 assert response.status_code == 20050 assert response.json() == {"hello": "world"}515253def test_threaded_streaming_response():54 url = "https://example.org/streaming_response"55 with Client(dispatch=MockDispatch()) as client:56 response = client.get(url)5758 assert response.status_code == 20059 assert response.text == "Hello, world!"606162def test_threaded_streaming_request():63 url = "https://example.org/echo_request_body"64 with Client(dispatch=MockDispatch()) as client:65 response = client.post(url, data=streaming_body())6667 assert response.status_code == 20068 assert response.text == "Hello, world!"697071def test_threaded_request_body():72 url = "https://example.org/echo_request_body"73 with Client(dispatch=MockDispatch()) as client:74 response = client.post(url, data=b"Hello, world!")7576 assert response.status_code == 20077 assert response.text == "Hello, world!"787980def test_threaded_request_body_streaming():81 url = "https://example.org/echo_request_body_streaming"82 with Client(dispatch=MockDispatch()) as client:83 response = client.post(url, data=b"Hello, world!")8485 assert response.status_code == 20086 assert response.text == "Hello, world!"878889def test_dispatch_class():90 """91 Use a syncronous 'Dispatcher' class directly.92 """93 url = "https://example.org/"94 with MockDispatch() as dispatcher:95 response = dispatcher.request("GET", url)9697 assert response.status_code == 20098 assert response.json() == {"hello": "world"}