test_http2.py
This file
Path
tests/dispatch/test_http2.py
150 lines · 5.0 KiB · Python
Last changed by
Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
History of this fileTake it
The walk stopped after 200 commits: lines older than b46d9ea point at it.
Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
line 1why, one step back1import jsonImprove robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
line 2why, one step back2import socketInstall package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
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, TimeoutExceptionAdd missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
line 10why, one step back10Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 11–25why, one step back11from .utils import MockHTTP2Backend121314def app(request):15 content = json.dumps(16 {17 "method": request.method,18 "path": request.url.path,19 "body": request.content.decode(),20 }21 ).encode()22 headers = {"Content-Length": str(len(content))}23 return Response(200, headers=headers, content=content)2425Drop sync (#544)Tom Christiebefore adoption206c537
lines 26–27why, one step back26@pytest.mark.asyncio27async def test_http2_get_request():Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 28–29why, one step back28 backend = MockHTTP2Backend(app=app)29Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 30why, one step back30 async with Client(backend=backend, http2=True) as client:Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 31–36why, one step back31 response = await client.get("http://example.org")3233 assert response.status_code == 20034 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}3536Drop sync (#544)Tom Christiebefore adoption206c537
lines 37–38why, one step back37@pytest.mark.asyncio38async def test_http2_post_request():Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 39–40why, one step back39 backend = MockHTTP2Backend(app=app)40Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 41why, one step back41 async with Client(backend=backend, http2=True) as client:Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 42–51why, one step back42 response = await client.post("http://example.org", data=b"<data>")4344 assert response.status_code == 20045 assert json.loads(response.content) == {46 "method": "POST",47 "path": "/",48 "body": "<data>",49 }5051Drop sync (#544)Tom Christiebefore adoption206c537
lines 52–53why, one step back52@pytest.mark.asyncio53async def test_http2_large_post_request():Fix bug in calculating maximum frame size (#153)IsaacBenghiatbefore adoptionab9cd0e
lines 54–56why, one step back54 backend = MockHTTP2Backend(app=app)5556 data = b"a" * 100000Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 57why, one step back57 async with Client(backend=backend, http2=True) as client:Fix bug in calculating maximum frame size (#153)IsaacBenghiatbefore adoptionab9cd0e
lines 58–66why, one step back58 response = await client.post("http://example.org", data=data)59 assert response.status_code == 20060 assert json.loads(response.content) == {61 "method": "POST",62 "path": "/",63 "body": data.decode(),64 }6566Drop sync (#544)Tom Christiebefore adoption206c537
lines 67–68why, one step back67@pytest.mark.asyncio68async def test_http2_multiple_requests():Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 69–70why, one step back69 backend = MockHTTP2Backend(app=app)70Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 71why, one step back71 async with Client(backend=backend, http2=True) as client:Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 72–85why, one step back72 response_1 = await client.get("http://example.org/1")73 response_2 = await client.get("http://example.org/2")74 response_3 = await client.get("http://example.org/3")7576 assert response_1.status_code == 20077 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}7879 assert response_2.status_code == 20080 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}8182 assert response_3.status_code == 20083 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}8485Drop sync (#544)Tom Christiebefore adoption206c537
lines 86–87why, one step back86@pytest.mark.asyncio87async def test_http2_reconnect():Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 88–93why, one step back88 """89 If a connection has been dropped between requests, then we should90 be seemlessly reconnected.91 """92 backend = MockHTTP2Backend(app=app)93Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 94why, one step back94 async with Client(backend=backend, http2=True) as client:Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9
lines 95–97why, one step back95 response_1 = await client.get("http://example.org/1")96 backend.server.close_connection = True97 response_2 = await client.get("http://example.org/2")Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 98–103why, one step back9899 assert response_1.status_code == 200100 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}101102 assert response_2.status_code == 200103 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 104–106why, one step back104105106async def test_http2_settings_in_handshake(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 107why, one step back107 backend = MockHTTP2Backend(app=app)Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230
line 108why, one step back108Rename http2 switch (#586)Tom Christiebefore adoption5076952
line 109why, one step back109 async with Client(backend=backend, http2=True) as client:Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230
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–141why, one step back139140141async def test_http2_live_request(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 142why, one step back142 async with Client(http2=True) as client:Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
lines 143–144why, one step back143 try:144 resp = await client.get("https://nghttp2.org/httpbin/anything")Fix Timeout -> TimeoutException in test case (#598)Tom Christiebefore adoptiondad3797
line 145why, one step back145 except TimeoutException:Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd
lines 146–148why, one step back146 pytest.xfail(reason="nghttp2.org appears to be unresponsive")147 except socket.gaierror:148 pytest.xfail(reason="You appear to be offline")Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 149–150why, one step back149 assert resp.status_code == 200150 assert resp.http_version == "HTTP/2"