Mock Mate is a powerful and flexible API mocking tool that generates API mocks from OpenAPI specifications (supports both YAML and JSON formats). It allows developers to easily simulate API endpoints for testing and development purposes. With built-in customization options, users can update mock responses dynamically based on request properties such as the request body.
- Automatic API Mock Generation: Automatically generate API mocks from OpenAPI specs (YAML/JSON).
- Supports Multiple Formats: Works with both YAML and JSON OpenAPI specs.
- Dynamic Response Updates: Customize API responses on the fly via an update endpoint.
- Conditional Response Matching: Define rules to send specific responses based on request body values.
- Flexible and Extensible: Easily mock any endpoint and customize default responses for your tests.
- Ideal for Development and Testing: Simulate real API behavior in local environments or CI pipelines.
To install Mock Mate, first clone the repository or add it as a dependency in your project.
npm install mock-mate
Mock Mate can use either YAML or JSON OpenAPI specs to generate mocks. Place your OpenAPI spec file in your project.
Example OpenAPI spec (YAML):
openapi: 3.0.0
info:
title: Mock Mate API
version: 1.0.0
paths:
/Consent:
post:
summary: Create a consent
responses:
'200':
description: Consent created
content:
application/json:
example:
ver: '2.0.0'
txnid: '123456789'
timestamp: '2023-06-26T11:39:57.153Z'
ConsentHandle: '654024c8-29c8-11e8-8868-0289437bf331'
'400':
description: Bad Request
content:
application/json:
example:
code: 400
msg: 'Bad Request'
In your project, you can use the getMockMate
function to load the OpenAPI spec file and initialize the mock server.
import { getMockMate } from 'mock-mate';
// Path to your OpenAPI spec file
const filePath = './path-to-your-openapi-spec.yaml';
// Initialize the Mock Mate instance
const mockMate = getMockMate(filePath);
// Start the mock server
mockMate.start();
This will start the mock server with the API paths and endpoints defined in the OpenAPI spec.
You can update mock responses dynamically, including the ability to define conditions based on the request body. When a request matches the defined conditions, the corresponding response will be returned.
For example, to return a 409 Conflict
when the txnid
field in the request body equals "conflict-id"
:
mockMate.updateMockConfig(
'/Consent',
'post',
409,
{ code: 409, msg: 'Conflict: Consent already exists' },
[{ field: 'txnid', value: 'conflict-id' }]
);
In this case, when a POST /Consent
request has { "txnid": "conflict-id" }
in the request body, a 409 Conflict
response will be sent.
You can dynamically update mock responses by sending a request to /mock/update
. Here’s an example:
curl -X POST http://localhost:3000/mock/update -H "Content-Type: application/json" -d '{
"path": "/Consent",
"method": "POST",
"statusCode": 409,
"responseBody": {
"code": 409,
"msg": "Conflict: Consent already exists"
},
"conditions": [
{
"field": "txnid",
"value": "conflict-id"
}
]
}'
This will ensure that a POST /Consent
request with "txnid": "conflict-id"
in the request body will return a 409 Conflict
response.
Mock Mate also includes Docker support for easy deployment in testing environments. You can build the Docker image and run the mock server as a container.
docker build -t mock-mate:latest .
docker run -d -p 3000:3000 mock-mate:latest
The mock server will now be available on http://localhost:3000
.
Contributions are welcome! Please open an issue or submit a pull request if you'd like to contribute to Mock Mate.
This project is licensed under the MIT License. See the LICENSE file for details.
This tool is developed and maintained by S25Digital.
- spec-mate
For robust API testing and validation, check out spec-mate, developed by S25Digital. spec-mate is designed to simplify API testing with comprehensive assertion capabilities, allowing developers to validate status codes, headers, cookies, JSON bodies, and much more.
With spec-mate, you can:
- Validate various aspects of API responses, including JSON paths, headers, and cookies.
- Write custom assertions to extend functionality for advanced API testing.
- Use pre- and post-request hooks for enhanced control and custom logic.
- Integrate with mock-mate to easily mock APIs while running tests with spec-mate.
Combine spec-mate with mock-mate for a complete, end-to-end solution for mocking and testing APIs, making your testing workflows faster and more efficient.