From: Florimond Manca Date: Fri, 23 Aug 2019 12:04:21 +0000 (+0200) Subject: Add missing async test cases (#269) X-Git-Tag: 0.7.2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4b93b91c03239738f6c118570fdfa155e7fe11e;p=thirdparty%2Fhttpx.git Add missing async test cases (#269) --- diff --git a/tests/dispatch/test_http2.py b/tests/dispatch/test_http2.py index 99caab6c..30332dd9 100644 --- a/tests/dispatch/test_http2.py +++ b/tests/dispatch/test_http2.py @@ -1,6 +1,8 @@ import json -from httpx import Client, Response +import pytest + +from httpx import AsyncClient, Client, Response from .utils import MockHTTP2Backend @@ -27,6 +29,17 @@ def test_http2_get_request(): assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""} +@pytest.mark.asyncio +async def test_async_http2_get_request(): + backend = MockHTTP2Backend(app=app) + + async with AsyncClient(backend=backend) as client: + response = await client.get("http://example.org") + + assert response.status_code == 200 + assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""} + + def test_http2_post_request(): backend = MockHTTP2Backend(app=app) @@ -41,6 +54,21 @@ def test_http2_post_request(): } +@pytest.mark.asyncio +async def test_async_http2_post_request(): + backend = MockHTTP2Backend(app=app) + + async with AsyncClient(backend=backend) as client: + response = await client.post("http://example.org", data=b"") + + assert response.status_code == 200 + assert json.loads(response.content) == { + "method": "POST", + "path": "/", + "body": "", + } + + def test_http2_multiple_requests(): backend = MockHTTP2Backend(app=app) @@ -59,6 +87,25 @@ def test_http2_multiple_requests(): assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""} +@pytest.mark.asyncio +async def test_async_http2_multiple_requests(): + backend = MockHTTP2Backend(app=app) + + async with AsyncClient(backend=backend) as client: + response_1 = await client.get("http://example.org/1") + response_2 = await client.get("http://example.org/2") + response_3 = await client.get("http://example.org/3") + + assert response_1.status_code == 200 + assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""} + + assert response_2.status_code == 200 + assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""} + + assert response_3.status_code == 200 + assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""} + + def test_http2_reconnect(): """ If a connection has been dropped between requests, then we should @@ -76,3 +123,23 @@ def test_http2_reconnect(): assert response_2.status_code == 200 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""} + + +@pytest.mark.asyncio +async def test_async_http2_reconnect(): + """ + If a connection has been dropped between requests, then we should + be seemlessly reconnected. + """ + backend = MockHTTP2Backend(app=app) + + async with AsyncClient(backend=backend) as client: + response_1 = await client.get("http://example.org/1") + backend.server.close_connection = True + response_2 = await client.get("http://example.org/2") + + assert response_1.status_code == 200 + assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""} + + assert response_2.status_code == 200 + assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""} diff --git a/tests/dispatch/test_threaded.py b/tests/dispatch/test_threaded.py index ac90bdd2..8d70eae6 100644 --- a/tests/dispatch/test_threaded.py +++ b/tests/dispatch/test_threaded.py @@ -1,6 +1,9 @@ import json +import pytest + from httpx import ( + AsyncClient, CertTypes, Client, Dispatcher, @@ -39,7 +42,7 @@ class MockDispatch(Dispatcher): def test_threaded_dispatch(): """ - Use a syncronous 'Dispatcher' class with the client. + Use a synchronous 'Dispatcher' class with the client. Calls to the dispatcher will end up running within a thread pool. """ url = "https://example.org/" @@ -50,6 +53,20 @@ def test_threaded_dispatch(): assert response.json() == {"hello": "world"} +@pytest.mark.asyncio +async def test_async_threaded_dispatch(): + """ + Use a synchronous 'Dispatcher' class with the async client. + Calls to the dispatcher will end up running within a thread pool. + """ + url = "https://example.org/" + async with AsyncClient(dispatch=MockDispatch()) as client: + response = await client.get(url) + + assert response.status_code == 200 + assert response.json() == {"hello": "world"} + + def test_threaded_streaming_response(): url = "https://example.org/streaming_response" with Client(dispatch=MockDispatch()) as client: diff --git a/tests/test_asgi.py b/tests/test_asgi.py index ca3d02dd..3ee76313 100644 --- a/tests/test_asgi.py +++ b/tests/test_asgi.py @@ -46,6 +46,14 @@ def test_asgi(): assert response.text == "Hello, World!" +@pytest.mark.asyncio +async def test_asgi_async(): + client = httpx.AsyncClient(app=hello_world) + response = await client.get("http://www.example.org/") + assert response.status_code == 200 + assert response.text == "Hello, World!" + + def test_asgi_upload(): client = httpx.Client(app=echo_body) response = client.post("http://www.example.org/", data=b"example") @@ -53,13 +61,35 @@ def test_asgi_upload(): assert response.text == "example" +@pytest.mark.asyncio +async def test_asgi_upload_async(): + client = httpx.AsyncClient(app=echo_body) + response = await client.post("http://www.example.org/", data=b"example") + assert response.status_code == 200 + assert response.text == "example" + + def test_asgi_exc(): client = httpx.Client(app=raise_exc) with pytest.raises(ValueError): client.get("http://www.example.org/") +@pytest.mark.asyncio +async def test_asgi_exc_async(): + client = httpx.AsyncClient(app=raise_exc) + with pytest.raises(ValueError): + await client.get("http://www.example.org/") + + def test_asgi_exc_after_response(): client = httpx.Client(app=raise_exc_after_response) with pytest.raises(ValueError): client.get("http://www.example.org/") + + +@pytest.mark.asyncio +async def test_asgi_exc_after_response_async(): + client = httpx.AsyncClient(app=raise_exc_after_response) + with pytest.raises(ValueError): + await client.get("http://www.example.org/")