Stubbing XML Responses Using Mountebank
Previous two blog post talked about how we can use mountebank for stubbing where responses are in json format . They can be accessed (here) and (here). We can use same approach for stubbing SOAP services using XML as well. In this post, I will explain how we can provide XML response using Mountebank .
Let us have a quick look into the files created. Before we begin, folder structure of various file as below
Imposter.ejs
The main Imposter file is
1 2 3 4 5 |
|
Port4547.json
This file specifies which port number to use and what all stubs needs to be created is as below
1 2 3 4 5 6 7 8 9 10 11 12 |
|
XMLStubGET.json
This is the first stub for this example and it looks for any request coming with the method “GET” and path “/Blog.Api/[0-9]+/CustomerView” , where [0-9]+ is regular expression of any numeric
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
XMLStubPOST.json
This is the second stub for this example and it looks for any request coming with method “POST” and path “/Blog.Api/XMLexamplePOST/[0-9]+” , where [0-9]+ is regular expression of any numeric .It also needs a body as
Note: If you have body in multi-line, then make sure to enter “\n” for new line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
GetXMLStub.js
Below js file create a response based on template mentioned and return the response with proper status. Please note that, we are not using “Json.Parse” here as we did for previous examples involving json.
1 2 3 4 5 6 7 8 9 10 |
|
GetXMLStub-POST.js
1 2 3 4 5 6 7 8 9 10 |
|
CustomerDetails.XML
This is the template for the first stub - GET example
1 2 3 4 5 6 |
|
RecordAdded.xml
This is the template for the second stub - POST example
1 2 |
|
After creating above files and keeping them as per directory structure is shown above, it is time to start mountebank
mb –configfile SOAP-XMLStubExample/Imposter.ejs –allowInjection
Note: Give the right path to Imposter.ejs . If you need to debug Mountebank, you can use below command at the end “ –loglevel debug”
Now trigger a get request to http://localhost:4547/Blog.Api/3123/CustomerView.
This should match with our first predicate and should return the response mentioned
Mountebank_XML_Response_
Now trigger a POST request with a body . If predicates are matched, then it will respond with expected response as below
In Nut shell, creating a XML response is similar to creating json response. There are only minor differences in the js file which creates the response. The main difference is the omission of Json.Parse and also changing the response headers.
Above examples can be cloned from my GitHub repository here. After cloning the repository to local, just run RunMounteBankStubsWithSOAPXMLStubExampleData.bat file. Postman scripts can also be found inside PostmanCollections Folder to testing this