From 4c47081a66c96a316fd01e1a2d60e76f967b7777 Mon Sep 17 00:00:00 2001 From: pgjones Date: Wed, 1 Jan 2025 19:42:31 +0000 Subject: [PATCH] Switch from ResponseHeadersValidationError to RuntimeError Response instances cannot be validated, and hence it is considered a programming error (RuntimeError) to try. It isn't clear why b5c610ba89a93b260edbd6ffe68eac68e7574338 choose a ResponseHeadersValidationError, but it doesn't make sense as the exception type. --- src/quart_schema/validation.py | 2 +- tests/test_validation.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/quart_schema/validation.py b/src/quart_schema/validation.py index 2f1a37d..c18c106 100644 --- a/src/quart_schema/validation.py +++ b/src/quart_schema/validation.py @@ -223,7 +223,7 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any: if isinstance(value, (Response, WerkzeugResponse)): if status == status_code: - raise ResponseHeadersValidationError() + raise RuntimeError("Cannot validate Response instance") else: return result diff --git a/tests/test_validation.py b/tests/test_validation.py index 5a32ca3..a201911 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -211,6 +211,20 @@ async def item() -> ResponseReturnValue: assert response.status_code == 302 +async def test_response_validation_of_response() -> None: + app = Quart(__name__) + QuartSchema(app) + + @app.route("/") + @validate_response(PyItem) + async def item() -> ResponseReturnValue: + return Response(200) + + test_client = app.test_client() + response = await test_client.get("/") + assert response.status_code == 500 + + @pytest.mark.parametrize( "return_value, status", [