test_http2.py
This file
Path
tests/dispatch/test_http2.py
158 lines · 5.5 KiB · Python
Last changed by
Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
History of this fileTake it
The walk stopped after 200 commits: lines older than 5e4f54d point at it.
Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 1why, one step back1import jsonImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 2why, one step back2import socketAdd test script (#451)Florimond Mancabefore adoption5e4f54d
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 test script (#451)Florimond Mancabefore adoption5e4f54d
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, TimeoutExceptionDrop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 10why, one step back10from httpx._dispatch.connection_pool import ConnectionPoolAdd test script (#451)Florimond Mancabefore adoption5e4f54d
lines 11–14why, one step back1112from .utils import MockHTTP2Backend1314Added RequestContent (#636)Tom Christiebefore adoption8d4f182
lines 15–17why, one step back15async def app(request):16 method = request.method17 path = request.url.pathUse Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 18why, one step back18 body = b"".join([part async for part in request.stream])Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 19why, one step back19 content = json.dumps(Added RequestContent (#636)Tom Christiebefore adoption8d4f182
line 20why, one step back20 {"method": method, "path": path, "body": body.decode()}Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 21–22why, one step back21 ).encode()22 headers = {"Content-Length": str(len(content))}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 23why, one step back23 return Response(200, headers=headers, content=content, request=request)Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 24–25why, one step back2425Drop sync (#544)Tom Christiebefore adoption206c537
lines 26–27why, one step back26@pytest.mark.asyncio27async def test_http2_get_request():Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 28why, one step back28 backend = MockHTTP2Backend(app=app)Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 29why, one step back29 dispatch = ConnectionPool(backend=backend, http2=True)Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 30why, one step back30Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 31why, one step back31 async with AsyncClient(dispatch=dispatch) as client:Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 32–37why, one step back32 response = await client.get("http://example.org")3334 assert response.status_code == 20035 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}3637Drop sync (#544)Tom Christiebefore adoption206c537
lines 38–39why, one step back38@pytest.mark.asyncio39async def test_http2_post_request():Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 40why, one step back40 backend = MockHTTP2Backend(app=app)Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 41why, one step back41 dispatch = ConnectionPool(backend=backend, http2=True)Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 42why, one step back42Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 43why, one step back43 async with AsyncClient(dispatch=dispatch) as client:Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 44–53why, one step back44 response = await client.post("http://example.org", data=b"<data>")4546 assert response.status_code == 20047 assert json.loads(response.content) == {48 "method": "POST",49 "path": "/",50 "body": "<data>",51 }5253Drop sync (#544)Tom Christiebefore adoption206c537
lines 54–55why, one step back54@pytest.mark.asyncio55async def test_http2_large_post_request():Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 56why, one step back56 backend = MockHTTP2Backend(app=app)Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 57why, one step back57 dispatch = ConnectionPool(backend=backend, http2=True)Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 58–59why, one step back5859 data = b"a" * 100000Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 60why, one step back60 async with AsyncClient(dispatch=dispatch) as client:Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 61–69why, one step back61 response = await client.post("http://example.org", data=data)62 assert response.status_code == 20063 assert json.loads(response.content) == {64 "method": "POST",65 "path": "/",66 "body": data.decode(),67 }6869Drop sync (#544)Tom Christiebefore adoption206c537
lines 70–71why, one step back70@pytest.mark.asyncio71async def test_http2_multiple_requests():Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 72why, one step back72 backend = MockHTTP2Backend(app=app)Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 73why, one step back73 dispatch = ConnectionPool(backend=backend, http2=True)Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 74why, one step back74Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 75why, one step back75 async with AsyncClient(dispatch=dispatch) as client:Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 76–89why, one step back76 response_1 = await client.get("http://example.org/1")77 response_2 = await client.get("http://example.org/2")78 response_3 = await client.get("http://example.org/3")7980 assert response_1.status_code == 20081 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}8283 assert response_2.status_code == 20084 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}8586 assert response_3.status_code == 20087 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}8889Drop sync (#544)Tom Christiebefore adoption206c537
lines 90–91why, one step back90@pytest.mark.asyncio91async def test_http2_reconnect():Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 92–96why, one step back92 """93 If a connection has been dropped between requests, then we should94 be seemlessly reconnected.95 """96 backend = MockHTTP2Backend(app=app)Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 97why, one step back97 dispatch = ConnectionPool(backend=backend, http2=True)Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 98why, one step back98Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 99why, one step back99 async with AsyncClient(dispatch=dispatch) as client:Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 100–110why, one step back100 response_1 = await client.get("http://example.org/1")101 backend.server.close_connection = True102 response_2 = await client.get("http://example.org/2")103104 assert response_1.status_code == 200105 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}106107 assert response_2.status_code == 200108 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}109110Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 111–112why, one step back111@pytest.mark.asyncio112async def test_http2_settings_in_handshake():Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 113why, one step back113 backend = MockHTTP2Backend(app=app)Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 114why, one step back114 dispatch = ConnectionPool(backend=backend, http2=True)Add test script (#451)Florimond Mancabefore adoption5e4f54d
line 115why, one step back115Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 116why, one step back116 async with AsyncClient(dispatch=dispatch) as client:Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 117–147why, one step back117 await client.get("http://example.org")118119 h2_conn = backend.server.conn120121 assert isinstance(h2_conn, h2.connection.H2Connection)122 expected_settings = {123 SettingCodes.HEADER_TABLE_SIZE: 4096,124 SettingCodes.ENABLE_PUSH: 0,125 SettingCodes.MAX_CONCURRENT_STREAMS: 100,126 SettingCodes.INITIAL_WINDOW_SIZE: 65535,127 SettingCodes.MAX_FRAME_SIZE: 16384,128 SettingCodes.MAX_HEADER_LIST_SIZE: 65536,129 # This one's here because h2 helpfully populates remote_settings130 # with default values even if the peer doesn't send the setting.131 SettingCodes.ENABLE_CONNECT_PROTOCOL: 0,132 }133 assert dict(h2_conn.remote_settings) == expected_settings134135 # We don't expect the ENABLE_CONNECT_PROTOCOL to be in the handshake136 expected_settings.pop(SettingCodes.ENABLE_CONNECT_PROTOCOL)137138 assert len(backend.server.settings_changed) == 1139 settings = backend.server.settings_changed[0]140141 assert isinstance(settings, h2.events.RemoteSettingsChanged)142 assert len(settings.changed_settings) == len(expected_settings)143 for setting_code, changed_setting in settings.changed_settings.items():144 assert isinstance(changed_setting, h2.settings.ChangedSetting)145 assert changed_setting.new_value == expected_settings[setting_code]146147Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 148–149why, one step back148@pytest.mark.usefixtures("async_environment")149async def test_http2_live_request():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 150why, one step back150 async with AsyncClient(http2=True) as client:Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
lines 151–152why, one step back151 try:152 resp = await client.get("https://nghttp2.org/httpbin/anything")Bump coverage (#705)Tom Christiebefore adoptionb8394f1
line 153why, one step back153 except TimeoutException: # pragma: nocoverImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 154why, one step back154 pytest.xfail(reason="nghttp2.org appears to be unresponsive")Bump coverage (#705)Tom Christiebefore adoptionb8394f1
line 155why, one step back155 except socket.gaierror: # pragma: nocoverImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 156why, one step back156 pytest.xfail(reason="You appear to be offline")Add test script (#451)Florimond Mancabefore adoption5e4f54d
lines 157–158why, one step back157 assert resp.status_code == 200158 assert resp.http_version == "HTTP/2"