Skip to content

Commit

Permalink
Skip optional group name/info lookup if regex fails. (#8145)
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime authored Jun 8, 2022
1 parent aba8aa6 commit 18653c4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,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 @@ -318,10 +325,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
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void testPrefixSpec()
}

@Test
public void testSuffixSpec()
public void testSuffixSpecTraditional()
{
RegexPathSpec spec = new RegexPathSpec("^(.*).do$");
assertEquals("^(.*).do$", spec.getDeclaration(), "Spec.pathSpec");
Expand All @@ -151,6 +151,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 18653c4

Please sign in to comment.