Gathering detailed insights and metrics for mock-mate
Gathering detailed insights and metrics for mock-mate
npm install mock-mate
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last day
-66.7%
1
Compared to previous day
Last week
100%
18
Compared to previous week
Last month
1,800%
38
Compared to previous month
Last year
0%
255
Compared to previous year
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.
To install Mock Mate, first clone the repository or add it as a dependency in your project.
1npm 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):
1openapi: 3.0.0 2info: 3 title: Mock Mate API 4 version: 1.0.0 5paths: 6 /Consent: 7 post: 8 summary: Create a consent 9 responses: 10 '200': 11 description: Consent created 12 content: 13 application/json: 14 example: 15 ver: '2.0.0' 16 txnid: '123456789' 17 timestamp: '2023-06-26T11:39:57.153Z' 18 ConsentHandle: '654024c8-29c8-11e8-8868-0289437bf331' 19 '400': 20 description: Bad Request 21 content: 22 application/json: 23 example: 24 code: 400 25 msg: 'Bad Request'
In your project, you can use the getMockMate
function to load the OpenAPI spec file and initialize the mock server.
1import { getMockMate } from 'mock-mate'; 2 3// Path to your OpenAPI spec file 4const filePath = './path-to-your-openapi-spec.yaml'; 5 6// Initialize the Mock Mate instance 7const mockMate = getMockMate(filePath); 8 9// Start the mock server 10mockMate.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"
:
1mockMate.updateMockConfig( 2 '/Consent', 3 'post', 4 409, 5 { code: 409, msg: 'Conflict: Consent already exists' }, 6 [{ field: 'txnid', value: 'conflict-id' }] 7);
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:
1curl -X POST http://localhost:3000/mock/update -H "Content-Type: application/json" -d '{ 2 "path": "/Consent", 3 "method": "POST", 4 "statusCode": 409, 5 "responseBody": { 6 "code": 409, 7 "msg": "Conflict: Consent already exists" 8 }, 9 "conditions": [ 10 { 11 "field": "txnid", 12 "value": "conflict-id" 13 } 14 ] 15}'
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.
1docker build -t mock-mate:latest .
1docker 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.
No vulnerabilities found.
No security vulnerabilities found.