How to use Mountebank for mocks

How to use Mountebank for mocks

Step 1: Install Mountebank

First, you need to have Node.js and npm installed. Then, you can install Mountebank globally via the command line.

1
npm install -g mountebank

Step 2: Define Your Imposter

Create a configuration file to define your mock API. Let’s call it imposters.json.

In this file, we’ll create an imposter that will listen on port 8080. We’ll add a “stub” to it that will respond to a GET request on the /test endpoint.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
"imposters": [
{
"port": 8080,
"protocol": "http",
"stubs": [
{
"responses": [
{
"is": {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"message": "Hello from Mountebank!"
}
}
}
],
"predicates": [
{
"equals": {
"method": "GET",
"path": "/test"
}
}
]
}
]
}
]
}
  • imposters: An array of services you want to mock.
  • port: The port the mock service will run on.
  • protocol: The protocol it will use (e.g., http, https).
  • stubs: A list of rules for how the imposter should respond.
  • predicates: The conditions that a request must meet for the stub to apply. Here, it must be a GET request to /test.
  • responses: The response to send back if the predicates match.

Step 3: Start the Mountebank Server

Now, open your terminal and run the mb command, pointing it to your configuration file.

1
mb --configfile imposters.json

You should see output indicating that Mountebank has started and is listening on port 8080.

1
2
info: [mb:2525] mountebank v2.9.1 now taking orders - point your browser to http://localhost:2525 for help
info: [http:8080] Open for business...

Step 4: Test Your Mock API

With the server running, you can now send a request to your mock endpoint using a tool like curl or Postman.

1
curl http://localhost:8080/test

You will receive the JSON response you defined in your stub:

1
{"message":"Hello from Mountebank!"}

Step 5: Adding More Complex Logic

Let’s add another stub to handle a POST request to the same /test endpoint, but this time we’ll make it respond with whatever JSON body we send to it.

Update your imposters.json file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
"imposters": [
{
"port": 8080,
"protocol": "http",
"stubs": [
{
"responses": [
{
"is": {
"statusCode": 200,
"headers": { "Content-Type": "application/json" },
"body": { "message": "Hello from Mountebank!" }
}
}
],
"predicates": [{ "equals": { "method": "GET", "path": "/test" } }]
},
{
"responses": [
{
"is": {
"statusCode": 201,
"headers": { "Content-Type": "application/json" },
"body": "${request.body}"
}
}
],
"predicates": [{ "equals": { "method": "POST", "path": "/test" } }]
}
]
}
]
}

Notice the new stub uses “${request.body}” in the response body. This tells Mountebank to dynamically use the body of the incoming POST request as the response.

Restart Mountebank with mb –configfile imposters.json –allowInjection (the –allowInjection flag is needed for this dynamic behavior) and test it:

1
curl -X POST http://localhost:8080/test -H "Content-Type: application/json" -d '{"name": "Carl"}'

The mock API will respond with the body you sent:

1
{"name":"Carl"}