tomakehurst/wiremock — pull request #94 by shashank-neo (2014-02-04 20:50:00+00:00)
Title: Adding Support for Dynamic Responses

I have integrated json template library with wiremock to support generating dynamic responses, where responses depend on in coming data in form of POST/PUT body, queryParameters, UrlParameters, Headers.

Json template is a small but powerful java library, It provides a templating language using which user can create a template and expand it by providing different data dictionaries. More here http://json-template.googlecode.com/svn/trunk/doc/Introducing-JSON-Template.html

This feature doesn't breaks or modifies any existing features of wiremock. If a User wants he can ask for this feature while creating the mock. He needs to set one more parameter(isDynamicResponse) and send the template as request body while creating the mock. When actual requests come for the mapping wiremock expands the template with incoming data and returns response.

For eg:
Lets say I want to create a mapping which returns
{"foo":"bar","list":[{"nick":"bob1","age":20},{"nick":"bob2","age":22}]}
based on user data which was sent as request body
i will create mapping as follows:

{
"request": {
"method": "POST",
"url": "/some/resource1"
},
"response": {
"status": 201,
"body": "{.section requestBody}{.meta-left}\"foo\":\"{foo}\",\"list\":[{.repeated section users}{.meta-left}\"name\":\"{nick}\",\"age\":{age}{.meta-right}{.alternates with},{.end}]{.meta-right}{.end}",
"isDynamicResponse":true
}
}

After setting the mapping If I make a post call with following POST body
{"foo":"bar","users":[{"nick":"bob1","age":20},{"nick":"bob2","age":22}]}

It will return
{"foo":"bar","list":[{"name":"bob1","age":20},{"name":"bob2","age":22}]}

Similarly We can use UrlParameters, QueryParameters and Headers to fill template