Skip to content

Commit

Permalink
Respect proxyUrlPrefixToRemove when using ResponseTemplateTransformer (
Browse files Browse the repository at this point in the history
  • Loading branch information
andrena-eike-kohnert committed Jan 25, 2022
1 parent 89907f0 commit 6fcffd2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public ResponseDefinition transform(
String newProxyBaseUrl = uncheckedApplyTemplate(proxyBaseUrlTemplate, model);

ResponseDefinitionBuilder.ProxyResponseDefinitionBuilder newProxyResponseDefBuilder =
newResponseDefBuilder.proxiedFrom(newProxyBaseUrl);
newResponseDefBuilder.proxiedFrom(newProxyBaseUrl)
.withProxyUrlPrefixToRemove(responseDefinition.getProxyUrlPrefixToRemove());

if (responseDefinition.getAdditionalProxyRequestHeaders() != null) {
Iterable<HttpHeader> newResponseHeaders =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.github.tomakehurst.wiremock.common.ProxySettings;
import com.github.tomakehurst.wiremock.core.Options;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.http.HttpClientFactory;
import com.github.tomakehurst.wiremock.testsupport.WireMockResponse;
import com.github.tomakehurst.wiremock.testsupport.WireMockTestClient;
Expand Down Expand Up @@ -83,7 +84,7 @@ void init(WireMockConfiguration proxyingServiceOptions) {

targetServiceBaseUrl = "http://localhost:" + targetService.port();

proxyingServiceOptions.dynamicPort().bindAddress("127.0.0.1");
proxyingServiceOptions.dynamicPort().bindAddress("127.0.0.1").extensions(new ResponseTemplateTransformer(true));
proxyingService = new WireMockServer(proxyingServiceOptions);
proxyingService.start();
proxy = WireMock.create().port(proxyingService.port()).build();
Expand Down Expand Up @@ -125,6 +126,29 @@ public void successfullyGetsResponseFromOtherServiceViaProxy() {
assertThat(response.firstHeader("Content-Type"), is("text/plain"));
}

@Test
public void successfullyGetsResponseFromOtherServiceViaProxyRemovingPrefix() {
initWithDefaultConfig();

target.register(
get(urlEqualTo("/resource?param=value"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/plain")
.withBody("Proxied content")));

proxy.register(
any(urlEqualTo("/proxied/resource?param=value"))
.atPriority(10)
.willReturn(aResponse().proxiedFrom(targetServiceBaseUrl).withProxyUrlPrefixToRemove("/proxied")));

WireMockResponse response = testClient.get("/proxied/resource?param=value");

assertThat(response.content(), is("Proxied content"));
assertThat(response.firstHeader("Content-Type"), is("text/plain"));
}

@Test
public void
successfullyGetsResponseFromOtherServiceViaProxyWhenInjectingAddtionalRequestHeaders() {
Expand Down

0 comments on commit 6fcffd2

Please sign in to comment.