Add missing async test cases (#269)

This commit is contained in:
Florimond Manca 2019-08-23 14:04:21 +02:00 committed by Seth Michael Larson
parent 46402dd59f
commit a4b93b91c0
3 changed files with 116 additions and 2 deletions

View 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": ""}

View 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:

View 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/")