diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactory.java index 903222712..ff658ca1f 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactory.java @@ -59,7 +59,7 @@ public class PrefixPathGatewayFilterFactory extends AbstractGatewayFilterFactory ServerHttpRequest req = exchange.getRequest(); addOriginalRequestUrl(exchange, req.getURI()); - String newPath = config.prefix + req.getURI().getPath(); + String newPath = config.prefix + req.getURI().getRawPath(); ServerHttpRequest request = req.mutate() .path(newPath) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java index f8d6a867c..a26fbba5a 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java @@ -49,7 +49,7 @@ public class RewritePathGatewayFilterFactory extends AbstractGatewayFilterFactor return (exchange, chain) -> { ServerHttpRequest req = exchange.getRequest(); addOriginalRequestUrl(exchange, req.getURI()); - String path = req.getURI().getPath(); + String path = req.getURI().getRawPath(); String newPath = path.replaceAll(config.regexp, replacement); ServerHttpRequest request = req.mutate() diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java index e525fb8c5..307faf0f9 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java @@ -65,7 +65,7 @@ public class SetPathGatewayFilterFactory extends AbstractGatewayFilterFactory c.setPrefix(prefix)); + MockServerHttpRequest request = MockServerHttpRequest + .get("http://localhost" + path) + .build(); + + ServerWebExchange exchange = MockServerWebExchange.from(request); + + + GatewayFilterChain filterChain = mock(GatewayFilterChain.class); + + ArgumentCaptor captor = ArgumentCaptor.forClass(ServerWebExchange.class); + when(filterChain.filter(captor.capture())).thenReturn(Mono.empty()); + + filter.filter(exchange, filterChain); + + ServerWebExchange webExchange = captor.getValue(); + + assertThat(webExchange.getRequest().getURI()).hasPath(expectedPath); + LinkedHashSet uris = webExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR); + assertThat(uris).contains(request.getURI()); + } + +} \ No newline at end of file diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactoryTests.java index 34df64bff..c058aa588 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactoryTests.java @@ -51,6 +51,11 @@ public class RewritePathGatewayFilterFactoryTests { testRewriteFilter("/foo", "/baz", "/foo/bar", "/baz/bar"); } + @Test + public void rewriteEncodedPathFilterWorks() { + testRewriteFilter("/foo", "/baz", "/foo/bar%20foobar", "/baz/bar foobar"); + } + @Test public void rewritePathFilterWithNamedGroupWorks() { testRewriteFilter("/foo/(?\\d.*)", "/bar/baz/$\\{id}", "/foo/123", "/bar/baz/123"); diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactoryTests.java index 2b37a6312..4130242d4 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactoryTests.java @@ -53,6 +53,12 @@ public class SetPathGatewayFilterFactoryTests { testFilter("/baz/bar","/baz/bar", variables); } + @Test + public void setEncodedPathFilterWorks() { + HashMap variables = new HashMap<>(); + testFilter("/baz/foo%20bar","/baz/foo%20bar", variables); + } + @Test public void setPathFilterWithTemplateVarsWorks() { HashMap variables = new HashMap<>();