Mmock is an HTTP mocking application for testing and fast prototyping
Mmock is an HTTP mocking application for testing and fast prototyping
=========
Mmock is a testing and fast prototyping tool for developers:
Easy and fast HTTP mock server.
Built with Go - Mmock runs without installation on multiple platforms.
Mock definition file example:
{
"request": {
"method": "GET",
"path": "/hello/*"
},
"response": {
"statusCode": 200,
"headers": {
"Content-Type":["application/json"]
},
"body": "{\"hello\": \"{{request.query.name}}, my name is {{fake.FirstName}}\"}"
}
}
Or
---
request:
method: GET
path: "/hello/*"
response:
statusCode: 200
headers:
Content-Type:
- application/json
body: '{"hello": "{{request.query.name}}, my name is {{fake.FirstName}}"}'
You can see more complex examples here.
Either:
Run it from Docker using the provided Dockerfile or from Docker Hub
docker image pull jordimartin/mmock
docker run -v YOUR_ABS_PATH:/config -p 8082:8082 -p 8083:8083 jordimartin/mmockOr run mmock locally from the command line. (Requires Go 1.8 at least)
go get github.com/jmartin82/mmock/...
mmock -h
To configure Mmock, use command line flags described in help.
…The default logging level is INFO, but you can change it by setting the environment variable LOG_LEVEL to one of the following:
Mock definition:
…A core feature of Mmock is the ability to return canned HTTP responses for requests matching criteria.
In case of queryStringParameters, headers and cookies, the request can be matched only if all defined keys in mock will be present with the exact or glob value.
Glob matching is available for:
Query strings and headers support also global matches (*) in the header/parameter name. For example:
"headers": {
"Custom-Header-*": [
"partial val*"
]
}Regexp matching is available for:
See https://pkg.go.dev/regexp/syntax for regexp syntax
This is used to have mmock make an API request after receiving the mocked request.
You can use variable data in response. The variables will be defined as tags like this {{nameVar}}
Request data: Use them if you want to add request data in your response.
You can extract information from the request body too, using a dot notation path:
application/json, application/xml and application/x-www-form-urlencoded requests)application/json, application/xml requests)Quick overview of the path syntax available to extract values form the request: https://github.com/tidwall/gjson#path-syntax
You can also use "regex" and "concat" commands to complement GJson query:
application/json, application/xml requests)application/json, application/xml requests)application/json, application/xml requests)Example request data:
{
"email": "[email protected]",
"age": 4,
"uuid":"^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$",
"discarded": "do not return"
}Example config mock data:
{
"email": "{{request.body.email.regex((\@gmail.com))}}",
"age": {{request.body.age}},
"uuid": "{{request.body.uuid.regex(\b([0-9a-zA-Z]{4})\b).concat(-878787)}}",
"discarded": "{{request.body.discarded.concat(, Please!)}}"
}Example response data:
{
"email": "",
"age": 4,
"uuid": "2307-878787",
"discarded": "do not return, Please!"
}External streams: Perfect for embedding big payloads or getting data from another service.
Fake data: Useful to provide a more rich and random response.
With the scenarios you can simulate a stateful service. It's useful to create test doubles.
A scenario is a state machine and you can assign an arbitrarily state.
When mmock recieve a new request and there is an scenario defined in the matching mock, mmock checks if the mock is valid for the current state. Also a new scenario state can be set after the mock match.
By default all scenarios has the state "not_started" until some mock triggers a new one.
Example of REST services using scenarios:
…Working examples here
MMock uses the Open API Specification (OAI, formerly known as Swagger) to describe its APIs. Our OAI specification schema is hosted at /swagger.json and serves as the canonical definition and comprehensive declaration of all available endpoints.
The OAI specification makes writing client applications easier by: auto-generating boilerplate code (like data object classes) and dealing with authentication and error handling.
You can find a comprehensive set of open tools for the OAI specification at: https://github.com/swagger-api.
The Mmock records the incoming requests in memory (last 100 by default). This makes it possible to verify that a request matching a specific pattern was received, and also to fetch the requests details.
Title : Get all requests.
URL : /api/request/all
Method : GET
Response Codes: Success (200 OK)
Title : Get all matched requests with any mock.
URL : /api/request/matched
Method : GET
Response Codes: Success (200 OK)
Title : Clean all recorded request.
URL : /api/request/reset
Method : GET
Response Codes: Success (200 OK)
Title : Reset all requests that match with an specific pattern.
URL : /api/request/reset_match
Method : POST
Data Params:
Like stubbing this call also uses the same DSL to filter and query requests.
{
"host": "example.com",
"method": "GET|POST|PUT|PATCH|... (Mandatory)",
"path": "/your/path/:variable (Mandatory)",
"queryStringParameters": {
"name": ["value"],
"name": ["value", "value"]
},
"headers": {
"name": ["value"]
},
"cookies": {
"name": "value"
},
"body": "Expected Body"
}Response Codes: Success (200 OK)
Title : Get all non matched requests.
URL : /api/request/unmatched
Method : GET
Response Codes: Success (200 OK)
Title : Get all requests that match with an specific pattern.
URL : /api/request/verify
Method : POST
Data Params:
Like stubbing this call also uses the same DSL to filter and query requests.
{
"host": "example.com",
"method": "GET|POST|PUT|PATCH|... (Mandatory)",
"path": "/your/path/:variable (Mandatory)",
"queryStringParameters": {
"name": ["value"],
"name": ["value", "value"]
},
"headers": {
"name": ["value"]
},
"cookies": {
"name": "value"
},
"body": "Expected Body"
}Response Codes: Success (200 OK)
Title : Get all active scenarios.
URL : /api/scenarios
Method : GET
Response Codes: Success (200 OK)
Title : Clean all scenarios status and pause state.
URL : /api/scenarios/reset_all
Method : GET
Response Codes: Success (200 OK)
Title : Manually progress a scenario state machine to the given state.
URL : /api/scenarios/set/:scenario/:state
Method : PUT
Response Codes: Success (200 OK)
Title : Pause prevents all scenarios st
No open issues yet, or sync has not completed.