question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proxy stub mappings - Need proxyUrlPrefixToRemove for proxy context url mapping

See original GitHub issue

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://<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:open
  • Created 6 years ago
  • Reactions:10
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
liufuyangcommented, Dec 21, 2018

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

2reactions
pavetokcommented, Apr 27, 2018

I found tricky workaround which works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Need proxyUrlPrefixToRemove for proxy context url mapping ...
Hi, Problem Description: In Proxy stub mappings, I am trying to map a prefixed request url to a proxy url, but I need...
Read more >
Proxying and proxy stub mappings - WireMock
Proxy responses are defined in exactly the same manner as stubs, meaning that the same request matching criteria can be used.
Read more >
WireMock 2.32.0 released - Google Groups
#745 Need proxyUrlPrefixToRemove for proxy context url mapping (#1556) ... Allow standalone runner to fetch mappings from classpath (#1592).
Read more >
wiremock - Bountysource
In Proxy stub mappings, I am trying to map a prefixed request url to a proxy url, but I need that prefix I...
Read more >
WireMock - Proxying with JSON Mappings - GeeksforGeeks
WireMock has the power to selectively proxy requests through to other ... create a mapping in WireMock and will route all the request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found