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
The walk stopped after 200 commits: lines older than 0fa55bb point at it.
Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
line 1why, one step back1import jsonImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 2why, one step back2import socketAdd debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 3–5why, one step back34import h2.connection5import h2.eventsImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 6why, one step back6import pytestAdd debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 7–8why, one step back7from h2.settings import SettingCodes8Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 9why, one step back9from httpx import AsyncClient, Response, TimeoutExceptionAdd debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 10–13why, one step back1011from .utils import MockHTTP2Backend1213Added RequestContent (#636)Tom Christiebefore adoption8d4f182
lines 14–16why, one step back14async def app(request):15 method = request.method16 path = request.url.pathUse Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 17why, one step back17 body = b"".join([part async for part in request.stream])Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
line 18why, one step back18 content = json.dumps(Added RequestContent (#636)Tom Christiebefore adoption8d4f182
line 19why, one step back19 {"method": method, "path": path, "body": body.decode()}Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 20–21why, one step back20 ).encode()21 headers = {"Content-Length": str(len(content))}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 22why, one step back22 return Response(200, headers=headers, content=content, request=request)Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 23–24why, one step back2324Drop sync (#544)Tom Christiebefore adoption206c537
lines 25–26why, one step back25@pytest.mark.asyncio26async def test_http2_get_request():Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 27–28why, one step back27 backend = MockHTTP2Backend(app=app)28Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 29why, one step back29 async with AsyncClient(backend=backend, http2=True) as client:Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 30–35why, one step back30 response = await client.get("http://example.org")3132 assert response.status_code == 20033 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}3435Drop sync (#544)Tom Christiebefore adoption206c537
lines 36–37why, one step back36@pytest.mark.asyncio37async def test_http2_post_request():Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 38–39why, one step back38 backend = MockHTTP2Backend(app=app)39Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 40why, one step back40 async with AsyncClient(backend=backend, http2=True) as client:Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 41–50why, one step back41 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 }4950Drop sync (#544)Tom Christiebefore adoption206c537
lines 51–52why, one step back51@pytest.mark.asyncio52async def test_http2_large_post_request():Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 53–55why, one step back53 backend = MockHTTP2Backend(app=app)5455 data = b"a" * 100000Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 56why, one step back56 async with AsyncClient(backend=backend, http2=True) as client:Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 57–65why, one step back57 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 }6465Drop sync (#544)Tom Christiebefore adoption206c537
lines 66–67why, one step back66@pytest.mark.asyncio67async def test_http2_multiple_requests():Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 68–69why, one step back68 backend = MockHTTP2Backend(app=app)69Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 70why, one step back70 async with AsyncClient(backend=backend, http2=True) as client:Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 71–84why, one step back71 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": ""}8384Drop sync (#544)Tom Christiebefore adoption206c537
lines 85–86why, one step back85@pytest.mark.asyncio86async def test_http2_reconnect():Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 87–92why, one step back87 """88 If a connection has been dropped between requests, then we should89 be seemlessly reconnected.90 """91 backend = MockHTTP2Backend(app=app)92Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 93why, one step back93 async with AsyncClient(backend=backend, http2=True) as client:Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 94–104why, one step back94 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": ""}103104Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 105–106why, one step back105@pytest.mark.asyncio106async def test_http2_settings_in_handshake():Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 107why, one step back107 backend = MockHTTP2Backend(app=app)Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
line 108why, one step back108Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 109why, one step back109 async with AsyncClient(backend=backend, http2=True) as client:Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb
lines 110–138why, one step back110 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]Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 139–140why, one step back139140Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 141–142why, one step back141@pytest.mark.usefixtures("async_environment")142async def test_http2_live_request():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 143why, one step back143 async with AsyncClient(http2=True) as client:Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
lines 144–145why, one step back144 try:145 resp = await client.get("https://nghttp2.org/httpbin/anything")Bump coverage (#705)Tom Christiebefore adoptionb8394f1
line 146why, one step back146 except TimeoutException: # pragma: nocoverImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 147why, one step back147 pytest.xfail(reason="nghttp2.org appears to be unresponsive")Bump coverage (#705)Tom Christiebefore adoptionb8394f1
line 148why, one step back148 except socket.gaierror: # pragma: nocoverImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 149why, one step back149 pytest.xfail(reason="You appear to be offline")Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 150–151why, one step back150 assert resp.status_code == 200151 assert resp.http_version == "HTTP/2"