test_http2.py
This file
Path
tests/dispatch/test_http2.py
151 lines · 5.2 KiB · Python
Last changed by
Bump coverage (#705)Tom Christiebefore adoptionb8394f1
History of this fileTake it
1import json2import socket34import h2.connection5import h2.events6import pytest7from h2.settings import SettingCodes89from httpx import AsyncClient, Response, TimeoutException1011from .utils import MockHTTP2Backend121314async def app(request):15 method = request.method16 path = request.url.path17 body = b"".join([part async for part in request.stream])18 content = json.dumps(19 {"method": method, "path": path, "body": body.decode()}20 ).encode()21 headers = {"Content-Length": str(len(content))}22 return Response(200, headers=headers, content=content, request=request)232425@pytest.mark.asyncio26async def test_http2_get_request():27 backend = MockHTTP2Backend(app=app)2829 async with AsyncClient(backend=backend, http2=True) as client:30 response = await client.get("http://example.org")3132 assert response.status_code == 20033 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}343536@pytest.mark.asyncio37async def test_http2_post_request():38 backend = MockHTTP2Backend(app=app)3940 async with AsyncClient(backend=backend, http2=True) as client:41 response = await client.post("http://example.org", data=b"<data>")4243 assert response.status_code == 20044 assert json.loads(response.content) == {45 "method": "POST",46 "path": "/",47 "body": "<data>",48 }495051@pytest.mark.asyncio52async def test_http2_large_post_request():53 backend = MockHTTP2Backend(app=app)5455 data = b"a" * 10000056 async with AsyncClient(backend=backend, http2=True) as client:57 response = await client.post("http://example.org", data=data)58 assert response.status_code == 20059 assert json.loads(response.content) == {60 "method": "POST",61 "path": "/",62 "body": data.decode(),63 }646566@pytest.mark.asyncio67async def test_http2_multiple_requests():68 backend = MockHTTP2Backend(app=app)6970 async with AsyncClient(backend=backend, http2=True) as client:71 response_1 = await client.get("http://example.org/1")72 response_2 = await client.get("http://example.org/2")73 response_3 = await client.get("http://example.org/3")7475 assert response_1.status_code == 20076 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}7778 assert response_2.status_code == 20079 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}8081 assert response_3.status_code == 20082 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}838485@pytest.mark.asyncio86async def test_http2_reconnect():87 """88 If a connection has been dropped between requests, then we should89 be seemlessly reconnected.90 """91 backend = MockHTTP2Backend(app=app)9293 async with AsyncClient(backend=backend, http2=True) as client:94 response_1 = await client.get("http://example.org/1")95 backend.server.close_connection = True96 response_2 = await client.get("http://example.org/2")9798 assert response_1.status_code == 20099 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}100101 assert response_2.status_code == 200102 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}103104105@pytest.mark.asyncio106async def test_http2_settings_in_handshake():107 backend = MockHTTP2Backend(app=app)108109 async with AsyncClient(backend=backend, http2=True) as client:110 await client.get("http://example.org")111112 h2_conn = backend.server.conn113114 assert isinstance(h2_conn, h2.connection.H2Connection)115 expected_settings = {116 SettingCodes.HEADER_TABLE_SIZE: 4096,117 SettingCodes.ENABLE_PUSH: 0,118 SettingCodes.MAX_CONCURRENT_STREAMS: 100,119 SettingCodes.INITIAL_WINDOW_SIZE: 65535,120 SettingCodes.MAX_FRAME_SIZE: 16384,121 SettingCodes.MAX_HEADER_LIST_SIZE: 65536,122 # This one's here because h2 helpfully populates remote_settings123 # with default values even if the peer doesn't send the setting.124 SettingCodes.ENABLE_CONNECT_PROTOCOL: 0,125 }126 assert dict(h2_conn.remote_settings) == expected_settings127128 # We don't expect the ENABLE_CONNECT_PROTOCOL to be in the handshake129 expected_settings.pop(SettingCodes.ENABLE_CONNECT_PROTOCOL)130131 assert len(backend.server.settings_changed) == 1132 settings = backend.server.settings_changed[0]133134 assert isinstance(settings, h2.events.RemoteSettingsChanged)135 assert len(settings.changed_settings) == len(expected_settings)136 for setting_code, changed_setting in settings.changed_settings.items():137 assert isinstance(changed_setting, h2.settings.ChangedSetting)138 assert changed_setting.new_value == expected_settings[setting_code]139140141@pytest.mark.usefixtures("async_environment")142async def test_http2_live_request():143 async with AsyncClient(http2=True) as client:144 try:145 resp = await client.get("https://nghttp2.org/httpbin/anything")146 except TimeoutException: # pragma: nocover147 pytest.xfail(reason="nghttp2.org appears to be unresponsive")148 except socket.gaierror: # pragma: nocover149 pytest.xfail(reason="You appear to be offline")150 assert resp.status_code == 200151 assert resp.http_version == "HTTP/2"