Wiremock Stand alone - How to manipulate response

2019-06-20 01:28发布

I was trying to implement mocking of POST REST call using Wiremock Standalone server. I am faced with a challenge like this, suppose the post body contains an "name" field and its value, the same value should be returned in the response of that 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 above approach not able to get it in the post response body.

I tried this 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 a regEx, which matches with any number that comes in the request, how to give back the same number in the response using Wiremock standalone json file? Thanks.

2条回答
姐就是有狂的资本
2楼-- · 2019-06-20 01:46

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

  1. Create own class that extends ResponseDefinitionTransformer
  2. Add Handlebar functionality in your own transformer class ( see https://github.com/jknack/handlebars.java how to do it )
  3. (Optional) Append your own/other helpers e.g. Arrays.stream(StringHelpers.values()).forEach(helper -> this.handlebars.registerHelper(helper.name(), helper));
  4. Export your transformer customization as a separate JAR file
  5. Create a start batch script to start up your WireMock standalone with your own transformer extension e.g. java -cp "-cp ".\lib\wiremock-standalone-2.5.1.jar;.\lib\customTransformer.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --extensions "your.name.CustomTransformer" (for linux use : instead of ; as classpath separator)

In case you have multiple transformers, just define all those using , (comma) as separator in the --extensions arg.

查看更多
We Are One
3楼-- · 2019-06-20 01:49

Unfortunately WireMock's response templating transformer doesn't currently break out the request body into a Map as would be necessary for what you're trying to do. The request body is just a single string.

The simplest way for you to enable this would probably be to write a Handlebars helper that implements JSONPath or some other mechanism for querying a JSON document, then registering this with the templating transformer when you initialise WireMock.

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

查看更多
登录 后发表回答