Skip to main content

FastAPI middleware for mocking responses based on Pydantic models response

Project description

fastmock

FastMock framework, mock your FastAPI APIs

Test Coverage Package version


Documentation: https://tpemeja.github.io/fastmock

Source Code: https://github.com/tpemeja/fastmock


FastMock is a Python framework designed to mock FastAPI APIs based on response models.

Key features of this project include:

  • Ease of Use: Simply add the middleware to start mocking your APIs.
  • Flexibility: Mocking can be customized through various parameters, such as activation, data generation, length, status codes, and more.
  • Modularity: The package includes default functions to retrieve mocking parameters from API declarations to request headers. However, all these functions can be modified, allowing you to create your own custom functions.

Requirements

This package is build to be used with FastAPI, please learn on to use it on their well documented website - FastAPI Documentation

To mock an API, you need to describe the API using FastAPI's decorator key responses.

For example:

@app.get("/",
         status_code=status.HTTP_200_OK,
         responses={
             status.HTTP_200_OK: {"model": list[int]},
             status.HTTP_404_NOT_FOUND: {"model": str}
         })

Installation

$ pip install fastmock

---> 100%

Example

Create it

  • Create a file main.py with:
from fastapi import FastAPI, status
from pydantic import BaseModel

from fastmock.decorator import FastMockDecorator
from fastmock.middleware import FastMockMiddleware

app = FastAPI()
app.add_middleware(FastMockMiddleware)


mock = FastMockDecorator()

class Item(BaseModel):
    name: str
    price: float
    is_offer: bool | None = None


@app.get("/",
         status_code=status.HTTP_200_OK,
         responses={
             status.HTTP_200_OK: {"model": dict[str, str]}
         })
def read_root():
    return {"Hello": "World"}


@mock(element_size=3)
@app.get("/items",
         status_code=status.HTTP_200_OK,
         responses={
             status.HTTP_200_OK: {"model": list[Item]}
         })
def read_items():
    return []
  • Start the server with uvicorn main:app

Check it

Open your browser at http://127.0.0.1:8000/items.

You will see the JSON response as:

[
  {
    "name": "fuquvERvYTfWVEbYRKgi",
    "price": 18164954265977.8,
    "is_offer": null
  },
  {
    "name": "akMCejCxOhMjgGMPMrcb",
    "price": 40.3130726635657,
    "is_offer": null
  },
  {
    "name": "uEONHBXGCirPDrLJKgXu",
    "price": -9.23356705084994,
    "is_offer": null
  }
]

You just created an API using FastAPI that:

  • Return value generated from defined response model
  • Modify output list size using decorator

Inspiration

  • A project idea that came from my use of FastAPI by tiangolo, the GitHub project inspired me for the structure and documentation of this project.
  • Ideas and code for data generation with Faker inspired by NiyazNz in the fastapi-mock-middleware project.

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastmock-0.1.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

fastmock-0.1.1-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file fastmock-0.1.1.tar.gz.

File metadata

  • Download URL: fastmock-0.1.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for fastmock-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4a34b49ace77cd2f243531c350fee6cc65dee7b558125fab39e88e9bbff11208
MD5 b1314e1c1f4916f32c8852d221bf4896
BLAKE2b-256 be55bc3c3b1bbfd4565dd45547a775f139ea414a90add5456c4cb13c5a576636

See more details on using hashes here.

File details

Details for the file fastmock-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fastmock-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for fastmock-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5bfd5f7069d07819c8162f986db25eb462d3ac9df5d45d0e1e981f5bb1d26c89
MD5 aa4505f168f957d95bad1f2b0fb4eb4b
BLAKE2b-256 03c31ccc12c7aacab8827093d3a1efa632c962ca51fc4802e93d8b164dd3dc77

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page