Skip to content

Commit 085f67b

Browse files
authored
Add test to make sure we send connection close when client sends it (#1776)
1 parent 782f17e commit 085f67b

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

‎tests/protocols/test_http.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
]
3636
)
3737

38+
CONNECTION_CLOSE_REQUEST = b"\r\n".join(
39+
[b"GET / HTTP/1.1", b"Host: example.org", b"Connection: close", b"", b""]
40+
)
41+
3842
LARGE_POST_REQUEST = b"\r\n".join(
3943
[
4044
b"POST / HTTP/1.1",
@@ -936,3 +940,29 @@ async def test_huge_headers_h11_max_incomplete():
936940
await protocol.loop.run_one()
937941
assert b"HTTP/1.1 200 OK" in protocol.transport.buffer
938942
assert b"Hello, world" in protocol.transport.buffer
943+
944+
945+
@pytest.mark.anyio
946+
@pytest.mark.parametrize(
947+
"protocol_cls,close_header",
948+
(
949+
pytest.param(
950+
HttpToolsProtocol,
951+
b"connection: close",
952+
marks=pytest.mark.skipif(
953+
HttpToolsProtocol is None, reason="httptools is not installed"
954+
),
955+
),
956+
(H11Protocol, b"Connection: close"),
957+
),
958+
)
959+
async def test_return_close_header(protocol_cls, close_header: bytes):
960+
app = Response("Hello, world", media_type="text/plain")
961+
962+
protocol = get_connected_protocol(app, protocol_cls)
963+
protocol.data_received(CONNECTION_CLOSE_REQUEST)
964+
await protocol.loop.run_one()
965+
assert b"HTTP/1.1 200 OK" in protocol.transport.buffer
966+
assert b"content-type: text/plain" in protocol.transport.buffer
967+
assert b"content-length: 12" in protocol.transport.buffer
968+
assert close_header in protocol.transport.buffer

‎tests/test_main.py‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@ async def app(scope, receive, send):
1616
await send({"type": "http.response.body", "body": b"", "more_body": False})
1717

1818

19-
@pytest.mark.anyio
20-
async def test_return_close_header():
21-
config = Config(app=app, host="localhost", loop="asyncio", limit_max_requests=1)
22-
async with run_server(config):
23-
async with httpx.AsyncClient() as client:
24-
response = await client.get(
25-
"http://127.0.0.1:8000", headers={"connection": "close"}
26-
)
27-
28-
assert response.status_code == 204
29-
assert (
30-
"connection" in response.headers and response.headers["connection"] == "close"
31-
)
32-
33-
3419
def _has_ipv6(host):
3520
sock = None
3621
has_ipv6 = False

0 commit comments

Comments
 (0)