Skip to content

Commit 777500b

Browse files
authored
Merge pull request #1518 from NiklasMM/fix/1510_lint-middleware-python3-compatibility
Make LintMiddleware Python 3 compatible and add tests
2 parents d590cc7 + e00c7c2 commit 777500b

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Unreleased
1616
- Fix the filename format string in
1717
:class:`~middleware.profiler.ProfilerMiddleware` to correctly handle
1818
float values. (:issue:`1511`)
19+
- Update :class:`~middleware.lint.LintMiddleware` to work on Python 3.
20+
(:issue:`1510`)
1921

2022

2123
Version 0.15.2

src/werkzeug/middleware/lint.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"""
1515
from warnings import warn
1616

17+
from .._compat import implements_iterator
18+
from .._compat import PY2
1719
from .._compat import string_types
1820
from ..datastructures import Headers
1921
from ..http import is_entity_header
@@ -124,18 +126,22 @@ def __call__(self, s):
124126
self._chunks.append(len(s))
125127

126128

129+
@implements_iterator
127130
class GuardedIterator(object):
128131
def __init__(self, iterator, headers_set, chunks):
129132
self._iterator = iterator
130-
self._next = iter(iterator).next
133+
if PY2:
134+
self._next = iter(iterator).next
135+
else:
136+
self._next = iter(iterator).__next__
131137
self.closed = False
132138
self.headers_set = headers_set
133139
self.chunks = chunks
134140

135141
def __iter__(self):
136142
return self
137143

138-
def next(self):
144+
def __next__(self):
139145
if self.closed:
140146
warn("Iterated over closed 'app_iter'.", WSGIWarning, stacklevel=2)
141147

tests/middleware/test_lint.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy