Alow Blueprint routes to explicitly define error_format (#2773)

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
This commit is contained in:
Moshe Nahmias
2023-07-09 14:47:59 +03:00
committed by GitHub
parent 976da69e79
commit 5dd1623192
2 changed files with 27 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ import logging
import pytest
import sanic
from sanic import Sanic
from sanic.config import Config
from sanic.errorpages import TextRenderer, exception_response, guess_mime
@@ -205,6 +206,27 @@ def test_route_error_response_from_explicit_format(app):
assert response.content_type == "text/plain; charset=utf-8"
def test_blueprint_error_response_from_explicit_format(app):
bp = sanic.Blueprint("MyBlueprint")
@bp.get("/text", error_format="json")
def text_response(request):
raise Exception("oops")
return text("Never gonna see this")
@bp.get("/json", error_format="text")
def json_response(request):
raise Exception("oops")
return json({"message": "Never gonna see this"})
app.blueprint(bp)
_, response = app.test_client.get("/text")
assert response.content_type == "application/json"
_, response = app.test_client.get("/json")
assert response.content_type == "text/plain; charset=utf-8"
def test_unknown_fallback_format(app):
with pytest.raises(SanicException, match="Unknown format: bad"):
app.config.FALLBACK_ERROR_FORMAT = "bad"