Wiremock extension to transform response headers and transformer parameters with request parameters
Sometimes we need to use values from request parameters in our wiremock response. The body-transformer extension allows us to use them in our response body.
This extension allows us to use the request parameters in response headers and also transformer parameters.
Download the wiremock jar.
Download the extension jar.
Start wiremock by running:
java -cp "wiremock-parameter-transformer.jar:wiremock-standalone.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --verbose --extensions 'com.github.viniciusffj.wiremock.ParametersTransformer,com.github.viniciusffj.wiremock.HeadersTransformer'
Please, read more about running wiremock as a standalone process in their documentation.
First of all, we need to tell wiremock we are using the extension by doing:
... "transformers": ["headers-transformer"] ...
}
Then we can use request parameters in the header by using the following:
{
...
"response": {
"status": 302,
"headers": {
"Location": "${body-type=<type>,query=<parameter>}"
},
...
}
}
Where <type>
is our body request type and <parameter>
we want to use. Please check supported types for more information about this two values.
Let's say that we'll receive a application/x-www-form-urlencoded
body like:
id=123&url=http://urltoredirect.com
Them we would use:
{
...
"response": {
"status": 302,
"headers": {
"Location": "${body-type=form,query=url}"
},
...
}
}
First of all, we need to tell wiremock we are using the extension by doing:
... "transformers": ["parameters-transformer"] ...
}
Then we can use request parameters in the transformer parameters by using the following:
{
...
"response": {
"status": 200,
"transformerParameters" : {
"id" : "${body-type=<type>,query=<parameter>}"
}
...
}
}
Where <type>
is our body request type and <parameter>
we want to use. Please check supported types for more information about this two values.
Let's say right now we'll receive a json
body like:
{"id": "123"}
Them we would use:
{
...
"response": {
"status": 200,
"transformerParameters" : {
"id" : "${body-type=json,query=$.id}"
}
...
}
}
This extension supports two body types
For this type of request, we use body-type=form
and query
we put the parameter name.
For example, if we have the following body:
param1=value1¶m2=value2
To get the value of param1, we should use:
${body-type=form,query=param1}
For json request, we use body-type=json
and in query
we use the json path.
For the following body:
{
"person": {
"name": "Pablo",
"last_name": "Escobar"
}
}
To get the person's last name, we should use:
${body-type=json,query=$.person.last_name}
To run the tests use:
./gradlew test
To generate a jar, just run:
./gradlew jar
And the jar will be in directory build/libs/
.