Fix RegexPathElement when matching variables against a root path

The first fix for issue 15264 covered the case of using a single
variable (the case mentioned in the bug report). However, when
more than one variable is used a different PathElement is built.
This RegexPathElement needs a similar change that checks the
path includes data to bind.

Issue: SPR-15264
This commit is contained in:
Andy Clement
2017-02-19 17:06:49 -08:00
parent 9829a62044
commit d924538211
2 changed files with 32 additions and 1 deletions

View File

@@ -26,6 +26,7 @@ import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.util.AntPathMatcher;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
@@ -524,12 +525,40 @@ public class PathPatternMatcherTests {
assertTrue(pp.matches("/abc/boo"));
assertTrue(pp.matches("/a/boo"));
assertFalse(pp.matches("//boo"));
pp = parse("/{foo}*");
assertTrue(pp.matches("/abc"));
assertFalse(pp.matches("/"));
checkCapture("/{word:[a-z]*}", "/abc", "word", "abc");
pp = parse("/{word:[a-z]*}");
assertFalse(pp.matches("/1"));
assertTrue(pp.matches("/a"));
assertFalse(pp.matches("/"));
// Two captures mean we use a RegexPathElement
pp = new PathPatternParser().parse("/{foo}{bar}");
assertTrue(pp.matches("/abcdef"));
assertFalse(pp.matches("/"));
assertFalse(pp.matches("//"));
checkCapture("/{foo:[a-z][a-z]}{bar:[a-z]}", "/abc", "foo", "ab", "bar", "c");
// Only patterns not capturing variables cannot match against just /
pp = new PathPatternParser().parse("/****");
assertTrue(pp.matches("/abcdef"));
assertTrue(pp.matches("/"));
assertTrue(pp.matches("//"));
// Confirming AntPathMatcher behaviour:
assertFalse(new AntPathMatcher().match("/{foo}", "/"));
assertTrue(new AntPathMatcher().match("/{foo}", "/a"));
assertTrue(new AntPathMatcher().match("/{foo}{bar}", "/a"));
assertFalse(new AntPathMatcher().match("/{foo}*", "/"));
assertTrue(new AntPathMatcher().match("/*", "/"));
assertFalse(new AntPathMatcher().match("/*{foo}", "/"));
Map<String, String> vars = new AntPathMatcher().extractUriTemplateVariables("/{foo}{bar}", "/a");
assertEquals("a",vars.get("foo"));
assertEquals("",vars.get("bar"));
}
@Test