test_http2.py
This file
Path
tests/dispatch/test_http2.py
150 lines · 5.0 KiB · Python
Last changed by
Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
History of this fileTake it
1import json2import socket34import h2.connection5import h2.events6import pytest7from h2.settings import SettingCodes89from httpx import Client, Response, TimeoutException1011from .utils import MockHTTP2Backend121314def app(request):15 content = json.dumps(16 {17 "method": request.method,18 "path": request.url.path,19 "body": request.content.decode(),20 }21 ).encode()22 headers = {"Content-Length": str(len(content))}23 return Response(200, headers=headers, content=content)242526@pytest.mark.asyncio27async def test_http2_get_request():28 backend = MockHTTP2Backend(app=app)2930 async with Client(backend=backend, http2=True) as client:31 response = await client.get("http://example.org")3233 assert response.status_code == 20034 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}353637@pytest.mark.asyncio38async def test_http2_post_request():39 backend = MockHTTP2Backend(app=app)4041 async with Client(backend=backend, http2=True) as client:42 response = await client.post("http://example.org", data=b"<data>")4344 assert response.status_code == 20045 assert json.loads(response.content) == {46 "method": "POST",47 "path": "/",48 "body": "<data>",49 }505152@pytest.mark.asyncio53async def test_http2_large_post_request():54 backend = MockHTTP2Backend(app=app)5556 data = b"a" * 10000057 async with Client(backend=backend, http2=True) as client:58 response = await client.post("http://example.org", data=data)59 assert response.status_code == 20060 assert json.loads(response.content) == {61 "method": "POST",62 "path": "/",63 "body": data.decode(),64 }656667@pytest.mark.asyncio68async def test_http2_multiple_requests():69 backend = MockHTTP2Backend(app=app)7071 async with Client(backend=backend, http2=True) as client:72 response_1 = await client.get("http://example.org/1")73 response_2 = await client.get("http://example.org/2")74 response_3 = await 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": ""}848586@pytest.mark.asyncio87async def test_http2_reconnect():88 """89 If a connection has been dropped between requests, then we should90 be seemlessly reconnected.91 """92 backend = MockHTTP2Backend(app=app)9394 async with Client(backend=backend, http2=True) as client:95 response_1 = await client.get("http://example.org/1")96 backend.server.close_connection = True97 response_2 = await client.get("http://example.org/2")9899 assert response_1.status_code == 200100 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}101102 assert response_2.status_code == 200103 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}104105106async def test_http2_settings_in_handshake(backend):107 backend = MockHTTP2Backend(app=app)108109 async with Client(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]139140141async def test_http2_live_request(backend):142 async with Client(http2=True) as client:143 try:144 resp = await client.get("https://nghttp2.org/httpbin/anything")145 except TimeoutException:146 pytest.xfail(reason="nghttp2.org appears to be unresponsive")147 except socket.gaierror:148 pytest.xfail(reason="You appear to be offline")149 assert resp.status_code == 200150 assert resp.http_version == "HTTP/2"