import json
-from httpx import Client, Response
+import pytest
+
+from httpx import AsyncClient, Client, Response
from .utils import MockHTTP2Backend
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)
}
+@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)
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
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": ""}
import json
+import pytest
+
from httpx import (
+ AsyncClient,
CertTypes,
Client,
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/"
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:
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")
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/")