https://github.com/niyaznz/fastapi-mock-middleware
FastAPI middleware for mocking response data of non-implemented endpoints
https://github.com/niyaznz/fastapi-mock-middleware
api-first fastapi mock mock-api mock-fastapi mock-response
Last synced: 19 days ago
JSON representation
FastAPI middleware for mocking response data of non-implemented endpoints
- Host: GitHub
- URL: https://github.com/niyaznz/fastapi-mock-middleware
- Owner: NiyazNz
- License: mit
- Created: 2024-03-19T17:45:27.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-09T19:42:13.000Z (almost 2 years ago)
- Last Synced: 2025-02-19T10:48:37.089Z (over 1 year ago)
- Topics: api-first, fastapi, mock, mock-api, mock-fastapi, mock-response
- Language: Python
- Homepage: https://niyaznz.github.io/fastapi-mock-middleware/
- Size: 584 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# FastAPI mock middleware
[](https://github.com/NiyazNz/fastapi-mock-middleware/blob/main/LICENSE.txt)
[](https://pypi.org/project/fastapi-mock-middleware/)
[](https://github.com/NiyazNz/fastapi-mock-middleware/actions/workflows/test.yml)
[](https://codecov.io/gh/NiyazNz/fastapi-mock-middleware)
FastAPI middleware for mocking response data of non-implemented endpoints.
Mock data is generated in accordance with endpoint return type or provided
response_model using [polifactory](https://github.com/litestar-org/polyfactory).
---
For more information on how to use fastapi-mock-middleware, please refer to the
[official documentation](https://niyaznz.github.io/fastapi-mock-middleware/).
## Installation
```shell
pip install fastapi-mock-middleware
```
## Usage example
Add `MockAPIMiddleware` middleware to app and raise `APINotImplementedError` in
your endpoint stubs.
```Python hl_lines="8 18"
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi_mock_middleware import MockAPIMiddleware, APINotImplementedError
app = FastAPI()
app.add_middleware(MockAPIMiddleware)
class Item(BaseModel):
id: int
name: str
@app.get('/')
async def list_items() -> list[Item]:
raise APINotImplementedError()
if __name__ == '__main__':
uvicorn.run('example:app', reload=True)
```
Check the response using `curl`.
```shell
curl http://127.0.0.1:8000/
```
Called API must return mocked data:
```json
[
{
"id": 5392,
"name": "gVzyVVUmGGevXlQvXGBW"
}
]
```