test_http2.py
This file
Path
tests/dispatch/test_http2.py
78 lines · 2.3 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
1import json23from httpx import 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": ""}282930def test_http2_post_request():31 backend = MockHTTP2Backend(app=app)3233 with Client(backend=backend) as client:34 response = client.post("http://example.org", data=b"<data>")3536 assert response.status_code == 20037 assert json.loads(response.content) == {38 "method": "POST",39 "path": "/",40 "body": "<data>",41 }424344def test_http2_multiple_requests():45 backend = MockHTTP2Backend(app=app)4647 with Client(backend=backend) as client:48 response_1 = client.get("http://example.org/1")49 response_2 = client.get("http://example.org/2")50 response_3 = client.get("http://example.org/3")5152 assert response_1.status_code == 20053 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}5455 assert response_2.status_code == 20056 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}5758 assert response_3.status_code == 20059 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}606162def test_http2_reconnect():63 """64 If a connection has been dropped between requests, then we should65 be seemlessly reconnected.66 """67 backend = MockHTTP2Backend(app=app)6869 with Client(backend=backend) as client:70 response_1 = client.get("http://example.org/1")71 backend.server.raise_disconnect = True72 response_2 = client.get("http://example.org/2")7374 assert response_1.status_code == 20075 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}7677 assert response_2.status_code == 20078 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}