Skip to content

Commit

Permalink
Test with Flask 3.0 (#2506)
Browse files Browse the repository at this point in the history
- run test suite with Flask 3.0
- fix `request.get_json()` in the tests (Flask/Werkzeug 3.0 now throws an `UnsupportedMediaType` exception if the `Content-Type` isn't `application/json`)
  • Loading branch information
sentrivana authored Nov 15, 2023
1 parent 44b0244 commit 5a6b5d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
40 changes: 31 additions & 9 deletions tests/integrations/flask/test_flask.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import json
import re
import pytest
import logging

from io import BytesIO

import pytest
from flask import (
Flask,
Response,
Expand All @@ -14,9 +13,14 @@
render_template_string,
)
from flask.views import View

from flask_login import LoginManager, login_user

try:
from werkzeug.wrappers.request import UnsupportedMediaType
except ImportError:
UnsupportedMediaType = None

import sentry_sdk.integrations.flask as flask_sentry
from sentry_sdk import (
set_tag,
configure_scope,
Expand All @@ -26,7 +30,6 @@
Hub,
)
from sentry_sdk.integrations.logging import LoggingIntegration
import sentry_sdk.integrations.flask as flask_sentry
from sentry_sdk.serializer import MAX_DATABAG_BREADTH


Expand Down Expand Up @@ -340,7 +343,11 @@ def test_flask_medium_formdata_request(sentry_init, capture_events, app):
def index():
assert request.form["foo"] == data["foo"]
assert not request.get_data()
assert not request.get_json()
try:
assert not request.get_json()
except UnsupportedMediaType:
# flask/werkzeug 3
pass
capture_message("hi")
return "ok"

Expand Down Expand Up @@ -372,7 +379,11 @@ def index():
assert request.form["username"] == data["username"]
assert request.form["age"] == data["age"]
assert not request.get_data()
assert not request.get_json()
try:
assert not request.get_json()
except UnsupportedMediaType:
# flask/werkzeug 3
pass
set_tag("view", "yes")
capture_message("hi")
return "ok"
Expand Down Expand Up @@ -405,7 +416,11 @@ def index():
assert request.get_data() == data
else:
assert request.get_data() == data.encode("ascii")
assert not request.get_json()
try:
assert not request.get_json()
except UnsupportedMediaType:
# flask/werkzeug 3
pass
capture_message("hi")
return "ok"

Expand All @@ -431,7 +446,11 @@ def test_flask_files_and_form(sentry_init, capture_events, app):
def index():
assert list(request.form) == ["foo"]
assert list(request.files) == ["file"]
assert not request.get_json()
try:
assert not request.get_json()
except UnsupportedMediaType:
# flask/werkzeug 3
pass
capture_message("hi")
return "ok"

Expand Down Expand Up @@ -545,9 +564,12 @@ def test_cli_commands_raise(app):
def foo():
1 / 0

def create_app(*_):
return app

with pytest.raises(ZeroDivisionError):
app.cli.main(
args=["foo"], prog_name="myapp", obj=ScriptInfo(create_app=lambda _: app)
args=["foo"], prog_name="myapp", obj=ScriptInfo(create_app=create_app)
)


Expand Down
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ envlist =
{py2.7,py3.5,py3.6,py3.7,py3.8,py3.9}-flask-v{0.11,0.12,1.0}
{py2.7,py3.5,py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-flask-v{1.1}
{py3.6,py3.8,py3.9,py3.10,py3.11,py3.12}-flask-v{2.0}
{py3.10,py3.11,py3.12}-flask-v{3.0}

# Gevent
{py2.7,py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-gevent
Expand Down Expand Up @@ -333,12 +334,14 @@ deps =

# Flask
flask: flask-login
flask: Werkzeug<2.1.0
flask-v{0.11,0.12,1.0,1.1,2.0}: Werkzeug<2.1.0
flask-v{3.0}: Werkzeug
flask-v0.11: Flask>=0.11,<0.12
flask-v0.12: Flask>=0.12,<0.13
flask-v1.0: Flask>=1.0,<1.1
flask-v1.1: Flask>=1.1,<1.2
flask-v2.0: Flask>=2.0,<2.1
flask-v3.0: Flask>=3.0,<3.1

# Gevent
# See http://www.gevent.org/install.html#older-versions-of-python
Expand Down

0 comments on commit 5a6b5d4

Please sign in to comment.