]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add missing async test cases (#269)
authorFlorimond Manca <florimond.manca@gmail.com>
Fri, 23 Aug 2019 12:04:21 +0000 (14:04 +0200)
committerSeth Michael Larson <sethmichaellarson@gmail.com>
Fri, 23 Aug 2019 12:04:21 +0000 (07:04 -0500)
tests/dispatch/test_http2.py
tests/dispatch/test_threaded.py
tests/test_asgi.py

index 99caab6c8e6b6f5e6dcf9e72dc49348488349eac..30332dd90185122c44b1e52e600ea778e55c712a 100644 (file)
@@ -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"<data>")
+
+    assert response.status_code == 200
+    assert json.loads(response.content) == {
+        "method": "POST",
+        "path": "/",
+        "body": "<data>",
+    }
+
+
 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": ""}
index ac90bdd2d6ba861b6f0833a014e6267fd8443774..8d70eae619edc18a0392aa6558bd8848b94e5cae 100644 (file)
@@ -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:
index ca3d02ddf3ea2e2254449ee2c798e3eb5f03d74d..3ee7631367ad7e0e8beb53362ee4df2743e8b34c 100644 (file)
@@ -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/")