Browse Source

Use getRawPath in filters. Fixes #253

pull/275/head
Ryan Baxter 7 years ago
parent
commit
db1767f749
  1. 2
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactory.java
  2. 2
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java
  3. 2
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java
  4. 71
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactoryTest.java
  5. 5
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactoryTests.java
  6. 6
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactoryTests.java

2
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactory.java

@ -59,7 +59,7 @@ public class PrefixPathGatewayFilterFactory extends AbstractGatewayFilterFactory @@ -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)

2
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java

@ -49,7 +49,7 @@ public class RewritePathGatewayFilterFactory extends AbstractGatewayFilterFactor @@ -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()

2
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java

@ -65,7 +65,7 @@ public class SetPathGatewayFilterFactory extends AbstractGatewayFilterFactory<Se @@ -65,7 +65,7 @@ public class SetPathGatewayFilterFactory extends AbstractGatewayFilterFactory<Se
}
URI uri = uriTemplate.expand(uriVariables);
String newPath = uri.getPath();
String newPath = uri.getRawPath();
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);

71
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactoryTest.java

@ -0,0 +1,71 @@ @@ -0,0 +1,71 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.gateway.filter.factory;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.util.LinkedHashSet;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR;
/**
* @author Ryan Baxter
*/
public class PrefixPathGatewayFilterFactoryTest {
@Test
public void testPrefixPath() {
testPrefixPathFilter("/foo", "/bar", "/foo/bar");
testPrefixPathFilter("/foo", "/hello%20world", "/foo/hello%20world");
}
private void testPrefixPathFilter(String prefix, String path, String expectedPath) {
GatewayFilter filter = new PrefixPathGatewayFilterFactory().apply(c -> c.setPrefix(prefix));
MockServerHttpRequest request = MockServerHttpRequest
.get("http://localhost" + path)
.build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
ArgumentCaptor<ServerWebExchange> 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<URI> uris = webExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
assertThat(uris).contains(request.getURI());
}
}

5
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactoryTests.java

@ -51,6 +51,11 @@ public class RewritePathGatewayFilterFactoryTests { @@ -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/(?<id>\\d.*)", "/bar/baz/$\\{id}", "/foo/123", "/bar/baz/123");

6
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactoryTests.java

@ -53,6 +53,12 @@ public class SetPathGatewayFilterFactoryTests { @@ -53,6 +53,12 @@ public class SetPathGatewayFilterFactoryTests {
testFilter("/baz/bar","/baz/bar", variables);
}
@Test
public void setEncodedPathFilterWorks() {
HashMap<String, String> variables = new HashMap<>();
testFilter("/baz/foo%20bar","/baz/foo%20bar", variables);
}
@Test
public void setPathFilterWithTemplateVarsWorks() {
HashMap<String, String> variables = new HashMap<>();

Loading…
Cancel
Save