Skip to content
Myr

test_http2.py

This file

Path

tests/dispatch/test_http2.py

167 lines · 5.3 KiB · Python

Last changed by

Fix bug in calculating maximum frame size (#153)IsaacBenghiatbefore adoptionab9cd0e

History of this file

Take it

1import json23from httpx import AsyncClient, Client, Response45from .utils import MockHTTP2Backend678def app(request):9    content = json.dumps(10        {11            "method": request.method,12            "path": request.url.path,13            "body": request.content.decode(),14        }15    ).encode()16    headers = {"Content-Length": str(len(content))}17    return Response(200, headers=headers, content=content)181920def test_http2_get_request():21    backend = MockHTTP2Backend(app=app)2223    with Client(backend=backend) as client:24        response = client.get("http://example.org")2526    assert response.status_code == 20027    assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}282930async def test_async_http2_get_request(backend):31    backend = MockHTTP2Backend(app=app, backend=backend)3233    async with AsyncClient(backend=backend) as client:34        response = await client.get("http://example.org")3536    assert response.status_code == 20037    assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}383940def test_http2_post_request():41    backend = MockHTTP2Backend(app=app)4243    with Client(backend=backend) as client:44        response = client.post("http://example.org", data=b"<data>")4546    assert response.status_code == 20047    assert json.loads(response.content) == {48        "method": "POST",49        "path": "/",50        "body": "<data>",51    }525354async def test_async_http2_post_request(backend):55    backend = MockHTTP2Backend(app=app, backend=backend)5657    async with AsyncClient(backend=backend) as client:58        response = await client.post("http://example.org", data=b"<data>")5960    assert response.status_code == 20061    assert json.loads(response.content) == {62        "method": "POST",63        "path": "/",64        "body": "<data>",65    }666768def test_http2_large_post_request():69    backend = MockHTTP2Backend(app=app)7071    data = b"a" * 10000072    with Client(backend=backend) as client:73        response = client.post("http://example.org", data=data)74    assert response.status_code == 20075    assert json.loads(response.content) == {76        "method": "POST",77        "path": "/",78        "body": data.decode(),79    }808182async def test_async_http2_large_post_request(backend):83    backend = MockHTTP2Backend(app=app, backend=backend)8485    data = b"a" * 10000086    async with AsyncClient(backend=backend) as client:87        response = await client.post("http://example.org", data=data)88    assert response.status_code == 20089    assert json.loads(response.content) == {90        "method": "POST",91        "path": "/",92        "body": data.decode(),93    }949596def test_http2_multiple_requests():97    backend = MockHTTP2Backend(app=app)9899    with Client(backend=backend) as client:100        response_1 = client.get("http://example.org/1")101        response_2 = client.get("http://example.org/2")102        response_3 = client.get("http://example.org/3")103104    assert response_1.status_code == 200105    assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}106107    assert response_2.status_code == 200108    assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}109110    assert response_3.status_code == 200111    assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}112113114async def test_async_http2_multiple_requests(backend):115    backend = MockHTTP2Backend(app=app, backend=backend)116117    async with AsyncClient(backend=backend) as client:118        response_1 = await client.get("http://example.org/1")119        response_2 = await client.get("http://example.org/2")120        response_3 = await client.get("http://example.org/3")121122    assert response_1.status_code == 200123    assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}124125    assert response_2.status_code == 200126    assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}127128    assert response_3.status_code == 200129    assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}130131132def test_http2_reconnect():133    """134    If a connection has been dropped between requests, then we should135    be seemlessly reconnected.136    """137    backend = MockHTTP2Backend(app=app)138139    with Client(backend=backend) as client:140        response_1 = client.get("http://example.org/1")141        backend.server.close_connection = True142        response_2 = client.get("http://example.org/2")143144    assert response_1.status_code == 200145    assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}146147    assert response_2.status_code == 200148    assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}149150151async def test_async_http2_reconnect(backend):152    """153    If a connection has been dropped between requests, then we should154    be seemlessly reconnected.155    """156    backend = MockHTTP2Backend(app=app, backend=backend)157158    async with AsyncClient(backend=backend) as client:159        response_1 = await client.get("http://example.org/1")160        backend.server.close_connection = True161        response_2 = await client.get("http://example.org/2")162163    assert response_1.status_code == 200164    assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}165166    assert response_2.status_code == 200167    assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}