2

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?

fyrkov
  • 2,245
  • 16
  • 41
  • 1
    The buzzword is `Request templating`. You can add some Java/.. classes which are working like a converter. Those are applying additional rules/Templates for reuqests. This will make wiremock be able to mirror special header/... on runetime and not require a static simple mapping. Check docs: http://wiremock.org/docs/response-templating/ – LenglBoy Jul 19 '21 at 07:51

1 Answers1

3

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}}"));
Tom
  • 3,471
  • 21
  • 14