Wiremock: Proxy stub mappings - Need proxyUrlPrefixToRemove for proxy context url mapping

Created on 2 Sep 2017  路  9Comments  路  Source: tomakehurst/wiremock

Hi,

Problem Description:
In Proxy stub mappings, I am trying to map a prefixed request url to a proxy url, but I need that prefix I added to the request call be removed when it is getting dispatched to the proxy host, if it is matched for example to a transformation parameter called proxyUrlPrefixToRemove

Acceptance Criteria:
GIVEN The following code will proxy all GET requests made to http://:/other/service/.* to http://otherservice.com/approot
And proxyUrlPrefixToRemove transformation parameter is set to "/other/service"
WHEN running WireMock locally a request to http://localhost:8080/other/service/doc/123
THEN it would be forwarded to http://otherservice.com/approot/doc/123

My proposed solution:
Since wiremock code is written in a way that there is no place to override classes to add a feature, I had to customize the wiremock code to do it.

Here is the change I made,

In com.github.tomakehurst.wiremock.http.ProxyResponseRenderer class:

Current Code:

   public static HttpUriRequest getHttpRequestFor(ResponseDefinition response) {
        final RequestMethod method = response.getOriginalRequest().getMethod();
        final String url = response.getProxyUrl();
        return HttpClientFactory.getHttpRequestFor(method, url);
    }

Changed Code:

protected synchronized HttpUriRequest getHttpRequestFor(ResponseDefinition response) {
        String proxyUrlPrefixToRemove = null;
        if(response.getTransformerParameters() != null) {
            proxyUrlPrefixToRemove = (String) response.getTransformerParameters().get("proxyUrlPrefixToRemove");
        }

        RequestMethod method = response.getOriginalRequest().getMethod();
        String requestContextUrl = response.getOriginalRequest().getUrl();
        String proxyBaseUrl = response.getProxyBaseUrl();
        StringBuilder sb = new StringBuilder();
        sb.append(proxyBaseUrl);
        if (StringUtils.isNoneBlank(proxyUrlPrefixToRemove) && requestContextUrl.startsWith(proxyUrlPrefixToRemove)) {
            requestContextUrl = requestContextUrl.substring(proxyUrlPrefixToRemove.length());
        }
        sb.append(requestContextUrl);
        return HttpClientFactory.getHttpRequestFor(method, sb.toString());
    }

As you see, I had to make getHttpRequestFor a non-static method so that I can have access to transformation parameter.

I am so interested to contribute wiremock and add this feature to the wiremock. For sure I will follow the wiremock contribution rules and write appropriate unit tests for this feature and run all existing test to avoid any side effect and breaking other things.

Please give me a direction so I can start contributing wiremock by adding this feature to it.

Thanks you so much in advance,

Nasim

Feature request

Most helpful comment

Would be nice to see this feature implemented in the default wiremock.

All 9 comments

This could be done rather easily through a ResponseDefinitionTransformer extension. Here's a similar extension I wrote awhile ago that proxies requests to a host that's determined by a header in the request: https://github.com/MasonM/wiremock-dynamic-proxy/blob/master/src/main/java/com/github/masonm/wiremock/DynamicProxyTransformer.java

For example, if you sent a request with the header X-Proxy-Dest: http://example.com and included the transformer with the parameter "headerName": "X-Proxy-Dest", then it would proxy the request to http://example.com.

We cannot change the existing logic and add a header in the initial request, suppose we only can change the endpoint URL. So playing with request header is not possible.

This is the Acceptance Criteria:
Acceptance Criteria:
GIVEN The following code will proxy all GET requests made to http://:/other/service/.* to http://otherservice.com/approot
And proxyUrlPrefixToRemove transformation parameter is set to "/other/service"
WHEN running WireMock locally a request to http://localhost:8080/other/service/doc/123
THEN it would be forwarded to http://otherservice.com/approot/doc/123

@kabiliravi When I talked about adding a header, that's just for my extension. I was explaining what my extension does so you can understand how it works, since I think it's a good example of how to write a very basic ResponseDefinitionTransformer extension (though I'm a bit biased). Changing headers (or existing logic) isn't a general requirement for extensions. Sorry for the confusion.

I don't see anything that'd prevent you from writing a ResponseDefinitionTransformer that fulfills the acceptance criteria. Is there something I'm missing?

I see, I will try to implement this with ResponseDefinitionTransformer, I hope I can change the target endpoint url and take out the part that I distinguish the Wiremock Stub rules that I defined for different targets. If it works, I will share my works here and close this feature request. Thanks a lot

I found tricky workaround which works.

Would be nice to see this feature implemented in the default wiremock.

I am using the admin API to do this in my functional tests. Is there a way this enhancement can be delivered for WM Admin API?

It is already possible to find this feature in last wiremock?

@Srso would you show me how with a brief sample code? Have you done that using ResponseDefinitionTransformer?

Was this page helpful?
0 / 5 - 0 ratings