Remove app instance from Config for error handler setting (#2320)

This commit is contained in:
Adam Hopkins
2021-12-18 18:58:14 +02:00
committed by GitHub
parent b5a00ac1ca
commit abe062b371
5 changed files with 164 additions and 77 deletions

View File

@@ -280,40 +280,20 @@ def test_allow_fallback_error_format_set_main_process_start(app):
async def start(app, _):
app.config.FALLBACK_ERROR_FORMAT = "text"
request, response = app.test_client.get("/error")
assert request.app.error_handler.fallback == "text"
_, response = app.test_client.get("/error")
assert response.status == 500
assert response.content_type == "text/plain; charset=utf-8"
def test_setting_fallback_to_non_default_raise_warning(app):
app.error_handler = ErrorHandler(fallback="text")
def test_setting_fallback_on_config_changes_as_expected(app):
app.error_handler = ErrorHandler()
assert app.error_handler.fallback == "text"
with pytest.warns(
UserWarning,
match=(
"Overriding non-default ErrorHandler fallback value. "
"Changing from text to auto."
),
):
app.config.FALLBACK_ERROR_FORMAT = "auto"
assert app.error_handler.fallback == "auto"
_, response = app.test_client.get("/error")
assert response.content_type == "text/html; charset=utf-8"
app.config.FALLBACK_ERROR_FORMAT = "text"
with pytest.warns(
UserWarning,
match=(
"Overriding non-default ErrorHandler fallback value. "
"Changing from text to json."
),
):
app.config.FALLBACK_ERROR_FORMAT = "json"
assert app.error_handler.fallback == "json"
_, response = app.test_client.get("/error")
assert response.content_type == "text/plain; charset=utf-8"
def test_allow_fallback_error_format_in_config_injection():
@@ -327,7 +307,6 @@ def test_allow_fallback_error_format_in_config_injection():
raise Exception("something went wrong")
request, response = app.test_client.get("/error")
assert request.app.error_handler.fallback == "text"
assert response.status == 500
assert response.content_type == "text/plain; charset=utf-8"
@@ -339,6 +318,23 @@ def test_allow_fallback_error_format_in_config_replacement(app):
app.config = MyConfig()
request, response = app.test_client.get("/error")
assert request.app.error_handler.fallback == "text"
assert response.status == 500
assert response.content_type == "text/plain; charset=utf-8"
def test_config_fallback_before_and_after_startup(app):
app.config.FALLBACK_ERROR_FORMAT = "json"
@app.main_process_start
async def start(app, _):
app.config.FALLBACK_ERROR_FORMAT = "text"
_, response = app.test_client.get("/error")
assert response.status == 500
assert response.content_type == "text/plain; charset=utf-8"
def test_config_fallback_bad_value(app):
message = "Unknown format: fake"
with pytest.raises(SanicException, match=message):
app.config.FALLBACK_ERROR_FORMAT = "fake"