Skip to content

Commit 5809b95

Browse files
Add HTTP/1.1 concurrent requests test (#332)
1 parent 8d87938 commit 5809b95

2 files changed

Lines changed: 52 additions & 161 deletions

File tree

tests/async_tests/test_http11.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,55 @@ async def test_request_with_missing_host_header() -> None:
263263
extensions={},
264264
)
265265
assert str(excinfo.value) == "Missing mandatory Host: header"
266+
267+
268+
@pytest.mark.trio
269+
async def test_concurrent_get_requests() -> None:
270+
backend = MockBackend(
271+
http_buffer=[
272+
b"HTTP/1.1 200 OK\r\n",
273+
b"Date: Sat, 06 Oct 2049 12:34:56 GMT\r\n",
274+
b"Server: Apache\r\n",
275+
b"Content-Length: 13\r\n",
276+
b"Content-Type: text/plain\r\n",
277+
b"\r\n",
278+
b"Hello, world.",
279+
]
280+
)
281+
282+
async with httpcore.AsyncConnectionPool(backend=backend) as http:
283+
# We're sending a request with a standard keep-alive connection, so
284+
# it will remain in the pool once we've sent the request.
285+
response_1 = await http.handle_async_request(
286+
method=b"GET",
287+
url=(b"http", b"example.org", None, b"/"),
288+
headers=[(b"Host", b"example.org")],
289+
stream=httpcore.ByteStream(b""),
290+
extensions={},
291+
)
292+
status_code, headers, stream_1, extensions = response_1
293+
assert await http.get_connection_info() == {
294+
"http://example.org": ["HTTP/1.1, ACTIVE"]
295+
}
296+
297+
response_2 = await http.handle_async_request(
298+
method=b"GET",
299+
url=(b"http", b"example.org", None, b"/"),
300+
headers=[(b"Host", b"example.org")],
301+
stream=httpcore.ByteStream(b""),
302+
extensions={},
303+
)
304+
status_code, headers, stream_2, extensions = response_2
305+
assert await http.get_connection_info() == {
306+
"http://example.org": ["HTTP/1.1, ACTIVE", "HTTP/1.1, ACTIVE"]
307+
}
308+
309+
await stream_1.aread()
310+
assert await http.get_connection_info() == {
311+
"http://example.org": ["HTTP/1.1, ACTIVE", "HTTP/1.1, IDLE"]
312+
}
313+
314+
await stream_2.aread()
315+
assert await http.get_connection_info() == {
316+
"http://example.org": ["HTTP/1.1, IDLE", "HTTP/1.1, IDLE"]
317+
}

