Wiremock: Rendering responses with variables

Created on 24 Mar 2014  路  7Comments  路  Source: tomakehurst/wiremock

Hi,
first of all Wiremock is a great tool!

My question, have you considered using some kind of DSL/template engine for rendering response bodies?
E.g. sending in custom variables as headers etc and using these to render the response.

I need a tool like Wiremock for performance testing and to test complex scenarios in a predictable manner.

It seems to me I would need to prepare each scenario in advance by specifying the exact response. (Create a stub for each request)
This is of course reasonable, but requires me to build something somewhere, when this could potentially be achieved within Wiremock.

Most helpful comment

I understand the need to keep WireMock thin. Also, the second you add some kind of templating engine, someone would not like it and would want some other engine.

A possible compromise solution is to create an interface for an injectable response post processor via which users can inject whatever templating they want. The post processor could get the request (headers, body, URL, verb etc.) as a parameter. Independent projects for various streamlined, test-appropriate templating solutions can then spring up and in interface in a standard way with WireMock.

All 7 comments

Hi,

I've been asked for something like this a few times now. The reason I've avoided adding a feature like this is that it adds considerable complexity (and potentially startup time, which I'm very keen to keep low) to do something I feel can be done much better using other tools.

Since most services don't just inject values from their URLs or headers, for this to be really useful it'd be necessary to support some kind of controller logic, and then you're well on the way to implementing an application framework.

I'd suggest having a look at http://ratpack.io or something similar if you want a quick and easy way to build dynamic HTTP services.

When I've used WireMock for load testing in the past and I've needed a large number of stubs I've tended to either record or generate them. In one case I set things up so that I could record my browser session against my app in JMeter while simultaneously recording the interactions between the app and a REST service with WireMock. That allowed me to build and discard tests very easily, even when there was a lot of data involved.

Hope this helps!
Tom

While not quite the same thing, at the suggestion of @bnickel , I am considering using Mustache.java ( https://github.com/spullara/mustache.java ) to populate template before loading into WireMock.

Maybe the WireMock client API could build in support for this. Something like:

stubFor(post(urlMatching("/foo")))
        .willReturn(aResponse().withMustacheTemplate("response.mustache", dataForTemplate));

If you've got the data at the time you're configuring the stub, can't you just pre-render it then set it as the body using the existing API?

Yes, I plan on doing that.

But building something like this into the wiremock client API, might fit the need of all the people requesting dynamic stuff.

Also, FWIW, I support NOT making wiremock dynamic.

Most of the folks who've asked for this have asked for the ability to merge the template with attributes from the request, which would preclude being able to render the response at stub configuration time.

I understand the need to keep WireMock thin. Also, the second you add some kind of templating engine, someone would not like it and would want some other engine.

A possible compromise solution is to create an interface for an injectable response post processor via which users can inject whatever templating they want. The post processor could get the request (headers, body, URL, verb etc.) as a parameter. Independent projects for various streamlined, test-appropriate templating solutions can then spring up and in interface in a standard way with WireMock.

That's a very reasonable idea. I'll try and take some time to think about this over the holidays.

Was this page helpful?
0 / 5 - 0 ratings