Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegexPathSpec backport of optional group name/info lookup if regex fails #8145

Merged
merged 1 commit into from
Jun 8, 2022
Merged
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 @@ -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