|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import pytest |
| 3 | + |
| 4 | +from werkzeug.middleware.lint import HTTPWarning |
| 5 | +from werkzeug.middleware.lint import LintMiddleware |
| 6 | +from werkzeug.middleware.lint import WSGIWarning |
| 7 | +from werkzeug.test import create_environ |
| 8 | +from werkzeug.test import run_wsgi_app |
| 9 | + |
| 10 | + |
| 11 | +def dummy_application(environ, start_response): |
| 12 | + start_response("200 OK", [("Content-Type", "text/plain")]) |
| 13 | + return ["Foo"] |
| 14 | + |
| 15 | + |
| 16 | +def test_lint_middleware(): |
| 17 | + """ Test lint middleware runs for a dummy applications without warnings """ |
| 18 | + app = LintMiddleware(dummy_application) |
| 19 | + |
| 20 | + environ = create_environ("/test") |
| 21 | + app_iter, status, headers = run_wsgi_app(app, environ, buffered=True) |
| 22 | + assert status == "200 OK" |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize( |
| 26 | + "key, value, message", |
| 27 | + [ |
| 28 | + ("wsgi.version", (0, 7), "Environ is not a WSGI 1.0 environ."), |
| 29 | + ("SCRIPT_NAME", "test", "'SCRIPT_NAME' does not start with a slash:"), |
| 30 | + ("PATH_INFO", "test", "'PATH_INFO' does not start with a slash:"), |
| 31 | + ], |
| 32 | +) |
| 33 | +def test_lint_middleware_check_environ(key, value, message): |
| 34 | + app = LintMiddleware(dummy_application) |
| 35 | + |
| 36 | + environ = create_environ("/test") |
| 37 | + environ[key] = value |
| 38 | + with pytest.warns(WSGIWarning, match=message): |
| 39 | + app_iter, status, headers = run_wsgi_app(app, environ, buffered=True) |
| 40 | + assert status == "200 OK" |
| 41 | + |
| 42 | + |
| 43 | +def test_lint_middleware_invalid_status(): |
| 44 | + def my_dummy_application(environ, start_response): |
| 45 | + start_response("20 OK", [("Content-Type", "text/plain")]) |
| 46 | + return ["Foo"] |
| 47 | + |
| 48 | + app = LintMiddleware(my_dummy_application) |
| 49 | + |
| 50 | + environ = create_environ("/test") |
| 51 | + with pytest.warns(WSGIWarning) as record: |
| 52 | + run_wsgi_app(app, environ, buffered=True) |
| 53 | + |
| 54 | + # Returning status 20 should raise three different warnings |
| 55 | + assert len(record) == 3 |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize( |
| 59 | + "headers, message", |
| 60 | + [ |
| 61 | + (tuple([("Content-Type", "text/plain")]), "header list is not a list"), |
| 62 | + (["fo"], "Headers must tuple 2-item tuples"), |
| 63 | + ([("status", "foo")], "The status header is not supported"), |
| 64 | + ], |
| 65 | +) |
| 66 | +def test_lint_middleware_http_headers(headers, message): |
| 67 | + def my_dummy_application(environ, start_response): |
| 68 | + start_response("200 OK", headers) |
| 69 | + return ["Foo"] |
| 70 | + |
| 71 | + app = LintMiddleware(my_dummy_application) |
| 72 | + |
| 73 | + environ = create_environ("/test") |
| 74 | + with pytest.warns(WSGIWarning, match=message): |
| 75 | + run_wsgi_app(app, environ, buffered=True) |
| 76 | + |
| 77 | + |
| 78 | +def test_lint_middleware_invalid_location(): |
| 79 | + def my_dummy_application(environ, start_response): |
| 80 | + start_response("200 OK", [("location", "foo")]) |
| 81 | + return ["Foo"] |
| 82 | + |
| 83 | + app = LintMiddleware(my_dummy_application) |
| 84 | + |
| 85 | + environ = create_environ("/test") |
| 86 | + with pytest.warns(HTTPWarning, match="absolute URLs required for location header"): |
| 87 | + run_wsgi_app(app, environ, buffered=True) |
0 commit comments