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.
- Key Features
- Available Providers
- Installation
- Examples
- How the Mock Mapping System Works
- Getting Started
- Contributing
- License
- 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
- MockResponse.Middleware.Azure.BlobStorage - Provider using Azure Blob Storage for remote mocks
- MockResponse.Middleware.LocalFolderStore - Provider serving mocks from a local file system, perfect for offline scenarios
Note: The
Core
package is only required when creating custom providers; it is automatically included with the Azure and Local Folder providers.
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.
Detailed examples demonstrating provider integrations can be found here:
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.
Quick setup steps:
- Install middleware provider packages.
- Configure your desired mock provider in
appsettings.json
and/or Secrets Manager. - Register middleware in application startup:
services.AddApiMocking(Configuration)
.AddAzureBlobStorage(); // or .AddLocalFolderStore()
- Enable middleware in your pipeline:
app.UseApiMocking();
For comprehensive details, troubleshooting, and provider-specific configurations, see:
- Core Middleware Documentation
- Azure Blob Storage Provider Documentation
- Local Folder Store Provider Documentation
Contributions and feedback are welcomed and greatly appreciated! Open an issue or submit a pull request to enhance the project.
This project is licensed under the MIT License.