Skip to content

Commit

Permalink
Skip optional group name/info lookup if regex fails.
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Jun 8, 2022
1 parent 590adf2 commit 4e419d8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
Expand Up @@ -289,10 +289,17 @@ public RegexMatchedPath(RegexPathSpec regexPathSpec, String path, Matcher matche
@Override
public String getPathMatch()
{
String p = matcher.group("name");
if (p != null)
try
{
return p;
String p = matcher.group("name");
if (p != null)
{
return p;
}
}
catch (IllegalArgumentException ignore)
{
// ignore if group name not found.
}

if (pathSpec.getGroup() == PathSpecGroup.PREFIX_GLOB && matcher.groupCount() >= 1)
Expand All @@ -313,10 +320,17 @@ public String getPathMatch()
@Override
public String getPathInfo()
{
String p = matcher.group("info");
if (p != null)
try
{
String p = matcher.group("info");
if (p != null)
{
return p;
}
}
catch (IllegalArgumentException ignore)
{
return p;
// ignore if group info not found.
}

// Path Info only valid for PREFIX_GLOB
Expand Down
Expand Up @@ -129,7 +129,7 @@ public void testPrefixSpec()
}

@Test
public void testSuffixSpec()
public void testSuffixSpecTraditional()
{
RegexPathSpec spec = new RegexPathSpec("^(.*).do$");
assertEquals("^(.*).do$", spec.getDeclaration(), "Spec.pathSpec");
Expand All @@ -146,6 +146,47 @@ public void testSuffixSpec()
assertNotMatches(spec, "/aa");
assertNotMatches(spec, "/aa/bb");
assertNotMatches(spec, "/aa/bb.do/more");

assertThat(spec.getPathMatch("/a/b/c.do"), equalTo("/a/b/c.do"));
assertThat(spec.getPathInfo("/a/b/c.do"), nullValue());
}

/**
* A suffix type path spec, where the beginning of the path is evaluated
* but the rest of the path is ignored.
* The beginning is starts with a glob, contains a literal, and no terminal "$".
*/
@Test
public void testSuffixSpecGlobish()
{
RegexPathSpec spec = new RegexPathSpec("^/[Hh]ello");
assertEquals("^/[Hh]ello", spec.getDeclaration(), "Spec.pathSpec");
assertEquals("^/[Hh]ello", spec.getPattern().pattern(), "Spec.pattern");
assertEquals(1, spec.getPathDepth(), "Spec.pathDepth");
assertEquals(PathSpecGroup.SUFFIX_GLOB, spec.getGroup(), "Spec.group");

assertMatches(spec, "/hello");
assertMatches(spec, "/Hello");

assertNotMatches(spec, "/Hello/World");
assertNotMatches(spec, "/a");
assertNotMatches(spec, "/aa");
assertNotMatches(spec, "/aa/bb");
assertNotMatches(spec, "/aa/bb.do/more");

assertThat(spec.getPathMatch("/hello"), equalTo("/hello"));
assertThat(spec.getPathInfo("/hello"), nullValue());

assertThat(spec.getPathMatch("/Hello"), equalTo("/Hello"));
assertThat(spec.getPathInfo("/Hello"), nullValue());

MatchedPath matchedPath = spec.matched("/hello");
assertThat(matchedPath.getPathMatch(), equalTo("/hello"));
assertThat(matchedPath.getPathInfo(), nullValue());

matchedPath = spec.matched("/Hello");
assertThat(matchedPath.getPathMatch(), equalTo("/Hello"));
assertThat(matchedPath.getPathInfo(), nullValue());
}

@Test
Expand Down

0 comments on commit 4e419d8

Please sign in to comment.