Skip to content
Myr

test_http2.py

This file

Path

tests/dispatch/test_http2.py

80 lines · 2.3 KiB · Python

Last changed by

Check for connection aliveness on pool re-acquiry (#111)Tom Christiebefore adoption025240c

History of this file

Take it

1import json23import pytest45from http3 import Client, Response67from .utils import MockHTTP2Backend8910def app(request):11    content = json.dumps(12        {13            "method": request.method,14            "path": request.url.path,15            "body": request.content.decode(),16        }17    ).encode()18    headers = {"Content-Length": str(len(content))}19    return Response(200, headers=headers, content=content)202122def test_http2_get_request():23    backend = MockHTTP2Backend(app=app)2425    with Client(backend=backend) as client:26        response = client.get("http://example.org")2728    assert response.status_code == 20029    assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}303132def test_http2_post_request():33    backend = MockHTTP2Backend(app=app)3435    with Client(backend=backend) as client:36        response = client.post("http://example.org", data=b"<data>")3738    assert response.status_code == 20039    assert json.loads(response.content) == {40        "method": "POST",41        "path": "/",42        "body": "<data>",43    }444546def test_http2_multiple_requests():47    backend = MockHTTP2Backend(app=app)4849    with Client(backend=backend) as client:50        response_1 = client.get("http://example.org/1")51        response_2 = client.get("http://example.org/2")52        response_3 = client.get("http://example.org/3")5354    assert response_1.status_code == 20055    assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}5657    assert response_2.status_code == 20058    assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}5960    assert response_3.status_code == 20061    assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}626364def test_http2_reconnect():65    """66    If a connection has been dropped between requests, then we should67    be seemlessly reconnected.68    """69    backend = MockHTTP2Backend(app=app)7071    with Client(backend=backend) as client:72        response_1 = client.get("http://example.org/1")73        backend.server.raise_disconnect = True74        response_2 = client.get("http://example.org/2")7576    assert response_1.status_code == 20077    assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}7879    assert response_2.status_code == 20080    assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}