Skip to content

Flexible ASP.NET Core middleware for mock API responses with support for local folders, Azure Blob Storage, and custom providers.

License

Notifications You must be signed in to change notification settings

BrandonSchreck/mockresponse-middleware

Repository files navigation

MockResponse.Middleware

.NET License

CI/CD Pipeline CodeQL

MockResponse.Middleware is a flexible and lightweight ASP.NET Core middleware solution designed to simplify serving mock responses during development, testing, or offline scenarios. It conditionally intercepts HTTP requests and returns configured mock responses based on defined mappings, request headers, and endpoint metadata, without altering backend logic.

✅ Tested with .NET 8 (ASP.NET Core Minimal API). Compatibility with earlier versions has not been verified.

Table of Contents

🚀 Key Features

  • Plug-and-play Provider Model: Easily integrate mock responses using Azure Blob Storage, local file systems, or implement your own custom providers
  • Variant Handling: Effortlessly switch response variations with the X-Mock-Variant header
  • Status Code Simulation: Test and handle different HTTP status codes by specifying them with the X-Mock-Status header
  • OpenAPI Integration: Utilize the mapping system powered by OpenAPI's Produces<T>() response types to automagically match response payloads

(back to top)

📚 Available Providers

Note: The Core package is only required when creating custom providers; it is automatically included with the Azure and Local Folder providers.

(back to top)

📦 Installation

Install your desired provider via .NET CLI:

dotnet add package MockResponse.Middleware.Azure.BlobStorage

OR

dotnet add package MockResponse.Middleware.LocalFolderStore

Important: Only one provider may be registered. Attempting to register more than one will throw a runtime exception.

(back to top)

💡 Examples

Detailed examples demonstrating provider integrations can be found here:

(back to top)

🔗 How the Mock Mapping System Works

Mock responses are selected based on:

  • HTTP Status Code (X-Mock-Status header)
  • Optional Variant (X-Mock-Variant header)
  • Response Type Metadata (.Produces<T>())
GET /weatherforecast
  X-Mock-Status: 200 // HTTP Status to lookup
app.MapGet("/weatherforecast", () => ...)
    .Produces<WeatherForecast>() // 200 - Object Type to be used
    .WithName("GetWeatherForecast");

The middleware creates a fully qualified lookup key from these inputs, referencing entries in your appsettings.json:

"MockOptions": {
  "ResponseMappings": {
    "Namespace.WeatherForecast": "WeatherForecast.json", // JSON to return
    "Namespace.WeatherForecast.VariantName": "WeatherForecast.Variant.json"
  }
}

For detailed mapping examples and explanations, refer to the Mock Resolution Strategy documentation.

(back to top)

🚦 Getting Started

Quick setup steps:

  1. Install middleware provider packages.
  2. Configure your desired mock provider in appsettings.json and/or Secrets Manager.
  3. Register middleware in application startup:
services.AddApiMocking(Configuration)
    .AddAzureBlobStorage(); // or .AddLocalFolderStore()
  1. Enable middleware in your pipeline:
app.UseApiMocking();

For comprehensive details, troubleshooting, and provider-specific configurations, see:

(back to top)

📌 Contributing

Contributions and feedback are welcomed and greatly appreciated! Open an issue or submit a pull request to enhance the project.

📝 License

This project is licensed under the MIT License.

About

Flexible ASP.NET Core middleware for mock API responses with support for local folders, Azure Blob Storage, and custom providers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages