Add extra invalid HTTP request test cases
@@ -72,8 +72,13 @@ UPGRADE_REQUEST = b"\r\n".join(
]
)
-INVALID_REQUEST = b"\r\n".join(
- [b"GET /?x=y z HTTP/1.1", b"Host: example.org", b"", b""] # bad space character
+INVALID_REQUEST_TEMPLATE = b"\r\n".join(
+ [
+ b"%s",
+ b"Host: example.org",
+ b"",
+ b"",
+ ]
)
@@ -706,15 +711,23 @@ def test_scopes(asgi2or3_app, expected_scopes, protocol_cls):
assert expected_scopes == protocol.scope.get("asgi")
+@pytest.mark.parametrize(
+ "request_line",
+ [
+ pytest.param(b"G?T / HTTP/1.1", id="invalid-method"),
+ pytest.param(b"GET /?x=y z HTTP/1.1", id="invalid-path"),
+ pytest.param(b"GET / HTTP1.1", id="invalid-http-version"),
+ ],
+)
@pytest.mark.parametrize("protocol_cls", HTTP_PROTOCOLS)
-def test_invalid_http_request(protocol_cls, caplog):
- def app(scope):
- return Response("Hello, world", media_type="text/plain")
+def test_invalid_http_request(request_line, protocol_cls, caplog):
+ app = Response("Hello, world", media_type="text/plain")
+ request = INVALID_REQUEST_TEMPLATE % request_line
caplog.set_level(logging.INFO, logger="uvicorn.error")
logging.getLogger("uvicorn.error").propagate = True
protocol = get_connected_protocol(app, protocol_cls)
- protocol.data_received(INVALID_REQUEST)
+ protocol.data_received(request)
assert not protocol.transport.buffer
assert "Invalid HTTP request received." in caplog.messages
