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

folderstructure

Imposter.ejs

The main Imposter file is

Imposter.ejs
1
2
3
4
5
{
"imposters": [
<% include Port4547.json %>
]
}

Port4547.json

This file specifies which port number to use and what all stubs needs to be created is as below

Port4547.json
1
2
3
4
5
6
7
8
9
10
11
12
{
"port": 4547,
"protocol": "http",
"stubs": [
{
<% include XMLStubGET.json %>
},
{
<% include XMLStubPOST.json %>
}
]
}

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

XMLStubGET.json
1
2
3
4
5
6
7
8
9
10
11
12
13
"responses": [
{
"inject": "<%-stringify(filename, 'ResponseInjection\\GetXMLStub.js') %>"
}
],
"predicates": [
{
"matches": {
"method" : "GET",
"path" : "/Blog.Api/[0-9]+/CustomerView"
}
}
]

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 InsertCustomer1

Note: If you have body in multi-line, then make sure to enter “\n” for new line

XMLStubPOST.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"responses": [
{
"inject": "<%-stringify(filename, 'ResponseInjection\\GetXMLStub-POST.js') %>"
}
],
"predicates": [
{
"matches": {
"body" : "<Action>Insert</Action><Record>Customer1</Record>",
"method" : "POST",
"path" : "/Blog.Api/XMLexamplePOST/[0-9]+"
}
}
]

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.

GetXMLStub.js
1
2
3
4
5
6
7
8
9
10
function GetTemplateResponse (request, state, logger) {
response = "<%- stringify(filename, 'StubTemplate\\CustomerDetails.xml') %>"
return {
statusCode : 200,
headers: {
'Content-Type': 'application/xml; charset=utf-8'
},
body: response
};
}

GetXMLStub-POST.js

GetXMLStub-POST.js
1
2
3
4
5
6
7
8
9
10
function GetTemplateResponse (request, state, logger) {
response = "<%- stringify(filename, 'StubTemplate\\RecordAdded.xml') %>"
return {
statusCode : 200,
headers: {
'Content-Type': 'application/xml; charset=utf-8'
},
body: response
};
}

CustomerDetails.XML

This is the template for the first stub - GET example

XML
1
2
3
4
5
6
<customer>
  <FirstName>John</FirstName>
  <LastName>Citizen</LastName>
  <Address>Some St, Some State, Some Country</Address>
  <Email>Test@test.com</Email>
</customer>

RecordAdded.xml

This is the template for the second stub - POST example

XML
1
2
<Status>Added</Status>
<Record>Customer1</Record>

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

PostManRequest

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

Comments