Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mundobien2025_fastapi-basekit /tests /test_exception_handlers.py
| """register_exception_handlers: envelope unificado para toda la app.""" | |
| import pytest | |
| from fastapi import FastAPI | |
| from fastapi.testclient import TestClient | |
| from pydantic import BaseModel | |
| from fastapi_basekit.exceptions import register_exception_handlers | |
| from fastapi_basekit.exceptions.api_exceptions import NotFoundException | |
| from fastapi_basekit.exceptions.domain import DomainError | |
| class InsufficientFundsError(DomainError): | |
| STATUS_CODE_MAP = {"INSUFFICIENT_FUNDS": 402} | |
| def client(): | |
| app = FastAPI() | |
| register_exception_handlers(app) | |
| class Body(BaseModel): | |
| n: int | |
| async def boom_api(): | |
| raise NotFoundException(message="no existe") | |
| async def boom_value(): | |
| raise ValueError("campo malo") | |
| async def boom_domain(): | |
| raise InsufficientFundsError("INSUFFICIENT_FUNDS", "saldo insuficiente") | |
| async def boom_domain_default(): | |
| # code sin mapeo → DEFAULT_STATUS (400) | |
| raise DomainError("SOMETHING", "algo pasó") | |
| async def boom_generic(): | |
| raise RuntimeError("error inesperado") | |
| async def echo(body: Body): | |
| return {"n": body.n} | |
| return TestClient(app, raise_server_exceptions=False) | |
| def test_api_exception_maps_to_envelope(client): | |
| r = client.get("/boom-api") | |
| assert r.status_code == 404 | |
| body = r.json() | |
| assert body["status"] == "NOT_FOUND" | |
| assert body["message"] == "no existe" | |
| assert set(body) == {"data", "message", "status"} | |
| def test_request_validation_maps_to_envelope(client): | |
| r = client.post("/echo", json={"n": "not-an-int"}) | |
| assert r.status_code == 422 | |
| body = r.json() | |
| assert body["status"] == "VALIDATION_ERROR" | |
| assert isinstance(body["data"], list) | |
| def test_value_error_maps_to_envelope(client): | |
| r = client.get("/boom-value") | |
| assert r.status_code == 400 | |
| assert r.json()["status"] == "VALUE_ERROR" | |
| def test_domain_error_maps_to_declared_status(client): | |
| """DomainError que escapa del endpoint → su STATUS_CODE_MAP, no 500.""" | |
| r = client.get("/boom-domain") | |
| assert r.status_code == 402 | |
| body = r.json() | |
| assert body["status"] == "INSUFFICIENT_FUNDS" | |
| assert body["message"] == "saldo insuficiente" | |
| assert set(body) == {"data", "message", "status"} | |
| def test_domain_error_default_status(client): | |
| r = client.get("/boom-domain-default") | |
| assert r.status_code == 400 # DEFAULT_STATUS | |
| assert r.json()["status"] == "SOMETHING" | |
| def test_cors_headers_injected_on_error(client): | |
| """Todas las respuestas de error incluyen headers CORS cuando el request | |
| tiene un header ``Origin`` (defensa en profundidad).""" | |
| r = client.get( | |
| "/boom-generic", | |
| headers={"Origin": "https://app.example.com"}, | |
| ) | |
| assert r.status_code == 500 | |
| assert r.headers.get("Access-Control-Allow-Origin") == "https://app.example.com" | |
| assert r.headers.get("Access-Control-Allow-Credentials") == "true" | |
| assert r.headers.get("Vary") == "Origin" | |
Xet Storage Details
- Size:
- 3.16 kB
- Xet hash:
- 46896021154b294144857bd1416320ae3812542a76e91a1ba03ab7a64b4d4e00
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.