Skip to content

Commit 1a66198

Browse files
committed
tests: add test for wsgi middleware handling in flask
1 parent 0fa479d commit 1a66198

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

sentry_sdk/integrations/flask.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ def install(self, client):
2929
appcontext_tearing_down.connect(_pop_appctx)
3030
got_request_exception.connect(_capture_exception)
3131

32-
old_app = Flask.wsgi_app
32+
old_app = Flask.__call__
3333

3434
def sentry_patched_wsgi_app(self, environ, start_response):
3535
return run_wsgi_app(
3636
lambda *a, **kw: old_app(self, *a, **kw), environ, start_response
3737
)
3838

39-
Flask.wsgi_app = sentry_patched_wsgi_app
39+
Flask.__call__ = sentry_patched_wsgi_app
4040

4141

4242
def _push_appctx(*args, **kwargs):

tests/integrations/flask/test_flask.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ def index():
321321
assert event["level"] == "error"
322322

323323

324-
def test_no_errors_without_request(app):
324+
def test_no_errors_without_request(app, sentry_init):
325+
sentry_init(integrations=[flask_sentry.FlaskIntegration()])
325326
with app.app_context():
326327
capture_exception(ValueError())
327328

@@ -340,3 +341,23 @@ def foo():
340341
app.cli.main(
341342
args=["foo"], prog_name="myapp", obj=ScriptInfo(create_app=lambda _: app)
342343
)
344+
345+
346+
def test_wsgi_level_error_is_caught(app, capture_exceptions, sentry_init):
347+
sentry_init(integrations=[flask_sentry.FlaskIntegration()])
348+
349+
def wsgi_app(environ, start_response):
350+
1 / 0
351+
352+
app.wsgi_app = wsgi_app
353+
354+
client = app.test_client()
355+
356+
exceptions = capture_exceptions()
357+
358+
with pytest.raises(ZeroDivisionError) as exc:
359+
client.get("/")
360+
361+
error, = exceptions
362+
363+
assert error is exc.value

0 commit comments

Comments
 (0)