test_http2.py
This file
Path
tests/dispatch/test_http2.py
149 lines · 5.0 KiB · Python
Last changed by
Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
History of this fileTake it
The walk stopped after 200 commits: lines older than 994403b point at it.
Serve test server in thread (#292)Florimond Mancabefore adoption994403b
line 1why, one step back1import jsonImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 2why, one step back2import socketServe test server in thread (#292)Florimond Mancabefore adoption994403b
line 3why, one step back3Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230
lines 4–5why, one step back4import h2.connection5import h2.eventsImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 6why, one step back6import pytestRemove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230
lines 7–8why, one step back7from h2.settings import SettingCodes8Fix Timeout -> TimeoutException in test case (#598)Tom Christiebefore adoptiondad3797
line 9why, one step back9from httpx import Client, Response, TimeoutExceptionServe test server in thread (#292)Florimond Mancabefore adoption994403b
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])Serve test server in thread (#292)Florimond Mancabefore adoption994403b
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()}Serve test server in thread (#292)Florimond Mancabefore adoption994403b
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)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
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():Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 27–28why, one step back27 backend = MockHTTP2Backend(app=app)28Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 29why, one step back29 async with Client(backend=backend, http2=True) as client:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
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():Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 38–39why, one step back38 backend = MockHTTP2Backend(app=app)39Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 40why, one step back40 async with Client(backend=backend, http2=True) as client:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
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():Fix bug in calculating maximum frame size (#153)IsaacBenghiatbefore adoptionab9cd0e
lines 53–55why, one step back53 backend = MockHTTP2Backend(app=app)5455 data = b"a" * 100000Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 56why, one step back56 async with Client(backend=backend, http2=True) as client:Fix bug in calculating maximum frame size (#153)IsaacBenghiatbefore adoptionab9cd0e
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():Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 68–69why, one step back68 backend = MockHTTP2Backend(app=app)69Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 70why, one step back70 async with Client(backend=backend, http2=True) as client:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
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():Serve test server in thread (#292)Florimond Mancabefore adoption994403b
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 http2 switch (#586)Tom Christiebefore adoption5076952
line 93why, one step back93 async with Client(backend=backend, http2=True) as client:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 94–102why, 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": ""}Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230
lines 103–105why, one step back103104105async def test_http2_settings_in_handshake(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 106why, one step back106 backend = MockHTTP2Backend(app=app)Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230
line 107why, one step back107Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 108why, one step back108 async with Client(backend=backend, http2=True) as client:Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230
lines 109–137why, one step back109 await client.get("http://example.org")110111 h2_conn = backend.server.conn112113 assert isinstance(h2_conn, h2.connection.H2Connection)114 expected_settings = {115 SettingCodes.HEADER_TABLE_SIZE: 4096,116 SettingCodes.ENABLE_PUSH: 0,117 SettingCodes.MAX_CONCURRENT_STREAMS: 100,118 SettingCodes.INITIAL_WINDOW_SIZE: 65535,119 SettingCodes.MAX_FRAME_SIZE: 16384,120 SettingCodes.MAX_HEADER_LIST_SIZE: 65536,121 # This one's here because h2 helpfully populates remote_settings122 # with default values even if the peer doesn't send the setting.123 SettingCodes.ENABLE_CONNECT_PROTOCOL: 0,124 }125 assert dict(h2_conn.remote_settings) == expected_settings126127 # We don't expect the ENABLE_CONNECT_PROTOCOL to be in the handshake128 expected_settings.pop(SettingCodes.ENABLE_CONNECT_PROTOCOL)129130 assert len(backend.server.settings_changed) == 1131 settings = backend.server.settings_changed[0]132133 assert isinstance(settings, h2.events.RemoteSettingsChanged)134 assert len(settings.changed_settings) == len(expected_settings)135 for setting_code, changed_setting in settings.changed_settings.items():136 assert isinstance(changed_setting, h2.settings.ChangedSetting)137 assert changed_setting.new_value == expected_settings[setting_code]Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 138–140why, one step back138139140async def test_http2_live_request(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 141why, one step back141 async with Client(http2=True) as client:Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
lines 142–143why, one step back142 try:143 resp = await client.get("https://nghttp2.org/httpbin/anything")Fix Timeout -> TimeoutException in test case (#598)Tom Christiebefore adoptiondad3797
line 144why, one step back144 except TimeoutException:Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
lines 145–147why, one step back145 pytest.xfail(reason="nghttp2.org appears to be unresponsive")146 except socket.gaierror:147 pytest.xfail(reason="You appear to be offline")Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 148–149why, one step back148 assert resp.status_code == 200149 assert resp.http_version == "HTTP/2"