Proxy stub mappings - Need proxyUrlPrefixToRemove for proxy context url mapping
See original GitHub issueHi,
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://<host>:<port>/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
Issue Analytics
- State:
- Created 6 years ago
- Reactions:10
- Comments:13 (2 by maintainers)
Would be nice to see this feature implemented in the default wiremock.
I found tricky workaround which works.