diff --git a/backend/tests/test_error_handling.py b/backend/tests/test_error_handling.py index 3f922101..3a72a155 100644 --- a/backend/tests/test_error_handling.py +++ b/backend/tests/test_error_handling.py @@ -4,6 +4,7 @@ from __future__ import annotations import pytest from fastapi import FastAPI, HTTPException +from fastapi.exceptions import RequestValidationError, ResponseValidationError from fastapi.testclient import TestClient from pydantic import BaseModel, Field from starlette.requests import Request @@ -16,6 +17,8 @@ from app.core.error_handling import ( _http_exception_exception_handler, _request_validation_exception_handler, _response_validation_exception_handler, + _request_validation_handler, + _response_validation_handler, install_error_handling, ) @@ -203,3 +206,47 @@ async def test_http_exception_wrapper_rejects_wrong_exception() -> None: req = Request({"type": "http", "headers": [], "state": {}}) with pytest.raises(TypeError, match="Expected StarletteHTTPException"): await _http_exception_exception_handler(req, Exception("x")) + + +@pytest.mark.asyncio +async def test_request_validation_handler_includes_request_id() -> None: + req = Request({"type": "http", "headers": [], "state": {"request_id": "req-1"}}) + exc = RequestValidationError( + [ + { + "loc": ("query", "limit"), + "msg": "value is not a valid integer", + "type": "type_error.integer", + } + ] + ) + + resp = await _request_validation_handler(req, exc) + assert resp.status_code == 422 + assert resp.body + + +@pytest.mark.asyncio +async def test_response_validation_handler_includes_request_id() -> None: + req = Request( + { + "type": "http", + "method": "GET", + "path": "/x", + "headers": [], + "state": {"request_id": "req-2"}, + } + ) + exc = ResponseValidationError( + [ + { + "loc": ("response", "name"), + "msg": "field required", + "type": "value_error.missing", + } + ] + ) + + resp = await _response_validation_handler(req, exc) + assert resp.status_code == 500 + assert resp.body