Creating the imposter configuration of mountebank using the recorded stub

Poopae Paisingkhon
Ascend Developers
Published in
4 min readJul 20, 2020

--

Lately, I am interested in one of the magic features of mountebank that is proxy. Most people config the proxy as default stub, because if the request does not match any predicate, it will be forwarded to the downstream service. Proxy is not only used for configuring the endpoint to forward the request to downstream service, but also for configuring the record and replay behavior of the response following our test scenarios.

Figure 1. the recorded and replayed behavior

Proxy has the mode parameter. The default proxy mode is ProxyOnce. This mode forwards the request to downstream service for the first call and then mountebank will capture its response data. If there is the same request, mountebank will replay the recorded response without calling the downstream service.

Pros

  • When we want to re-execute the test case with the mock in ProxyOnce mode, we can ensure that the response as a static response will not change from the first request.
  • Because the other same requests ( except the first request) use the stored response, this makes the data reuseable and the service under test can get the response faster than calling real service every time.

Cons

  • If the downstream service down or change the response, we will not know in real-time. This maybe affects on delivery of our service.

Another recorded mode is proxyAlways. The mode is always forward the request to downstream service, the mountebank will save the multiple responses. However, this mode does not automatically replay the response like ProxyOnce. When you want to replay the response, you have to run the command mb replay to change from record mode to replay mode. The mountebank will remove the proxy. Then you can replay the response with the matched request in the stub.

Figure 2. the log after running the replayed command

Pros

  • The proxyAlways mode benefits some test scenarios that expected multiple responses. Moreover, it also benefits when you want to check the connection with downstream service in real-time.

Cons

  • This mode can decrease the stability of your test cases, because of many factors during communication with real service that makes your test cases fail such as, an unstable network connection or downstream service’ s health.

In short, the ProxyOnce mode is suitable for the test scenario that expected the same response in every request, and the proxyAlways mode should be used when you want the multiple responses and want to check the data of real service in real-time.

I think that the proxy feature is very convenient for us. We can use the recorded stub to automatically generate the configuration file of mock service to test microservices instead of the manual creating following the below steps:

Step 1: setting up proxy configuration

You can send the below POST request to set up the imposter configuration.

the imposter configuration
  • Parameter to use for adding the downstream service endpoint.
  • For the solution to create a predicate, it can be set at predicateGenerators field with the pattern at matches object. Mountebank will create the predicate base upon the incoming request.

Step 2: Recording the stub

You can send the GET request to http://localhost:2525/imposters/9901. Then you can see the saved stub:

the saved stub

Step 3: Saving the imposters in file

You can save the recorded stub in the file by using the mb save command and can use --removeProxies command for removing the proxy configuration in the saved stub. However, you can ignore the remove proxy command, if you do not want to remove the proxy.

mb save --savefile <filename>.json --removeProxies 

Moreover, for who would like to replay the saved imposter configuration, can use the mb restart command.

mb restart --configfile <filename>.json
Figure 3. Restarting configuration file

It can be seen that creating the imposter configuration file with the saved stub is faster than creating manually. Actually, there is some more detail about proxy, if you are interested in it can read at www.mbtest.org.

Reference 🧚🏻‍♀️

--

--