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
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
Contents of GetCustomerFound.js is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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 which is the customer ID from the request
Finally , the template response
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
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.