Extending the model used by Handlebar Template

1,406 views
Skip to first unread message

hervé DARRITCHON

unread,
Apr 19, 2017, 5:57:24 AM4/19/17
to wiremock-user
Hi !

I have a question.

It's possible to use in the response some data from the request as it is written in the documentation and it works :)

Exemple :

{
  "request": {
    "method": "GET",
    "urlPath": "/some/thing"
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "uuid": "c80ec299-e09d-43ee-afcf-10f4eeee349a",
      "lastModificationDate": "2016-11-21T10:02:27.390Z",
      "title": "{{request.url}}",
    },
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

So far, it was enough :) But I'd like to extend the use of Handlebar template with some data that are not available in the request.
In the next example, I'd like to value the {{today}} with the current date.

Exemple :

{
  "request": {
    "method": "GET",
    "urlPath": "/some/thing"
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "uuid": "c80ec299-e09d-43ee-afcf-10f4eeee349a",
      "lastModificationDate": "{{today}}",
      "title": "{{request.url}}",
    },
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

What is the best way to do so ?

Thanks for the stuff done on this great mock and stub framework.

Hervé

Tom Akehurst

unread,
Apr 20, 2017, 8:14:36 AM4/20/17
to wiremock-user
At the moment the easiest way to do this is to add your own Handlebars helper (documented near the bottom of the response templating page).

Unfortunately if you're running standalone this means you'll need to re-bake your own standalone JAR with your code in it.

hervé DARRITCHON

unread,
Apr 20, 2017, 10:16:42 AM4/20/17
to wiremock-user
ok, thanks Tom.

But the documentation is a bit short :)

Where do I have to put the code of the Helper

Helper<String> stringLengthHelper = new Helper<String>() {
@Override public Object apply(String context, Options options) throws IOException { return context.length(); } };

I tried to put it in my class that starts the mock server 
 WireMockServer wireMockServer = new WireMockServer(wireMockConfig()
               
.extensions(new MyShopDateTransformer(false),"string-length", stringLengthHelper)
               
.extensions(new MyShopStatusTransformer())
               
.port(8089)
               
.usingFilesUnderDirectory("src/test/resources"));

But it's not working, no constructor found for extensions with this signature.

I'm a bit disoriented, If you can help me ;)

Thanks.

Tom Akehurst

unread,
Apr 21, 2017, 6:06:32 AM4/21/17
to wiremock-user

alperen üretmen

unread,
Oct 25, 2017, 9:34:23 AM10/25/17
to wiremock-user
I think custom helpers part is unclear beacuse there is almost no information in the website  (in " http://wiremock.org/docs/response-templating#customer-helpers")
I read the instructions but I still dont have any idea  where to declare it, where to use it and what is the context in helper(request or response ?? )

21 Nisan 2017 Cuma 13:06:32 UTC+3 tarihinde Tom Akehurst yazdı:

Tom Akehurst

unread,
Oct 25, 2017, 12:59:25 PM10/25/17
to wiremock-user
The context is whatever you pass into the helper's arguments when you use it in a template. Suggest checking out the Handlebars docs because this is totally standard Handlebars behaviour, nothing unique to MockLab here.

You declare it in WireMock's configuration builder, passed to the WireMockRule constructor or WireMockServer constructor. This is shown in the doc I linked.

alperen üretmen

unread,
Oct 31, 2017, 2:31:40 AM10/31/17
to wiremock-user
After reading more documentation and trial-errors, I finally did it, thank you:)

25 Ekim 2017 Çarşamba 19:59:25 UTC+3 tarihinde Tom Akehurst yazdı:
Message has been deleted

Michael McFadyen

unread,
Aug 10, 2018, 10:00:08 AM8/10/18
to wiremock-user
Could you possibly share your code or create an example project for future reference? There seems to be very little documentation or examples on this feature. 

Tom Akehurst

unread,
Aug 10, 2018, 10:03:23 AM8/10/18
to wiremock-user
Are you having trouble actually writing the helper or just with integrating it into WireMock?

If the former, have you looked at the Handlebars docs?

Michael McFadyen

unread,
Aug 10, 2018, 10:17:32 AM8/10/18
to wiremock-user
I'm unsure of how to go about create a custom helper and using it with standalone wiremock.

Tom Akehurst

unread,
Aug 10, 2018, 10:19:55 AM8/10/18
to wiremock-user
Creating custom helpers with standalone is a bit of a pain at the moment unfortunately.

Basically you need to create your own fat JAR with WireMock + your helpers included.

Michael McFadyen

unread,
Aug 10, 2018, 10:59:03 AM8/10/18
to wiremock-user
Yeah, that's why I was looking for an example as I'm not sure where to start. I'm guessing I will have to have a new main method that configures and runs wiremock with the custom helper. 
Can you post a link to the main class that is used for standalone wiremock? 

Tom Akehurst

unread,
Aug 10, 2018, 11:23:46 AM8/10/18
to wiremock-user
It's this one:

You could just clone that, and swap the command line options stuff for WireMockConfiguration.options(), which would allow you to register your helpers.

Michael McFadyen

unread,
Aug 10, 2018, 11:36:02 AM8/10/18
to wiremock-user
great, thanks. I'll give it go and hopefully create an example project.
Reply all
Reply to author
Forward
0 new messages