Skip to content
Myr

test_http2.py

This file

Path

tests/dispatch/test_http2.py

150 lines · 5.0 KiB · Python

Last changed by

Fix Timeout -> TimeoutException in test case (#598)Tom Christiebefore adoptiondad3797

History of this file

Take 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 back
1import json

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

line 2why, one step back
2import socket

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

line 3why, one step back
3

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 4–5why, one step back
4import h2.connection
5import h2.events

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

line 6why, one step back
6import pytest

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 7–8why, one step back
7from h2.settings import SettingCodes
8

Fix Timeout -> TimeoutException in test case (#598)Tom Christiebefore adoptiondad3797

line 9why, one step back
9from httpx import Client, Response, TimeoutException

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

line 10why, one step back
10

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 11–25why, one step back
11from .utils import MockHTTP2Backend
121314def 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)2425

Drop sync (#544)Tom Christiebefore adoption206c537

lines 26–27why, one step back
26@pytest.mark.asyncio
27async def test_http2_get_request():

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 28–29why, one step back
28 backend = MockHTTP2Backend(app=app)
29

Rename http2 switch (#586)Tom Christiebefore adoption5076952

line 30why, one step back
30 async with Client(backend=backend, http2=True) as client:

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 31–36why, one step back
31 response = await client.get("http://example.org")
3233 assert response.status_code == 20034 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}3536

Drop sync (#544)Tom Christiebefore adoption206c537

lines 37–38why, one step back
37@pytest.mark.asyncio
38async def test_http2_post_request():

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 39–40why, one step back
39 backend = MockHTTP2Backend(app=app)
40

Rename http2 switch (#586)Tom Christiebefore adoption5076952

line 41why, one step back
41 async with Client(backend=backend, http2=True) as client:

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 42–51why, one step back
42 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 }5051

Drop sync (#544)Tom Christiebefore adoption206c537

lines 52–53why, one step back
52@pytest.mark.asyncio
53async def test_http2_large_post_request():

Fix bug in calculating maximum frame size (#153)IsaacBenghiatbefore adoptionab9cd0e

lines 54–56why, one step back
54 backend = MockHTTP2Backend(app=app)
5556 data = b"a" * 100000

Rename http2 switch (#586)Tom Christiebefore adoption5076952

line 57why, one step back
57 async with Client(backend=backend, http2=True) as client:

Fix bug in calculating maximum frame size (#153)IsaacBenghiatbefore adoptionab9cd0e

lines 58–66why, one step back
58 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 }6566

Drop sync (#544)Tom Christiebefore adoption206c537

lines 67–68why, one step back
67@pytest.mark.asyncio
68async def test_http2_multiple_requests():

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 69–70why, one step back
69 backend = MockHTTP2Backend(app=app)
70

Rename http2 switch (#586)Tom Christiebefore adoption5076952

line 71why, one step back
71 async with Client(backend=backend, http2=True) as client:

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 72–85why, one step back
72 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": ""}8485

Drop sync (#544)Tom Christiebefore adoption206c537

lines 86–87why, one step back
86@pytest.mark.asyncio
87async def test_http2_reconnect():

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 88–93why, one step back
88 """
89 If a connection has been dropped between requests, then we should90 be seemlessly reconnected.91 """92 backend = MockHTTP2Backend(app=app)93

Rename http2 switch (#586)Tom Christiebefore adoption5076952

line 94why, one step back
94 async with Client(backend=backend, http2=True) as client:

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 95–97why, one step back
95 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 back
98
99 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–108why, one step back
104
105106async def test_http2_settings_in_handshake(backend):107 backend = MockHTTP2Backend(app=app, backend=backend)108

Rename http2 switch (#586)Tom Christiebefore adoption5076952

line 109why, one step back
109 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 back
110 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 back
139
140141async def test_http2_live_request(backend):

Rename http2 switch (#586)Tom Christiebefore adoption5076952

line 142why, one step back
142 async with Client(backend=backend, http2=True) as client:

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 143–144why, one step back
143 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 back
145 except TimeoutException:

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 146–148why, one step back
146 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 back
149 assert resp.status_code == 200
150 assert resp.http_version == "HTTP/2"