tests/async_tests/test_http2.py

Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -247,164 +247,3 @@ async def test_request_with_missing_host_header() -> None:
247247
extensions={},
248248
)
249249
assert str(excinfo.value) == "Missing mandatory Host: header"
250-
251-
252-
# # This second request will go out over the same connection.
253-
# response = await http.handle_async_request(
254-
# method=b"GET",
255-
# url=(b"http", b"example.org", None, b"/"),
256-
# headers=[(b"Host", b"example.org")],
257-
# stream=httpcore.ByteStream(b""),
258-
# extensions={},
259-
# )
260-
# status_code, headers, stream, extensions = response
261-
# body = await stream.aread()
262-
# assert status_code == 200
263-
# assert body == b"Hello, world."
264-
# assert await http.get_connection_info() == {
265-
# "http://example.org": ["HTTP/1.1, IDLE"]
266-
# }
267-
#
268-
#
269-
# @pytest.mark.trio
270-
# async def test_get_request_with_connection_close_header() -> None:
271-
# backend = MockBackend(
272-
# http_buffer=[
273-
# b"HTTP/1.1 200 OK\r\n",
274-
# b"Date: Sat, 06 Oct 2049 12:34:56 GMT\r\n",
275-
# b"Server: Apache\r\n",
276-
# b"Content-Length: 13\r\n",
277-
# b"Content-Type: text/plain\r\n",
278-
# b"\r\n",
279-
# b"Hello, world.",
280-
# b"", # Terminate the connection.
281-
# ]
282-
# )
283-
#
284-
# async with httpcore.AsyncConnectionPool(backend=backend) as http:
285-
# # We're sending a request with 'Connection: close', so the connection
286-
# # does not remain in the pool once we've sent the request.
287-
# response = await http.handle_async_request(
288-
# method=b"GET",
289-
# url=(b"http", b"example.org", None, b"/"),
290-
# headers=[(b"Host", b"example.org"), (b"Connection", b"close")],
291-
# stream=httpcore.ByteStream(b""),
292-
# extensions={},
293-
# )
294-
# status_code, headers, stream, extensions = response
295-
# body = await stream.aread()
296-
# assert status_code == 200
297-
# assert body == b"Hello, world."
298-
# assert await http.get_connection_info() == {}
299-
#
300-
# # The second request will go out over a new connection.
301-
# response = await http.handle_async_request(
302-
# method=b"GET",
303-
# url=(b"http", b"example.org", None, b"/"),
304-
# headers=[(b"Host", b"example.org"), (b"Connection", b"close")],
305-
# stream=httpcore.ByteStream(b""),
306-
# extensions={},
307-
# )
308-
# status_code, headers, stream, extensions = response
309-
# body = await stream.aread()
310-
# assert status_code == 200
311-
# assert body == b"Hello, world."
312-
# assert await http.get_connection_info() == {}
313-
#
314-
#
315-
# @pytest.mark.trio
316-
# async def test_get_request_with_socket_disconnect_between_requests() -> None:
317-
# backend = MockBackend(
318-
# http_buffer=[
319-
# b"HTTP/1.1 200 OK\r\n",
320-
# b"Date: Sat, 06 Oct 2049 12:34:56 GMT\r\n",
321-
# b"Server: Apache\r\n",
322-
# b"Content-Length: 13\r\n",
323-
# b"Content-Type: text/plain\r\n",
324-
# b"\r\n",
325-
# b"Hello, world.",
326-
# ],
327-
# disconnect=True,
328-
# )
329-
#
330-
# async with httpcore.AsyncConnectionPool(backend=backend) as http:
331-
# # Send an initial request. We're using a standard keep-alive
332-
# # connection, so the connection remains in the pool after completion.
333-
# response = await http.handle_async_request(
334-
# method=b"GET",
335-
# url=(b"http", b"example.org", None, b"/"),
336-
# headers=[(b"Host", b"example.org")],
337-
# stream=httpcore.ByteStream(b""),
338-
# extensions={},
339-
# )
340-
# status_code, headers, stream, extensions = response
341-
# body = await stream.aread()
342-
# assert status_code == 200
343-
# assert body == b"Hello, world."
344-
# assert await http.get_connection_info() == {
345-
# "http://example.org": ["HTTP/1.1, IDLE"]
346-
# }
347-
#
348-
# # On sending this second request, at the point of pool re-acquiry the
349-
# # socket indicates that it has disconnected, and we'll send the request
350-
# # over a new connection.
351-
# response = await http.handle_async_request(
352-
# method=b"GET",
353-
# url=(b"http", b"example.org", None, b"/"),
354-
# headers=[(b"Host", b"example.org")],
355-
# stream=httpcore.ByteStream(b""),
356-
# extensions={},
357-
# )
358-
# status_code, headers, stream, extensions = response
359-
# body = await stream.aread()
360-
# assert status_code == 200
361-
# assert body == b"Hello, world."
362-
# assert await http.get_connection_info() == {
363-
# "http://example.org": ["HTTP/1.1, IDLE"]
364-
# }
365-
#
366-
#
367-
# @pytest.mark.trio
368-
# async def test_get_request_with_unclean_close_after_first_request() -> None:
369-
# backend = MockBackend(
370-
# http_buffer=[
371-
# b"HTTP/1.1 200 OK\r\n",
372-
# b"Date: Sat, 06 Oct 2049 12:34:56 GMT\r\n",
373-
# b"Server: Apache\r\n",
374-
# b"Content-Length: 13\r\n",
375-
# b"Content-Type: text/plain\r\n",
376-
# b"\r\n",
377-
# b"Hello, world.",
378-
# b"", # Terminate the connection.
379-
# ],
380-
# )
381-
#
382-
# async with httpcore.AsyncConnectionPool(backend=backend) as http:
383-
# # Send an initial request. We're using a standard keep-alive
384-
# # connection, so the connection remains in the pool after completion.
385-
# response = await http.handle_async_request(
386-
# method=b"GET",
387-
# url=(b"http", b"example.org", None, b"/"),
388-
# headers=[(b"Host", b"example.org")],
389-
# stream=httpcore.ByteStream(b""),
390-
# extensions={},
391-
# )
392-
# status_code, headers, stream, extensions = response
393-
# body = await stream.aread()
394-
# assert status_code == 200
395-
# assert body == b"Hello, world."
396-
# assert await http.get_connection_info() == {
397-
# "http://example.org": ["HTTP/1.1, IDLE"]
398-
# }
399-
#
400-
# # At this point we successfully write another request, but the socket
401-
# # read returns `b""`, indicating a premature close.
402-
# with pytest.raises(httpcore.RemoteProtocolError) as excinfo:
403-
# await http.handle_async_request(
404-
# method=b"GET",
405-
# url=(b"http", b"example.org", None, b"/"),
406-
# headers=[(b"Host", b"example.org")],
407-
# stream=httpcore.ByteStream(b""),
408-
# extensions={},
409-
# )
410-
# assert str(excinfo.value) == "Server disconnected without sending a response."

0 commit comments

Comments
 (0)