Overview
Themock middleware provides request mocking capabilities for testing and development, returning predefined responses for specified routes.
Use it when you need:
- API mocking for frontend development
- Test fixtures
- Stubbing external services
Installation
Quick Start
Configuration
Routes Map
Examples
JSON Response
Multiple Routes
With Headers
From File
Conditional Mocking
Delay Response
API Reference
Functions
Technical Details
Implementation Overview
The mock middleware is built on a thread-safe architecture usingsync.RWMutex for concurrent access to mock responses. It supports both path-only and method-specific mocking patterns.
Core Components:
- Mock struct: Central registry holding mock responses with thread-safe access
- Response matching: Two-tier matching system (method+path, then path-only)
- Passthrough mode: Configurable behavior for unmatched requests
- Extract request path and method from incoming request
- Acquire read lock for concurrent-safe access
- Check method-specific mocks first (
methods[method][path]) - Fall back to path-only mocks (
mocks[path]) - Apply default response if configured
- Either pass through to next handler or return 404
JSON(): Marshal Go data structures to JSON with appropriate headersText(): Plain text responsesHTML(): HTML responses with proper content typeFile(): Generic file responses with custom content typesRedirect(): HTTP redirects with location headersError(): Standardized error responses in JSON format
Best Practices
- Use environment variable to enable/disable
- Keep mock data in separate files
- Match production response structure
- Add realistic delays for latency testing
Testing
The mock middleware includes comprehensive test coverage for all major functionality:| Test Case | Description | Expected Behavior |
|---|---|---|
| TestNew | Creates middleware with predefined mocks | Returns mocked JSON response instead of real handler |
| TestMock_Register | Registers a mock response for a path | Mock response is served for registered path |
| TestMock_RegisterMethod | Registers method-specific mock | POST request returns mocked response with status 201 |
| TestMock_Clear | Clears all registered mocks | After clear, real handler is called instead of mock |
| TestWithOptions_DefaultResponse | Uses default response for unmatched paths | Returns default response (503) for any unmatched path |
| TestJSON | Creates JSON response helper | Response has correct status and JSON content type |
| TestText | Creates text response helper | Response contains correct text body and content type |
| TestHTML | Creates HTML response helper | Response has text/html content type |
| TestRedirect | Creates redirect response | Response has correct redirect status and Location header |
| TestError | Creates error response helper | Returns JSON error response with correct status |
| TestPrefix | Mocks all paths matching prefix | Prefixed paths return mock, non-prefixed pass through |
| TestPassthrough | Passes unmatched requests to next handler | Real handler is called when no mock matches |