test_http2.py
This file
Path
tests/dispatch/test_http2.py
214 lines · 7.1 KiB · Python
Last changed by
Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
History of this fileTake it
1import json23import h2.connection4import h2.events5from h2.settings import SettingCodes67from httpx import AsyncClient, Client, Response89from .utils import MockHTTP2Backend101112def app(request):13 content = json.dumps(14 {15 "method": request.method,16 "path": request.url.path,17 "body": request.content.decode(),18 }19 ).encode()20 headers = {"Content-Length": str(len(content))}21 return Response(200, headers=headers, content=content)222324def test_http2_get_request():25 backend = MockHTTP2Backend(app=app)2627 with Client(backend=backend) as client:28 response = client.get("http://example.org")2930 assert response.status_code == 20031 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}323334async def test_async_http2_get_request(backend):35 backend = MockHTTP2Backend(app=app, backend=backend)3637 async with AsyncClient(backend=backend) as client:38 response = await client.get("http://example.org")3940 assert response.status_code == 20041 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}424344def test_http2_post_request():45 backend = MockHTTP2Backend(app=app)4647 with Client(backend=backend) as client:48 response = client.post("http://example.org", data=b"<data>")4950 assert response.status_code == 20051 assert json.loads(response.content) == {52 "method": "POST",53 "path": "/",54 "body": "<data>",55 }565758async def test_async_http2_post_request(backend):59 backend = MockHTTP2Backend(app=app, backend=backend)6061 async with AsyncClient(backend=backend) as client:62 response = await client.post("http://example.org", data=b"<data>")6364 assert response.status_code == 20065 assert json.loads(response.content) == {66 "method": "POST",67 "path": "/",68 "body": "<data>",69 }707172def test_http2_large_post_request():73 backend = MockHTTP2Backend(app=app)7475 data = b"a" * 10000076 with Client(backend=backend) as client:77 response = client.post("http://example.org", data=data)78 assert response.status_code == 20079 assert json.loads(response.content) == {80 "method": "POST",81 "path": "/",82 "body": data.decode(),83 }848586async def test_async_http2_large_post_request(backend):87 backend = MockHTTP2Backend(app=app, backend=backend)8889 data = b"a" * 10000090 async with AsyncClient(backend=backend) as client:91 response = await client.post("http://example.org", data=data)92 assert response.status_code == 20093 assert json.loads(response.content) == {94 "method": "POST",95 "path": "/",96 "body": data.decode(),97 }9899100def test_http2_multiple_requests():101 backend = MockHTTP2Backend(app=app)102103 with Client(backend=backend) as client:104 response_1 = client.get("http://example.org/1")105 response_2 = client.get("http://example.org/2")106 response_3 = client.get("http://example.org/3")107108 assert response_1.status_code == 200109 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}110111 assert response_2.status_code == 200112 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}113114 assert response_3.status_code == 200115 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}116117118async def test_async_http2_multiple_requests(backend):119 backend = MockHTTP2Backend(app=app, backend=backend)120121 async with AsyncClient(backend=backend) as client:122 response_1 = await client.get("http://example.org/1")123 response_2 = await client.get("http://example.org/2")124 response_3 = await client.get("http://example.org/3")125126 assert response_1.status_code == 200127 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}128129 assert response_2.status_code == 200130 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}131132 assert response_3.status_code == 200133 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}134135136def test_http2_reconnect():137 """138 If a connection has been dropped between requests, then we should139 be seemlessly reconnected.140 """141 backend = MockHTTP2Backend(app=app)142143 with Client(backend=backend) as client:144 response_1 = client.get("http://example.org/1")145 backend.server.close_connection = True146 response_2 = client.get("http://example.org/2")147148 assert response_1.status_code == 200149 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}150151 assert response_2.status_code == 200152 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}153154155async def test_async_http2_reconnect(backend):156 """157 If a connection has been dropped between requests, then we should158 be seemlessly reconnected.159 """160 backend = MockHTTP2Backend(app=app, backend=backend)161162 async with AsyncClient(backend=backend) as client:163 response_1 = await client.get("http://example.org/1")164 backend.server.close_connection = True165 response_2 = await client.get("http://example.org/2")166167 assert response_1.status_code == 200168 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}169170 assert response_2.status_code == 200171 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}172173174async def test_http2_settings_in_handshake(backend):175 backend = MockHTTP2Backend(app=app, backend=backend)176177 async with AsyncClient(backend=backend) as client:178 await client.get("http://example.org")179180 h2_conn = backend.server.conn181182 assert isinstance(h2_conn, h2.connection.H2Connection)183 expected_settings = {184 SettingCodes.HEADER_TABLE_SIZE: 4096,185 SettingCodes.ENABLE_PUSH: 0,186 SettingCodes.MAX_CONCURRENT_STREAMS: 100,187 SettingCodes.INITIAL_WINDOW_SIZE: 65535,188 SettingCodes.MAX_FRAME_SIZE: 16384,189 SettingCodes.MAX_HEADER_LIST_SIZE: 65536,190 # This one's here because h2 helpfully populates remote_settings191 # with default values even if the peer doesn't send the setting.192 SettingCodes.ENABLE_CONNECT_PROTOCOL: 0,193 }194 assert dict(h2_conn.remote_settings) == expected_settings195196 # We don't expect the ENABLE_CONNECT_PROTOCOL to be in the handshake197 expected_settings.pop(SettingCodes.ENABLE_CONNECT_PROTOCOL)198199 assert len(backend.server.settings_changed) == 1200 settings = backend.server.settings_changed[0]201202 assert isinstance(settings, h2.events.RemoteSettingsChanged)203 assert len(settings.changed_settings) == len(expected_settings)204 for setting_code, changed_setting in settings.changed_settings.items():205 assert isinstance(changed_setting, h2.settings.ChangedSetting)206 assert changed_setting.new_value == expected_settings[setting_code]207208209async def test_http2_live_request(backend):210 async with AsyncClient(backend=backend) as client:211 resp = await client.get("https://nghttp2.org/httpbin/anything")212213 assert resp.status_code == 200214 assert resp.http_version == "HTTP/2"