Don't set request url in BeforeFilterFunctions.rewritePath.

Fixes gh-3183
Fixes gh-3178
Fixes gh-3055
This commit is contained in:
sgibb
2023-12-20 15:55:58 -05:00
parent e360c22f4a
commit ed637f5497
2 changed files with 115 additions and 5 deletions

View File

@@ -324,7 +324,8 @@ public abstract class BeforeFilterFunctions {
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;
};
}

View File

@@ -193,7 +193,16 @@ public class ServerMvcIntegrationTests {
}
@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();
@@ -202,6 +211,17 @@ public class ServerMvcIntegrationTests {
});
}
@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)
@@ -328,6 +348,28 @@ public class ServerMvcIntegrationTests {
});
}
@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()
@@ -398,7 +440,7 @@ public class ServerMvcIntegrationTests {
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()
@@ -413,7 +455,7 @@ public class ServerMvcIntegrationTests {
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());
@@ -498,6 +540,15 @@ public class ServerMvcIntegrationTests {
.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()
@@ -704,6 +755,17 @@ public class ServerMvcIntegrationTests {
// @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
@@ -715,6 +777,18 @@ public class ServerMvcIntegrationTests {
// @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
@@ -855,6 +929,30 @@ public class ServerMvcIntegrationTests {
// @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
@@ -869,7 +967,7 @@ public class ServerMvcIntegrationTests {
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"))
@@ -970,6 +1068,17 @@ public class ServerMvcIntegrationTests {
// @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