Skip to content

Commit

Permalink
Add extra invalid HTTP request test cases (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca authored Dec 12, 2020
1 parent 2e17dd9 commit 5e8a888
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions tests/protocols/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@
]
)

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"",
]
)


Expand Down Expand Up @@ -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

0 comments on commit 5e8a888

Please sign in to comment.