Mountebank - Creating a Response Based on a File Template and Modifying It Based on Request - PART 2
This is an extension to my previous blog about how we can use mountebank to create a stubbed response based on a template file . You can read about it here. In this step by step example, I will explain how we will use mountebank to modify the response based on the request . Before we start, please ensure you are familiar with Part1 of the excercise. If you need to know more about mountebank and how to use mountebank , please read through how to install mountebank and service virtualisation using mountebank.
As in previous example, let us create Imposter.ejs and 4547.json . Contents of the Imposter.ejs is as below
1 2 3 4 5 6 7 8 |
|
Contents of 4547.json is as below
4547.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Now create CustomerFound.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
As we can see from above, if there is request which matches the predicates , then response will be dictated by the GetCustomerFound javascript file kept inside directory ResponseInjection. Predicate used here is a GET request which have a matching path of /Blog.Api/[0-9]+/CustomerView.
Contents of GetCustomerFound.js is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
The javascript file have a single function , which reads the stubbed response kept in template file . Then it calls another Javascript function to called “extractrequest”. We will see the details of it soon. For now, it actually returns the customer number from the request . For eg, if request is “http://localhost:4547/Blog.Api/3123/CustomerView ” then it return 3123 as customer ID. Once we extract the customer ID, then it will replace the customer ID in our template response with the value coming from request and return the response.
Let us take a close look at the extractrequest function.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
This method will take the input parameter as the request and split it at “/” to get a an array . Then we will return the array[2] which is the customer ID from the request
Finally , the template response
CustomerFoundView.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Now let us fire up mountebank
Make few request using postman, which have different request parameter
Another request
In above two examples,we can see the CustomerID field is response is updated with number extracted from request.
Now let us try another example , where request is http://localhost:4547/Blog.Api/1234542323/CustomerView
As you can see, we are getting a customer Not found response. This is due to the order of predicates we use. In our 4547.json, the order of response are as below.
Customer Not found which has a predicate of “/Blog.Api/1[0-9]+/CustomerView”
Customer found which has a predicate of “/Blog.Api/[0-9]+/CustomerView”
As you can see from above order, when a request comes through , mountebank will first match with predicate of first response and if it matches, it returns the response. If not, mountebank will keep trying with next one followed by all others. In this particular example, since our request have a customer ID of 1234542323, it matches with regular expression of first one ( 1[0-9]+) and hence it return customer not found response.
In next blog post, I will provide more insights about how to extract request from different type of requests.