Wiremock Stand alone - how to manipulate a response with request data

I was trying to implement a POST REST call mock using Wiremock standalone server. I faced such a problem, suppose the body of the message contains a field "name" and its value, the same value should be returned in response to this POST call. My json file looks like this:

{
"priority": 1,
"request": {
    "method": "POST",
    "urlPath": "/primeSlots",
    "bodyPatterns" : [ {
        "matchesJsonPath" : "{ \"things\": [ { \"name\": \"794363\" }] 
 }"
    } ]
 },
 "response": {
    "status": 200,
    "body": "{{$.things.name.value}}",
    "transformers": ["response-template"]
 }
}

      

so I need to get the value, that is 794363, but using the above approach won't be able to get it in the post body after the response.

I tried it too:

{
 "request": {
    "method": "POST",
    "urlPath": "/transform",
    "bodyPatterns": [
        {
            "matchesJsonPath" : "$.things[?(@.name =~ /[0-9]+/i)]"
        }
    ]
  },
  "response": {
    "status": 200,
    "body": "{\"responseName\": \"
   {{request.body.things.name.value}}\"}",
    "headers": {
        "Content-Type": "application/json"
    },
    "transformers": ["body-transformer"]
  }
 }

      

So my question is, even if I use regEx which is the same as any number that comes in the request, how do I return the same number in the response using Wiremock's standalone json file? Thank.

+3


source to share


2 answers


Unfortunately, the WireMock decision templating transformer does not currently split the request body into a map as it would be necessary for what you are trying to do. The request body is just one line.

The easiest way to enable this would probably be to create a Handlebars helper that implements JSONPath or some other mechanism to request a JSON document and then registers it with a templating transformer when WireMock is initialized.



At some point I'll write handlebars helpers to do this sort of thing for XML and JSON, but that won't be for a while.

+1


source


Today I was in the same situation as you and found a solution that I would like to share with you:

  • Create your own class that extends ResponseDefinitionTransformer

  • Add Handlebar functionality to your own transformer class (see https://github.com/jknack/handlebars.java for how to do this)
  • (not necessary). Add your / other helpers, for example. Arrays.stream(StringHelpers.values()).forEach(helper -> this.handlebars.registerHelper(helper.name(), helper));

  • Export your transformer settings to a separate JAR file
  • Create an initial batch script to run a standalone WireMock module with its own transformer extension eg. java -cp "-cp ".\lib\wiremock-standalone-2.5.1.jar;.\lib\customTransformer.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --extensions "your.name.CustomTransformer"

    (to use linux :

    instead ;

    of as classpath separator)


If you have multiple transformers, just define all using ,

(comma) as delimiter in the argument --extensions

.

0


source







All Articles