JSON Body Transformer#
Request value referencing#
The response body stub acts as a template where the match patterns are defined and which will be replaced by matching JsonPaths in the request body. The syntax of match patterns is slightly different as original JsonPath patterns used by WireMocks request matching as the dot '.' of a pattern is omitted but the path is encapsulated with braces.
Imagine following JSON request body
To get theage
property with JsonPath one would define $.age
JsonPath to get the value 35
. But to reference the value of the age
property in a JSON response body one has to define $(age)
instead.
The JSON of a response body referencing the values of age
and name
might look like
Data Type Handling#
As the request pattern is always a string value it has to be quoted even for numbers, booleans, a.s.o.. The JsonBodyTransformer
will take care of the resulting data type and adds quotes if necessary and will omit them if required, but worth to mention that values used in composed strings will always be the raw values.
If a pattern defined in a JSON response has no matching counterpart in the JSON request the result will yield to null
.
For the example above that will mean a response like
will be returned as rather than which would be incorrect aslastname
wasn't specified as "null"
but was not found in the request.
Stubbing#
Instantiating the WireMock server with JsonBodyTransformer
extension instance
The examples assume that you are familiar with WireMocks stubbing technique.
Specifying the transformer in code with response body
stubFor(post(urlEqualTo("url/to/post/to")).willReturn(aResponse()
.withStatus(201)
.withHeader("content-type", "application/json")
.withBody("{\"name\":\"$(name)\"}")
.withTransformers("json-body-transformer");
{
"request": {
"method": "POST",
"url": "url/to/post/to"
},
"response": {
"status": 201,
"headers": {
"content-type": "application/json"
},
"jsonBody": {
"name": "$(name)"
},
"transformers" : ["json-body-transformer"]
}
}
Specifying the transformer in code with file response
Similar in JSON