From db1767f749b608ae55d44d3367865aa1d7babcfc Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Tue, 10 Apr 2018 20:07:00 -0400 Subject: [PATCH] Use getRawPath in filters. Fixes #253 --- .../PrefixPathGatewayFilterFactory.java | 2 +- .../RewritePathGatewayFilterFactory.java | 2 +- .../factory/SetPathGatewayFilterFactory.java | 2 +- .../PrefixPathGatewayFilterFactoryTest.java | 71 +++++++++++++++++++ .../RewritePathGatewayFilterFactoryTests.java | 5 ++ .../SetPathGatewayFilterFactoryTests.java | 6 ++ 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactoryTest.java 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<>();