Add missing async test cases (#269)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Florimond Manca · committed by Seth Michael Larson · parent 46402dd · Browse files · 3 files·+116 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+116 added−2 removed
- tests:+116 added−2 removed
Provenance
- Commit
a4b93b9- Tree
02ac346- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
46402dd
3 files changed+116 added−2 removed
modified:tests/dispatch/test_http2.py+68 added-1 removed
1 import json2 3-from httpx import Client, Response3+import pytest4 5+from httpx import AsyncClient, Client, Response6+7 from .utils import MockHTTP2Backend8 9 10 def app(request):17 unmodified lines28 assert response.status_code == 20029 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}30 31 32+@pytest.mark.asyncio33+async def test_async_http2_get_request():34+ backend = MockHTTP2Backend(app=app)35+36+ async with AsyncClient(backend=backend) as client:37+ response = await client.get("http://example.org")38+39+ assert response.status_code == 20040+ assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}41+42+43 def test_http2_post_request():44 backend = MockHTTP2Backend(app=app)45 46 with Client(backend=backend) as client:6 unmodified lines53 "body": "<data>",54 }55 56 57+@pytest.mark.asyncio58+async def test_async_http2_post_request():59+ backend = MockHTTP2Backend(app=app)60+61+ async with AsyncClient(backend=backend) as client:62+ response = await client.post("http://example.org", data=b"<data>")63+64+ assert response.status_code == 20065+ assert json.loads(response.content) == {66+ "method": "POST",67+ "path": "/",68+ "body": "<data>",69+ }70+71+72 def test_http2_multiple_requests():73 backend = MockHTTP2Backend(app=app)74 75 with Client(backend=backend) as client:10 unmodified lines86 assert response_3.status_code == 20087 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}88 89 90+@pytest.mark.asyncio91+async def test_async_http2_multiple_requests():92+ backend = MockHTTP2Backend(app=app)93+94+ async with AsyncClient(backend=backend) as client:95+ response_1 = await client.get("http://example.org/1")96+ response_2 = await client.get("http://example.org/2")97+ response_3 = await client.get("http://example.org/3")98+99+ assert response_1.status_code == 200100+ assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}101+102+ assert response_2.status_code == 200103+ assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}104+105+ assert response_3.status_code == 200106+ assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}107+108+109 def test_http2_reconnect():110 """111 If a connection has been dropped between requests, then we should112 be seemlessly reconnected.9 unmodified lines122 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}123 124 assert response_2.status_code == 200125 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}126+127+128+@pytest.mark.asyncio129+async def test_async_http2_reconnect():130+ """131+ If a connection has been dropped between requests, then we should132+ be seemlessly reconnected.133+ """134+ backend = MockHTTP2Backend(app=app)135+136+ async with AsyncClient(backend=backend) as client:137+ response_1 = await client.get("http://example.org/1")138+ backend.server.close_connection = True139+ response_2 = await client.get("http://example.org/2")140+141+ assert response_1.status_code == 200142+ assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}143+144+ assert response_2.status_code == 200145+ assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}modified:tests/dispatch/test_threaded.py+18 added-1 removed
1 import json2 3+import pytest4+5 from httpx import (6+ AsyncClient,7 CertTypes,8 Client,9 Dispatcher,10 Request,30 unmodified lines41 42 43 def test_threaded_dispatch():44 """42- Use a syncronous 'Dispatcher' class with the client.45+ Use a synchronous 'Dispatcher' class with the client.46 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:2 unmodified lines52 assert response.status_code == 20053 assert response.json() == {"hello": "world"}54 55 56+@pytest.mark.asyncio57+async 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)65+66+ assert response.status_code == 20067+ assert response.json() == {"hello": "world"}68+69+70 def test_threaded_streaming_response():71 url = "https://example.org/streaming_response"72 with Client(dispatch=MockDispatch()) as client:73 response = client.get(url)42 unmodified linesmodified:tests/test_asgi.py+30 added
44 unmodified lines45 assert response.status_code == 20046 assert response.text == "Hello, World!"47 48 49+@pytest.mark.asyncio50+async def test_asgi_async():51+ client = httpx.AsyncClient(app=hello_world)52+ response = await client.get("http://www.example.org/")53+ assert response.status_code == 20054+ assert response.text == "Hello, World!"55+56+57 def test_asgi_upload():58 client = httpx.Client(app=echo_body)59 response = client.post("http://www.example.org/", data=b"example")60 assert response.status_code == 20061 assert response.text == "example"62 63 64+@pytest.mark.asyncio65+async def test_asgi_upload_async():66+ client = httpx.AsyncClient(app=echo_body)67+ response = await client.post("http://www.example.org/", data=b"example")68+ assert response.status_code == 20069+ assert response.text == "example"70+71+72 def test_asgi_exc():73 client = httpx.Client(app=raise_exc)74 with pytest.raises(ValueError):75 client.get("http://www.example.org/")76 77 78+@pytest.mark.asyncio79+async def test_asgi_exc_async():80+ client = httpx.AsyncClient(app=raise_exc)81+ with pytest.raises(ValueError):82+ await client.get("http://www.example.org/")83+84+85 def test_asgi_exc_after_response():86 client = httpx.Client(app=raise_exc_after_response)87 with pytest.raises(ValueError):88 client.get("http://www.example.org/")89+90+91+@pytest.mark.asyncio92+async def test_asgi_exc_after_response_async():93+ client = httpx.AsyncClient(app=raise_exc_after_response)94+ with pytest.raises(ValueError):95+ await client.get("http://www.example.org/")