How to respond with the same header in Wiremock?

Viewed 424

I am using Wiremock Java API to create a stub for a REST HTTP service.
I want to make this stub "mirror" the header in a response, i.e. put the same header and value that it got in a request.

The header is generated in runtime so, unfortunately, there is no way to put a static value with the method

.willReturn(WireMock.aResponse()
                    .withHeader(<key>, <value>)

Is there a way to achieve this with Wiremock?

1 Answers

The simplest way to do this is to enable response templating when you start WireMock then do something like this when configuring your stub:

.willReturn(aResponse()
            .withHeader("My-Header-Key", "{{request.headers.My-Header-Key}}"));
Related