Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public ShortcutType shortcutType() {
public Predicate<ServerWebExchange> apply(Config config) {
final ArrayList<PathPattern> pathPatterns = new ArrayList<>();
synchronized (this.pathPatternParser) {
// FIXME: 5.0.0 setMatchOptionalTrailingSeparator missing
// pathPatternParser.setMatchOptionalTrailingSeparator(config.isMatchTrailingSlash());
config.getPatterns().forEach(pattern -> {
String basePath = webFluxProperties.getBasePath();
boolean basePathIsNotBlank = StringUtils.hasText(basePath);
Expand All @@ -110,17 +108,27 @@ public boolean test(ServerWebExchange exchange) {
s -> parsePath(exchange.getRequest().getURI().getRawPath()));

PathPattern match = null;
PathContainer pathForMatch = path;
for (int i = 0; i < pathPatterns.size(); i++) {
PathPattern pathPattern = pathPatterns.get(i);
if (pathPattern.matches(path)) {
match = pathPattern;
break;
}
else if (config.isMatchTrailingSlash() && path.value().endsWith("/")) {
PathContainer pathWithoutTrailingSlash = parsePath(
path.value().substring(0, path.value().length() - 1));
if (pathPattern.matches(pathWithoutTrailingSlash)) {
match = pathPattern;
pathForMatch = pathWithoutTrailingSlash;
break;
}
}
}

if (match != null) {
traceMatch("Pattern", match.getPatternString(), path, true);
PathMatchInfo pathMatchInfo = match.matchAndExtract(path);
PathMatchInfo pathMatchInfo = match.matchAndExtract(pathForMatch);
if (pathMatchInfo != null) {
putUriTemplateVariables(exchange, pathMatchInfo.getUriVariables());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.security.web.server.WebFilterChainProxy;
import org.springframework.security.web.server.firewall.StrictServerWebExchangeFirewall;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.web.server.ServerWebExchange;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.getUriTemplateVariables;

@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "debug=true")
@DirtiesContext
Expand Down Expand Up @@ -128,6 +131,48 @@ public void pathRouteWorksWithRegex() {
.valueEquals(ROUTE_ID_HEADER, "path_regex");
}

@Test
public void trailingSlashMatchesWhenMatchTrailingSlashEnabled() {
Config config = new Config().setPatterns(Arrays.asList("/abc/{id}/function")).setMatchTrailingSlash(true);
Predicate<ServerWebExchange> predicate = new PathRoutePredicateFactory(new WebFluxProperties()).apply(config);
MockServerWebExchange exchange = MockServerWebExchange
.from(MockServerHttpRequest.get("/abc/123/function/").build());

assertThat(predicate.test(exchange)).isTrue();
assertThat(getUriTemplateVariables(exchange)).containsEntry("id", "123");
}

@Test
public void trailingSlashDoesNotMatchWhenMatchTrailingSlashDisabled() {
Config config = new Config().setPatterns(Arrays.asList("/abc/{id}/function")).setMatchTrailingSlash(false);
Predicate<ServerWebExchange> predicate = new PathRoutePredicateFactory(new WebFluxProperties()).apply(config);
MockServerWebExchange exchange = MockServerWebExchange
.from(MockServerHttpRequest.get("/abc/123/function/").build());

assertThat(predicate.test(exchange)).isFalse();
}

@Test
public void pathWithoutTrailingSlashDoesNotMatchPatternWithTrailingSlash() {
Config config = new Config().setPatterns(Arrays.asList("/abc/123/function/")).setMatchTrailingSlash(true);
Predicate<ServerWebExchange> predicate = new PathRoutePredicateFactory(new WebFluxProperties()).apply(config);
MockServerWebExchange exchange = MockServerWebExchange
.from(MockServerHttpRequest.get("/abc/123/function").build());

assertThat(predicate.test(exchange)).isFalse();
}

@Test
public void patternWithTrailingSlashMatchesPathWithTrailingSlash() {
Config config = new Config().setPatterns(Arrays.asList("/abc/{id}/function/")).setMatchTrailingSlash(true);
Predicate<ServerWebExchange> predicate = new PathRoutePredicateFactory(new WebFluxProperties()).apply(config);
MockServerWebExchange exchange = MockServerWebExchange
.from(MockServerHttpRequest.get("/abc/123/function/").build());

assertThat(predicate.test(exchange)).isTrue();
assertThat(getUriTemplateVariables(exchange)).containsEntry("id", "123");
}

@Test
public void matchOptionalTrailingSeparatorCopiedToMatchTrailingSlash() {
Config config = new Config().setPatterns(Arrays.asList("patternA", "patternB")).setMatchTrailingSlash(false);
Expand Down