-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathtest_http11.py
381 lines (340 loc) · 12.7 KB
/
test_http11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import pytest
import httpcore
@pytest.mark.anyio
async def test_http11_connection():
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
async with httpcore.AsyncHTTP11Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
response = await conn.request("GET", "https://example.com/")
assert response.status == 200
assert response.content == b"Hello, world!"
assert conn.is_idle()
assert conn.is_connected()
assert not conn.is_closed()
assert conn.is_available()
assert not conn.has_expired()
assert (
repr(conn)
== "<AsyncHTTP11Connection ['https://example.com:443', IDLE, Request Count: 1]>"
)
@pytest.mark.anyio
async def test_http11_connection_unread_response():
"""
If the client releases the response without reading it to termination,
then the connection will not be reusable.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
async with httpcore.AsyncHTTP11Connection(origin=origin, stream=stream) as conn:
async with conn.stream("GET", "https://example.com/") as response:
assert response.status == 200
assert not conn.is_idle()
assert conn.is_closed()
assert not conn.is_available()
assert not conn.has_expired()
assert (
repr(conn)
== "<AsyncHTTP11Connection ['https://example.com:443', CLOSED, Request Count: 1]>"
)
@pytest.mark.anyio
async def test_http11_connection_with_remote_protocol_error():
"""
If a remote protocol error occurs, then no response will be returned,
and the connection will not be reusable.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream([b"Wait, this isn't valid HTTP!", b""])
async with httpcore.AsyncHTTP11Connection(origin=origin, stream=stream) as conn:
with pytest.raises(httpcore.RemoteProtocolError):
await conn.request("GET", "https://example.com/")
assert not conn.is_idle()
assert conn.is_closed()
assert not conn.is_available()
assert not conn.has_expired()
assert (
repr(conn)
== "<AsyncHTTP11Connection ['https://example.com:443', CLOSED, Request Count: 1]>"
)
@pytest.mark.anyio
async def test_http11_connection_with_incomplete_response():
"""
We should be gracefully handling the case where the connection ends prematurely.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, wor",
]
)
async with httpcore.AsyncHTTP11Connection(origin=origin, stream=stream) as conn:
with pytest.raises(httpcore.RemoteProtocolError):
await conn.request("GET", "https://example.com/")
assert not conn.is_idle()
assert conn.is_closed()
assert not conn.is_available()
assert not conn.has_expired()
assert (
repr(conn)
== "<AsyncHTTP11Connection ['https://example.com:443', CLOSED, Request Count: 1]>"
)
@pytest.mark.anyio
async def test_http11_connection_with_local_protocol_error():
"""
If a local protocol error occurs, then no response will be returned,
and the connection will not be reusable.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
async with httpcore.AsyncHTTP11Connection(origin=origin, stream=stream) as conn:
with pytest.raises(httpcore.LocalProtocolError) as exc_info:
await conn.request("GET", "https://example.com/", headers={"Host": "\0"})
assert str(exc_info.value) == "Illegal header value b'\\x00'"
assert not conn.is_idle()
assert conn.is_closed()
assert not conn.is_available()
assert not conn.has_expired()
assert (
repr(conn)
== "<AsyncHTTP11Connection ['https://example.com:443', CLOSED, Request Count: 1]>"
)
@pytest.mark.anyio
async def test_http11_connection_handles_one_active_request():
"""
Attempting to send a request while one is already in-flight will raise
a ConnectionNotAvailable exception.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
async with httpcore.AsyncHTTP11Connection(origin=origin, stream=stream) as conn:
async with conn.stream("GET", "https://example.com/"):
with pytest.raises(httpcore.ConnectionNotAvailable):
await conn.request("GET", "https://example.com/")
@pytest.mark.anyio
async def test_http11_connection_attempt_close():
"""
A connection can only be closed when it is idle.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
async with httpcore.AsyncHTTP11Connection(origin=origin, stream=stream) as conn:
async with conn.stream("GET", "https://example.com/") as response:
await response.aread()
assert response.status == 200
assert response.content == b"Hello, world!"
@pytest.mark.anyio
async def test_http11_request_to_incorrect_origin():
"""
A connection can only send requests to whichever origin it is connected to.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream([])
async with httpcore.AsyncHTTP11Connection(origin=origin, stream=stream) as conn:
with pytest.raises(RuntimeError):
await conn.request("GET", "https://other.com/")
@pytest.mark.anyio
async def test_http11_expect_continue():
"""
HTTP "100 Continue" is an interim response.
We simply ignore it and return the final response.
https://httpwg.org/specs/rfc9110.html#status.100
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 100 Continue\r\n",
b"\r\n",
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
async with httpcore.AsyncHTTP11Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
response = await conn.request(
"GET",
"https://example.com/",
headers={"Expect": "continue"},
)
assert response.status == 200
assert response.content == b"Hello, world!"
@pytest.mark.anyio
async def test_http11_upgrade_connection():
"""
HTTP "101 Switching Protocols" indicates an upgraded connection.
We should return the response, so that the network stream
may be used for the upgraded connection.
https://httpwg.org/specs/rfc9110.html#status.101
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/101
"""
origin = httpcore.Origin(b"wss", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 101 Switching Protocols\r\n",
b"Connection: upgrade\r\n",
b"Upgrade: custom\r\n",
b"\r\n",
b"...",
]
)
async with httpcore.AsyncHTTP11Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
async with conn.stream(
"GET",
"wss://example.com/",
headers={"Connection": "upgrade", "Upgrade": "custom"},
) as response:
assert response.status == 101
network_stream = response.extensions["network_stream"]
content = await network_stream.read(max_bytes=1024)
assert content == b"..."
@pytest.mark.anyio
async def test_http11_upgrade_with_trailing_data():
"""
HTTP "101 Switching Protocols" indicates an upgraded connection.
In `CONNECT` and `Upgrade:` requests, we need to handover the trailing data
in the h11.Connection object.
https://h11.readthedocs.io/en/latest/api.html#switching-protocols
"""
origin = httpcore.Origin(b"wss", b"example.com", 443)
stream = httpcore.AsyncMockStream(
# The first element of this mock network stream buffer simulates networking
# in which response headers and data are received at once.
# This means that "foobar" becomes trailing data.
[
(
b"HTTP/1.1 101 Switching Protocols\r\n"
b"Connection: upgrade\r\n"
b"Upgrade: custom\r\n"
b"\r\n"
b"foobar"
),
b"baz",
]
)
async with httpcore.AsyncHTTP11Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
async with conn.stream(
"GET",
"wss://example.com/",
headers={"Connection": "upgrade", "Upgrade": "custom"},
) as response:
assert response.status == 101
network_stream = response.extensions["network_stream"]
content = await network_stream.read(max_bytes=3)
assert content == b"foo"
content = await network_stream.read(max_bytes=3)
assert content == b"bar"
content = await network_stream.read(max_bytes=3)
assert content == b"baz"
# Lazy tests for AsyncHTTP11UpgradeStream
await network_stream.write(b"spam")
invalid = network_stream.get_extra_info("invalid")
assert invalid is None
await network_stream.aclose()
@pytest.mark.anyio
async def test_http11_early_hints():
"""
HTTP "103 Early Hints" is an interim response.
We simply ignore it and return the final response.
https://datatracker.ietf.org/doc/rfc8297/
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 103 Early Hints\r\n",
b"Link: </style.css>; rel=preload; as=style\r\n",
b"Link: </script.js.css>; rel=preload; as=style\r\n",
b"\r\n",
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: text/html; charset=utf-8\r\n",
b"Content-Length: 30\r\n",
b"Link: </style.css>; rel=preload; as=style\r\n",
b"Link: </script.js>; rel=preload; as=script\r\n",
b"\r\n",
b"<html>Hello, world! ...</html>",
]
)
async with httpcore.AsyncHTTP11Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
response = await conn.request(
"GET",
"https://example.com/",
headers={"Expect": "continue"},
)
assert response.status == 200
assert response.content == b"<html>Hello, world! ...</html>"
@pytest.mark.anyio
async def test_http11_header_sub_100kb():
"""
A connection should be able to handle a http header size up to 100kB.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.AsyncMockStream(
[
b"HTTP/1.1 200 OK\r\n", # 17
b"Content-Type: plain/text\r\n", # 43
b"Cookie: " + b"x" * (100 * 1024 - 72) + b"\r\n", # 102381
b"Content-Length: 0\r\n", # 102400
b"\r\n",
b"",
]
)
async with httpcore.AsyncHTTP11Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
response = await conn.request("GET", "https://example.com/")
assert response.status == 200
assert response.content == b""