I want to have a wiremock rule that will match
/my-login-service/callback.*(\?.*)
and will redirect to
https://myservice.local/myservice/callback
while keeping all the query parameters intact.
So a wiremock request to
/my-login-service/callback?param1=val1¶m2=val2
becomes redirected to
https://myservice.local/myservice/callback?param1=val1¶m2=val2
I cannot use proxiedFrom
because I need the webpage to land in a specific spot.
How can I use Wiremock server to redirect with all the existing headers and request parameters from the original request to the willReturn?
stubMappingProxy = wireMock.register(WireMock.get(WireMock.urlPathMatching("/my-login-service/callback.*"))
.willReturn(WireMock.aResponse()
.withStatus(302)
// how do i redirect to a location based on the request path?
.withHeader("Location", "https://myservice.local/my-login-service/callback"))));
There isn't a nice out of the box way to do this in WireMock at the moment. To achieve this you will need to use response templating to loop through the query parameters in the request and add them to the url you are returning in the header.
The template to achieve this is as follows:
{{#each request.query as |v k|}}{{k}}={{v}}{{#lt @index (math (size request.query) '-' 1)}}&{{/lt}}{{/each}}
So in your example, you would append the above to the url in your header
stubMappingProxy = wireMock.register(WireMock.get(WireMock.urlPathMatching("/my-login-service/callback.*"))
.willReturn(WireMock.aResponse()
.withStatus(302)
.withTransformers("response-template")
.withHeader("Location", "https://myservice.local/my-login-service/callback?{{#each request.query as |v k|}}{{k}}={{v}}{{#lt @index (math (size request.query) '-' 1)}}&{{/lt}}{{/each}}"))));
Notice that the withTransformers
has been added to enable response templating.
It is important to note that this does not guarantee the order of the query parameters in the header but they should be the same as the query parameters used in the request