Skip to content
Myr

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 file

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

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

line 2why, one step back
2import socket

Add test script (#451)Florimond Mancabefore adoption5e4f54d

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 test script (#451)Florimond Mancabefore adoption5e4f54d

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

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 10why, one step back
10from httpx._dispatch.connection_pool import ConnectionPool

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 11–14why, one step back
11
12from .utils import MockHTTP2Backend1314

Added RequestContent (#636)Tom Christiebefore adoption8d4f182

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

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

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

Add test script (#451)Florimond Mancabefore adoption5e4f54d

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

Added RequestContent (#636)Tom Christiebefore adoption8d4f182

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

Add test script (#451)Florimond Mancabefore adoption5e4f54d

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

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

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

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 24–25why, one step back
24
25

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add test script (#451)Florimond Mancabefore adoption5e4f54d

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

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 29why, one step back
29 dispatch = ConnectionPool(backend=backend, http2=True)

Add test script (#451)Florimond Mancabefore adoption5e4f54d

line 30why, one step back
30

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 31why, one step back
31 async with AsyncClient(dispatch=dispatch) as client:

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 32–37why, one step back
32 response = await client.get("http://example.org")
3334 assert response.status_code == 20035 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}3637

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add test script (#451)Florimond Mancabefore adoption5e4f54d

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

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 41why, one step back
41 dispatch = ConnectionPool(backend=backend, http2=True)

Add test script (#451)Florimond Mancabefore adoption5e4f54d

line 42why, one step back
42

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 43why, one step back
43 async with AsyncClient(dispatch=dispatch) as client:

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 44–53why, one step back
44 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 }5253

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add test script (#451)Florimond Mancabefore adoption5e4f54d

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

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 57why, one step back
57 dispatch = ConnectionPool(backend=backend, http2=True)

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 58–59why, one step back
58
59 data = b"a" * 100000

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 60why, one step back
60 async with AsyncClient(dispatch=dispatch) as client:

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 61–69why, one step back
61 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 }6869

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add test script (#451)Florimond Mancabefore adoption5e4f54d

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

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 73why, one step back
73 dispatch = ConnectionPool(backend=backend, http2=True)

Add test script (#451)Florimond Mancabefore adoption5e4f54d

line 74why, one step back
74

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 75why, one step back
75 async with AsyncClient(dispatch=dispatch) as client:

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 76–89why, one step back
76 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": ""}8889

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 92–96why, one step back
92 """
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 back
97 dispatch = ConnectionPool(backend=backend, http2=True)

Add test script (#451)Florimond Mancabefore adoption5e4f54d

line 98why, one step back
98

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 99why, one step back
99 async with AsyncClient(dispatch=dispatch) as client:

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 100–110why, one step back
100 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": ""}109110

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

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

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

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

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 114why, one step back
114 dispatch = ConnectionPool(backend=backend, http2=True)

Add test script (#451)Florimond Mancabefore adoption5e4f54d

line 115why, one step back
115

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

line 116why, one step back
116 async with AsyncClient(dispatch=dispatch) as client:

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 117–147why, one step back
117 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]146147

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

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

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

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

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

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

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

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

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

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

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

line 156why, one step back
156 pytest.xfail(reason="You appear to be offline")

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 157–158why, one step back
157 assert resp.status_code == 200
158 assert resp.http_version == "HTTP/2"