Skip to content

Commit

Permalink
Don't set request url in BeforeFilterFunctions.rewritePath.
Browse files Browse the repository at this point in the history
Fixes gh-3183
Fixes gh-3178
Fixes gh-3055
  • Loading branch information
spencergibb committed Dec 20, 2023
1 parent e360c22 commit ed637f5
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ public static Function<ServerRequest, ServerRequest> rewritePath(String regexp,

ServerRequest modified = ServerRequest.from(request).uri(rewrittenUri).build();

MvcUtils.setRequestUrl(modified, modified.uri());
// TODO: can this be restored at some point?
// MvcUtils.setRequestUrl(modified, modified.uri());
return modified;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,16 @@ public void setPathWorks() {
}

@Test
public void stripPathWorks() {
public void setPathPostWorks() {
restClient.post().uri("/mycustompathpost").bodyValue("hello").header("Host", "www.setpathpost.org").exchange()
.expectStatus().isOk().expectBody(Map.class).consumeWith(res -> {
Map<String, Object> map = res.getResponseBody();
assertThat(map).containsEntry("data", "hello");
});
}

@Test
public void stripPrefixWorks() {
restClient.get().uri("/long/path/to/get").exchange().expectStatus().isOk().expectBody(Map.class)
.consumeWith(res -> {
Map<String, Object> map = res.getResponseBody();
Expand All @@ -202,6 +211,17 @@ public void stripPathWorks() {
});
}

@Test
public void stripPrefixPostWorks() {
restClient.post().uri("/long/path/to/post").bodyValue("hello").header("Host", "www.stripprefixpost.org")
.exchange().expectStatus().isOk().expectBody(Map.class).consumeWith(res -> {
Map<String, Object> map = res.getResponseBody();
assertThat(map).containsEntry("data", "hello");
Map<String, Object> headers = getMap(map, "headers");
assertThat(headers).containsEntry("X-Test", "stripPrefixPost");
});
}

@Test
public void setStatusGatewayRouterFunctionWorks() {
restClient.get().uri("/status/201").exchange().expectStatus().isEqualTo(HttpStatus.TOO_MANY_REQUESTS)
Expand Down Expand Up @@ -328,6 +348,28 @@ public void rewritePathWorks() {
});
}

@Test
public void rewritePathPostWorks() {
restClient.post().uri("/baz/post").bodyValue("hello").header("Host", "www.rewritepathpost.org").exchange()
.expectStatus().isOk().expectBody(Map.class).consumeWith(res -> {
Map<String, Object> map = res.getResponseBody();
assertThat(map).containsEntry("data", "hello");
Map<String, Object> headers = getMap(map, "headers");
assertThat(headers).containsEntry("X-Test", "rewritepathpost");
});
}

@Test
public void rewritePathPostLocalWorks() {
restClient.post().uri("/baz/post").bodyValue("hello").header("Host", "www.rewritepathpostlocal.org").exchange()
.expectStatus().isOk().expectBody(Map.class).consumeWith(res -> {
Map<String, Object> map = res.getResponseBody();
assertThat(map).containsEntry("data", "hello");
Map<String, Object> headers = getMap(map, "headers");
assertThat(headers).containsEntry("x-test", "rewritepathpostlocal");
});
}

@Test
public void forwardedHeadersWork() {
restClient.get().uri("/headers").header("test", "forwarded").exchange().expectStatus().isOk()
Expand Down Expand Up @@ -398,7 +440,7 @@ void multipartFormDataWorks() {
MultiValueMap<String, HttpEntity<?>> formData = createMultipartData();
// @formatter:off
restClient.post().uri("/post").contentType(MULTIPART_FORM_DATA)
.header("test", "form")
.header("Host", "www.testform.org")
.bodyValue(formData)
.exchange()
.expectStatus().isOk()
Expand All @@ -413,7 +455,7 @@ void multipartFormDataWorks() {
void multipartFormDataRestTemplateWorks() {
MultiValueMap<String, HttpEntity<?>> formData = createMultipartData();
RequestEntity<MultiValueMap<String, HttpEntity<?>>> request = RequestEntity.post("/post")
.contentType(MULTIPART_FORM_DATA).header("test", "form").body(formData);
.contentType(MULTIPART_FORM_DATA).header("Host", "www.testform.org").body(formData);
ResponseEntity<Map> response = restTemplate.exchange(request, Map.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertMultipartData(response.getBody());
Expand Down Expand Up @@ -498,6 +540,15 @@ public void removeRequestParameterWorks() {
.expectStatus().isOk().expectHeader().doesNotExist("foo");
}

@Test
public void removeRequestParameterPostWorks() {
restClient.post().uri("/post?foo=bar").bodyValue("hello").header("Host", "www.removerequestparampost.org")
.exchange().expectStatus().isOk().expectHeader().doesNotExist("foo").expectBody(Map.class)
.consumeWith(res -> {
assertThat(res.getResponseBody()).containsEntry("data", "hello");
});
}

@Test
public void removeResponseHeaderWorks() {
restClient.get().uri("/anything/removeresponseheader").header("test", "removeresponseheader").exchange()
Expand Down Expand Up @@ -704,6 +755,17 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsSetPath() {
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsSetPathPost() {
// @formatter:off
return route("testsetpath")
.route(POST("/mycustompath{extra}").and(host("**.setpathpost.org")), http())
.filter(new HttpbinUriResolver())
.filter(setPath("/{extra}"))
.build();
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefix() {
// @formatter:off
Expand All @@ -715,6 +777,18 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefix() {
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefixPost() {
// @formatter:off
return route("teststripprefixpost")
.route(POST("/long/path/to/post").and(host("**.stripprefixpost.org")), http())
.filter(new HttpbinUriResolver())
.filter(stripPrefix(3))
.filter(addRequestHeader("X-Test", "stripPrefixPost"))
.build();
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsRemoveHopByHopRequestHeaders() {
// @formatter:off
Expand Down Expand Up @@ -855,6 +929,30 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsRewritePath() {
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsRewritePathPost() {
// @formatter:off
return route("testrewritepathpost")
.route(POST("/baz/**").and(host("**.rewritepathpost.org")), http())
.filter(new HttpbinUriResolver())
.filter(rewritePath("/baz/(?<segment>.*)", "/${segment}"))
.filter(addRequestHeader("X-Test", "rewritepathpost"))
.build();
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsRewritePathPostLocal() {
// @formatter:off
return route("testrewritepathpostlocal")
.route(POST("/baz/**").and(host("**.rewritepathpostlocal.org")), http())
.before(new LocalServerPortUriResolver())
.filter(rewritePath("/baz/(?<segment>.*)", "/test/${segment}"))
.filter(addRequestHeader("X-Test", "rewritepathpostlocal"))
.build();
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsForwardedHeaders() {
// @formatter:off
Expand All @@ -869,7 +967,7 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsForwardedHeaders() {
public RouterFunction<ServerResponse> gatewayRouterFunctionsForm() {
// @formatter:off
return route("testform")
.POST("/post", header("test", "form"), http())
.POST("/post", host("**.testform.org"), http())
.before(new LocalServerPortUriResolver())
.filter(prefixPath("/test"))
.filter(addRequestHeader("X-Test", "form"))
Expand Down Expand Up @@ -970,6 +1068,17 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsRemoveRequestParam()
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsRemoveRequestParamPost() {
// @formatter:off
return route("removerequestparampost")
.route(host("www.removerequestparampost.org").and(POST("/post")), http())
.filter(new HttpbinUriResolver())
.before(removeRequestParameter("foo"))
.build();
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsRemoveResponseHeader() {
// @formatter:off
Expand Down

0 comments on commit ed637f5

Please sign in to comment.