Merge pull request #275 from ryanjbaxter/use-raw-paths
Use getRawPath in filters
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,6 +48,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");
|
||||
|
||||
@@ -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<>();
|
||||
|
||||
Reference in New Issue
Block a user