WireMock CustomTransformer (handlebars helpers not working in bodyFile)

939 Views Asked by At

I have problems with Handlebars helpers in Wiremock.

I registered a custom transformer like that:

final WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(PORT).extensions(new ResponseTemplateTransformer(true), new CustomTransformers.FileNameTransformer()));

WORKING SCENARIO: It is working perfectly when I create stub like that:

stubFor(get("/my/path/with/12345/id")
                .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "text/json")
                        .withTransformers("response-filename-handler")
                        .withBodyFile("myJsonFile.json")
                ));

Content in myJsonFile.json:

{
  "key": "{{request.query.path.[3]}}"
}

In that case, I get the expected response:

{
  "key": "12345"
}

NOT WORKING SCENARIO: The problem is when I try to use that file for response in my CustomTransformer:

public class CustomTransformers {
  public static class FileNameTransformer extends ResponseDefinitionTransformer {
    @Override
    public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource fileSource, Parameters parameters) {
      return ResponseDefinitionBuilder.like(responseDefinition).but().withBodyFile("myJsonFile.json").build();
    }

    @Override
    public String getName() {
        return "response-filename-handler";
    }
  }
}

I want the same thing with Handlebars helpers as in WORKING SCENARIO. But I actually get the same response as file content. Instead of value from the request path I get String "{{request.request.path.[3]}}".

Expected response:

{
  "key": "12345"
}

The response I get:

{
  "key": "{{request.query.path.[3]}}"
}

Any ideas on how can I achieve that?

1

There are 1 best solutions below

0
On

You're correct - handlebars won't be evaluated when you serve up the response JSON file in this way. If you want to use the file in the response transformer, you'll need to replace the key value as part of the transformer, and then serve it up as a response.