Skip to content
Myr

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 file

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

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

line 2why, one step back
2import socket

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

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

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

line 6why, one step back
6import pytest

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 10–13why, one step back
10
11from .utils import MockHTTP2Backend1213

Added RequestContent (#636)Tom Christiebefore adoption8d4f182

lines 14–16why, one step back
14async def app(request):
15 method = request.method16 path = request.url.path

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 17why, one step back
17 body = b"".join([part async for part in request.stream])

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

line 18why, one step back
18 content = json.dumps(

Added RequestContent (#636)Tom Christiebefore adoption8d4f182

line 19why, one step back
19 {"method": method, "path": path, "body": body.decode()}

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 20–21why, one step back
20 ).encode()
21 headers = {"Content-Length": str(len(content))}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 22why, one step back
22 return Response(200, headers=headers, content=content, request=request)

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 23–24why, one step back
23
24

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 30–35why, one step back
30 response = await client.get("http://example.org")
3132 assert response.status_code == 20033 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}3435

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 41–50why, one step back
41 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 }4950

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 53–55why, one step back
53 backend = MockHTTP2Backend(app=app)
5455 data = b"a" * 100000

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 57–65why, one step back
57 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 }6465

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 71–84why, one step back
71 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": ""}8384

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 94–104why, one step back
94 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": ""}103104

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 105–106why, one step back
105@pytest.mark.asyncio
106async def test_http2_settings_in_handshake():

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 107why, one step back
107 backend = MockHTTP2Backend(app=app)

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

line 108why, one step back
108

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

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–140why, one step back
139
140

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 141–142why, one step back
141@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 back
143 async with AsyncClient(http2=True) as client:

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

lines 144–145why, one step back
144 try:
145 resp = await client.get("https://nghttp2.org/httpbin/anything")

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

line 146why, one step back
146 except TimeoutException: # pragma: nocover

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

line 147why, one step back
147 pytest.xfail(reason="nghttp2.org appears to be unresponsive")

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

line 148why, one step back
148 except socket.gaierror: # pragma: nocover

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

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