From 7bc36edb7b15cae90bbfa81187b63821466cdd59 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 24 Mar 2023 19:45:17 +0000 Subject: add middleware tests --- tests/middleware/test_if_none_match.py | 138 +++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 tests/middleware/test_if_none_match.py (limited to 'tests/middleware/test_if_none_match.py') diff --git a/tests/middleware/test_if_none_match.py b/tests/middleware/test_if_none_match.py new file mode 100644 index 0000000..8456c05 --- /dev/null +++ b/tests/middleware/test_if_none_match.py @@ -0,0 +1,138 @@ +from wsgiref.validate import validator + +import pytest + +from paste import if_none_match + +from ..common_wsgi import call_app + +ETAG = "1234" + + +@pytest.fixture +def app(): + @validator + @if_none_match + @validator + def app(environ, start_response): + body = b"Hello, world!" + etag_header = ("ETag", f'"{ETAG}"') + if environ["REQUEST_METHOD"] == "HEAD": + start_response( + "200 OK", + [ + ("Content-Type", "text/plain"), + ("Content-Length", str(len(body))), + etag_header, + ], + ) + return [] + start_response("200 OK", [("Content-Type", "text/plain"), etag_header]) + return [body] + + return app + + +@pytest.mark.parametrize("method", ["POST", "PUT", "DELETE"]) +def test_non_get_or_head_request_passthrough(app, method): + environ = {"REQUEST_METHOD": method, "HTTP_IF_NONE_MATCH": f'"{ETAG}"'} + response = call_app(app, environ) + assert response.data == b"Hello, world!" + assert response.status == "200 OK" + assert ("Content-Type", "text/plain") in response.headers + + +def test_if_none_match_header_not_set(app): + environ = {"REQUEST_METHOD": "GET"} + response = call_app(app, environ) + assert response.data == b"Hello, world!" + assert response.status == "200 OK" + assert ("Content-Type", "text/plain") in response.headers + + +@pytest.mark.parametrize("value", [f'"{ETAG}"', f'W/"{ETAG}"']) +def test_etag_matches_if_none_match_header(app, value): + environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": value} + response = call_app(app, environ) + assert response.data == b"" + assert response.status == "304 Not Modified" + + +@pytest.mark.parametrize( + "value", + [ + f'"{ETAG}", "54321"', + f'"54321", "{ETAG}", "098765"', + f'"54321", "{ETAG}"', + f'W/"{ETAG}", "54321"', + f'"54321", W/"{ETAG}", "098765"', + f'"54321", W/"{ETAG}"', + ], +) +def test_etag_matches_if_none_match_header_list(app, value): + environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": value} + response = call_app(app, environ) + assert response.data == b"" + assert response.status == "304 Not Modified" + + +def test_etag_matches_if_none_match_header_list2(app): + environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": f'"54321", "{ETAG}"'} + response = call_app(app, environ) + assert response.data == b"" + assert response.status == "304 Not Modified" + + +def test_etag_matches_if_none_match_header_star(app): + environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": "*"} + response = call_app(app, environ) + assert response.data == b"" + assert response.status == "304 Not Modified" + + +def test_missing_etag_does_not_match_if_none_match_header_star(): + @validator + @if_none_match + @validator + def app(environ, start_response): + body = b"Hello, world!" + if environ["REQUEST_METHOD"] == "HEAD": + start_response( + "200 OK", + [ + ("Content-Type", "text/plain"), + ("Content-Length", str(len(body))), + ], + ) + return [] + start_response("200 OK", [("Content-Type", "text/plain")]) + return [body] + + environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": "*"} + response = call_app(app, environ) + assert response.data == b"Hello, world!" + assert response.status == "200 OK" + + +def test_etag_does_not_match_if_none_match_header(app): + environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": '"54321"'} + response = call_app(app, environ) + assert response.data == b"Hello, world!" + assert response.status == "200 OK" + assert ("Content-Type", "text/plain") in response.headers + + +def test_etag_does_not_match_if_none_match_header_list(app): + environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": '"123","4567","*"'} + response = call_app(app, environ) + assert response.data == b"Hello, world!" + assert response.status == "200 OK" + assert ("Content-Type", "text/plain") in response.headers + + +def test_etag_does_not_match_if_none_match_malformed_header(app): + environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": "{ETAG}"} + response = call_app(app, environ) + assert response.data == b"400 Bad Request\n" + assert response.status == "400 Bad Request" + assert ("Content-Type", "text/plain") in response.headers -- cgit v1.2.3-54-g00ecf