def test_register_add_schema_custom_name(): app = FastAPI() middleware.register(app, add_schema='FancyProblem') openapi = app.openapi() assert 'components' in openapi assert 'FancyProblem' in openapi['components']['schemas']
def test_register_add_schema_default(): app = FastAPI() middleware.register(app, add_schema=True) openapi = app.openapi() assert 'components' in openapi assert 'Problem' in openapi['components']['schemas']
def test_register(): app = FastAPI() assert len(app.exception_handlers) == 2 # default handlers assert len(app.user_middleware) == 0 orig_handlers = app.exception_handlers.copy() middleware.register(app) assert len(app.exception_handlers) == 2 assert len(app.user_middleware) == 1 for key in app.exception_handlers: assert app.exception_handlers[key] != orig_handlers[key] openapi = app.openapi() assert 'components' not in openapi
from fastapi_rfc7807.middleware import register def print_error(request: Request, exc: Exception) -> None: print(exc) def add_response_header(request: Request, response: Response, exc: Exception) -> None: response.headers['X-Custom-Header'] = 'foobar' app = FastAPI() register( app=app, pre_hooks=[print_error], post_hooks=[add_response_header], ) @app.get('/error') async def error(): raise ValueError('something went wrong') # Response: # # $ curl -i localhost:8000/error # HTTP/1.1 200 OK # date: Wed, 30 Sep 2020 20:43:32 GMT # server: uvicorn
""" A basic example application showcasing fastapi_rfc7807 with debug enabled. Run from the `examples` directory with: $ uvicorn debug:app """ from fastapi import FastAPI from fastapi_rfc7807.middleware import register app = FastAPI(debug=True) register(app) @app.get('/error') async def error(): raise ValueError('something went wrong') # Response: # # $ curl localhost:8000/error # { # "exc_type": "ValueError", # "type": "about:blank", # "title": "Unexpected Server Error", # "status": 500, # "detail": "something went wrong" # }