Skip to content

Commit 327eefe

Browse files
committed
test(wsgi): Complete 100% coverage for _werkzeug
1 parent 6b4bb5d commit 327eefe

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

tests/integrations/wsgi/test_wsgi.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sentry_sdk
88
from sentry_sdk import capture_message
99
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
10-
from sentry_sdk._werkzeug import get_host
10+
from sentry_sdk._werkzeug import get_host, _get_headers
1111

1212

1313
@pytest.fixture
@@ -76,9 +76,24 @@ def next(self):
7676
"ham",
7777
id="ignore x-forwarded-host",
7878
),
79+
pytest.param(
80+
{
81+
"SERVER_NAME": "2001:0db8:85a3:0042:1000:8a2e:0370:7334",
82+
"SERVER_PORT": "8080",
83+
},
84+
"[2001:0db8:85a3:0042:1000:8a2e:0370:7334]:8080",
85+
id="IPv6, custom port",
86+
),
87+
pytest.param(
88+
{"SERVER_NAME": "eggs"},
89+
"eggs",
90+
id="name, no port",
91+
),
7992
),
8093
)
94+
#
8195
# https://github.com/pallets/werkzeug/blob/main/tests/test_wsgi.py#L60
96+
#
8297
def test_get_host(environ, expect):
8398
environ.setdefault("wsgi.url_scheme", "http")
8499
assert get_host(environ) == expect
@@ -106,26 +121,19 @@ def test_basic(sentry_init, crashing_app, capture_events):
106121
}
107122

108123

109-
def test_basic_forwarded_host(sentry_init, crashing_app, capture_events):
110-
sentry_init(send_default_pii=True)
111-
app = SentryWsgiMiddleware(crashing_app, use_x_forwarded_for=True)
112-
client = Client(app)
113-
events = capture_events()
114-
115-
with pytest.raises(ZeroDivisionError):
116-
client.get("/", environ_overrides={"HTTP_X_FORWARDED_HOST": "foobarbaz:80"})
117-
118-
(event,) = events
119-
120-
assert event["transaction"] == "generic WSGI request"
121-
122-
assert event["request"] == {
123-
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
124-
"headers": {"Host": "localhost", "X-Forwarded-Host": "foobarbaz:80"},
125-
"method": "GET",
126-
"query_string": "",
127-
"url": "http://foobarbaz/",
128-
}
124+
@pytest.mark.parametrize(
125+
("environ", "expect"),
126+
(
127+
pytest.param(
128+
{"CONTENT_TYPE": "text/html", "CONTENT_LENGTH": "0"},
129+
[("Content-Length", "0"), ("Content-Type", "text/html")],
130+
id="headers",
131+
),
132+
),
133+
)
134+
def test_headers(environ, expect):
135+
environ.setdefault("wsgi.url_scheme", "http")
136+
assert sorted(_get_headers(environ)) == expect
129137

130138

131139
@pytest.mark.parametrize("path_info", ("bark/", "/bark/"))

0 commit comments

Comments
 (0)