diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctions.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctions.java index 78e1b31be2..d5a918646c 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctions.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctions.java @@ -324,7 +324,8 @@ public static Function 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; }; } diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java index ea54872a27..ee28356ebb 100644 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java @@ -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 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 map = res.getResponseBody(); @@ -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 map = res.getResponseBody(); + assertThat(map).containsEntry("data", "hello"); + Map 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 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 map = res.getResponseBody(); + assertThat(map).containsEntry("data", "hello"); + Map 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 map = res.getResponseBody(); + assertThat(map).containsEntry("data", "hello"); + Map 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 @@ void multipartFormDataWorks() { MultiValueMap> 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 @@ void multipartFormDataWorks() { void multipartFormDataRestTemplateWorks() { MultiValueMap> formData = createMultipartData(); RequestEntity>> 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 response = restTemplate.exchange(request, Map.class); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertMultipartData(response.getBody()); @@ -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() @@ -704,6 +755,17 @@ public RouterFunction gatewayRouterFunctionsSetPath() { // @formatter:on } + @Bean + public RouterFunction 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 gatewayRouterFunctionsStripPrefix() { // @formatter:off @@ -715,6 +777,18 @@ public RouterFunction gatewayRouterFunctionsStripPrefix() { // @formatter:on } + @Bean + public RouterFunction 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 gatewayRouterFunctionsRemoveHopByHopRequestHeaders() { // @formatter:off @@ -855,6 +929,30 @@ public RouterFunction gatewayRouterFunctionsRewritePath() { // @formatter:on } + @Bean + public RouterFunction gatewayRouterFunctionsRewritePathPost() { + // @formatter:off + return route("testrewritepathpost") + .route(POST("/baz/**").and(host("**.rewritepathpost.org")), http()) + .filter(new HttpbinUriResolver()) + .filter(rewritePath("/baz/(?.*)", "/${segment}")) + .filter(addRequestHeader("X-Test", "rewritepathpost")) + .build(); + // @formatter:on + } + + @Bean + public RouterFunction gatewayRouterFunctionsRewritePathPostLocal() { + // @formatter:off + return route("testrewritepathpostlocal") + .route(POST("/baz/**").and(host("**.rewritepathpostlocal.org")), http()) + .before(new LocalServerPortUriResolver()) + .filter(rewritePath("/baz/(?.*)", "/test/${segment}")) + .filter(addRequestHeader("X-Test", "rewritepathpostlocal")) + .build(); + // @formatter:on + } + @Bean public RouterFunction gatewayRouterFunctionsForwardedHeaders() { // @formatter:off @@ -869,7 +967,7 @@ public RouterFunction gatewayRouterFunctionsForwardedHeaders() { public RouterFunction 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 RouterFunction gatewayRouterFunctionsRemoveRequestParam() // @formatter:on } + @Bean + public RouterFunction 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 gatewayRouterFunctionsRemoveResponseHeader() { // @formatter:off