test_http2.py
This file
Path
tests/dispatch/test_http2.py
139 lines · 4.5 KiB · Python
Last changed by
Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
History of this fileTake 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_multiple_requests():69 backend = MockHTTP2Backend(app=app)7071 with Client(backend=backend) as client:72 response_1 = client.get("http://example.org/1")73 response_2 = client.get("http://example.org/2")74 response_3 = client.get("http://example.org/3")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": ""}8182 assert response_3.status_code == 20083 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}848586async def test_async_http2_multiple_requests(backend):87 backend = MockHTTP2Backend(app=app, backend=backend)8889 async with AsyncClient(backend=backend) as client:90 response_1 = await client.get("http://example.org/1")91 response_2 = await client.get("http://example.org/2")92 response_3 = await client.get("http://example.org/3")9394 assert response_1.status_code == 20095 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}9697 assert response_2.status_code == 20098 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}99100 assert response_3.status_code == 200101 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}102103104def test_http2_reconnect():105 """106 If a connection has been dropped between requests, then we should107 be seemlessly reconnected.108 """109 backend = MockHTTP2Backend(app=app)110111 with Client(backend=backend) as client:112 response_1 = client.get("http://example.org/1")113 backend.server.close_connection = True114 response_2 = client.get("http://example.org/2")115116 assert response_1.status_code == 200117 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}118119 assert response_2.status_code == 200120 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}121122123async def test_async_http2_reconnect(backend):124 """125 If a connection has been dropped between requests, then we should126 be seemlessly reconnected.127 """128 backend = MockHTTP2Backend(app=app, backend=backend)129130 async with AsyncClient(backend=backend) as client:131 response_1 = await client.get("http://example.org/1")132 backend.server.close_connection = True133 response_2 = await client.get("http://example.org/2")134135 assert response_1.status_code == 200136 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}137138 assert response_2.status_code == 200139 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}