Reference
fastapi_mock_middleware.MockAPIMiddleware
¶
MockAPIMiddleware(app, not_implemented_error_class=APINotImplementedError, content_mocked_header_name='X-Content-Mocked', default_list_size=10)
Mock API middleware
Mocks unimplemented endpoint responses with generated data according to their response models.
PARAMETER | DESCRIPTION |
---|---|
app |
ASGI application
|
not_implemented_error_class |
Exception class on raising which
middleware returns mocked response: Default:
TYPE:
|
content_mocked_header_name |
response header name indicating that the
response has been mocked. Default:
TYPE:
|
default_list_size |
List size to generate for list responses.
TYPE:
|
Usage example
app = FastAPI()
app.add_middleware(MockAPIMiddleware)
Usage example with arguments
app = FastAPI()
app.add_middleware(
MockAPIMiddleware,
not_implemented_error_class=APINotImplementedError,
content_mocked_header_name='X-Content-Mocked',
default_list_size=10,
)
Source code in fastapi_mock_middleware/middleware.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
get_content
async
¶
get_content(scope, list_size=10)
Get generated content conforming to the route response model
Source code in fastapi_mock_middleware/middleware.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
fastapi_mock_middleware.APINotImplementedError
¶
APINotImplementedError(*, return_value=None, list_size=None)
Bases: NotImplementedError
API NotImplementedError
Exception class to be risen on unimplemented endpoints for mocking their
response data by MockAPIMiddleware
PARAMETER | DESCRIPTION |
---|---|
return_value |
Value to return as mock data. Optional, in most cases autogenerated data should be enough. Use when specific data is required or in complex cases where data mocking according to response model did not work as expected.
TYPE:
|
list_size |
List size to generate for list responses.
TYPE:
|
Source code in fastapi_mock_middleware/middleware.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|