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
The walk stopped after 200 commits: lines older than 53a3e55 point at it.
Update README.mdTom Christiebefore adoption53a3e55
lines 1–2why, one step back1import json2Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230
lines 3–6why, one step back3import h2.connection4import h2.events5from h2.settings import SettingCodes6Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 7–8why, one step back7from httpx import AsyncClient, Client, Response8Update README.mdTom Christiebefore adoption53a3e55
lines 9–33why, one step back9from .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": ""}3233Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 34–35why, one step back34async def test_async_http2_get_request(backend):35 backend = MockHTTP2Backend(app=app, backend=backend)Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 36–43why, one step back3637 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": ""}4243Update README.mdTom Christiebefore adoption53a3e55
lines 44–57why, one step back44def 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 }5657Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 58–59why, one step back58async def test_async_http2_post_request(backend):59 backend = MockHTTP2Backend(app=app, backend=backend)Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 60–71why, one step back6061 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 }7071Fix bug in calculating maximum frame size (#153)IsaacBenghiatbefore adoptionab9cd0e
lines 72–99why, one step back72def 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 }9899Update README.mdTom Christiebefore adoption53a3e55
lines 100–115why, one step back100def 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": ""}Check for connection aliveness on pool re-acquiry (#111)Tom Christiebefore adoption025240c
lines 116–117why, one step back116117Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 118–119why, one step back118async def test_async_http2_multiple_requests(backend):119 backend = MockHTTP2Backend(app=app, backend=backend)Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 120–135why, one step back120121 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": ""}134135Check for connection aliveness on pool re-acquiry (#111)Tom Christiebefore adoption025240c
lines 136–144why, one step back136def 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")Check disconnections on connection reacquiry (#145)Tom Christiebefore adoptionec365c0
line 145why, one step back145 backend.server.close_connection = TrueCheck for connection aliveness on pool re-acquiry (#111)Tom Christiebefore adoption025240c
line 146why, one step back146 response_2 = client.get("http://example.org/2")Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 147–154why, one step back147148 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": ""}153154Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 155why, one step back155async def test_async_http2_reconnect(backend):Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 156–159why, one step back156 """157 If a connection has been dropped between requests, then we should158 be seemlessly reconnected.159 """Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 160why, one step back160 backend = MockHTTP2Backend(app=app, backend=backend)Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 161–165why, one step back161162 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")Check for connection aliveness on pool re-acquiry (#111)Tom Christiebefore adoption025240c
lines 166–171why, one step back166167 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": ""}Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230
lines 172–206why, one step back172173174async 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]Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 207–214why, one step back207208209async 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"