Issue #7748 - Merge PathSpec improvements made in 9.4.x (#7845)

* Fixes to backport of #7748

+ Backport of #7748
+ Fix RegexPathSpec pathInfo
+ Fix UriTemplatePathSpec pathInfo
+ Test regression option to 93 behaviour

* small optimization

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2022-04-06 11:14:52 +02:00 committed by GitHub
parent 0837aadb8e
commit 5fc63bf8e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 3 deletions

View File

@ -135,7 +135,7 @@ public class RegexPathSpec extends AbstractPathSpec
Matcher matcher = getMatcher(path);
if (matcher.matches())
{
if (matcher.groupCount() >= 1)
if (_group == PathSpecGroup.PREFIX_GLOB && matcher.groupCount() >= 1)
{
int idx = matcher.start(1);
if (idx > 0)

View File

@ -345,7 +345,7 @@ public class ServletPathSpec extends AbstractPathSpec
case PREFIX_GLOB:
if (path.length() == (_specLength - 2))
return null;
return path.substring(_specLength - 2);
return _specLength == 2 ? path : path.substring(_specLength - 2);
default:
return null;

View File

@ -353,7 +353,7 @@ public class UriTemplatePathSpec extends AbstractPathSpec
Matcher matcher = getMatcher(path);
if (matcher.matches())
{
if (matcher.groupCount() >= 1)
if (_group == PathSpecGroup.PREFIX_GLOB && matcher.groupCount() >= 1)
{
int idx = matcher.start(1);
if (idx > 0)

View File

@ -19,7 +19,9 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class RegexPathSpecTest
{
@ -71,6 +73,20 @@ public class RegexPathSpecTest
assertNotMatches(spec, "/rest/list");
}
@Test
public void testPathInfo()
{
RegexPathSpec spec = new RegexPathSpec("^/test(/.*)?$");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test"));
assertThat(spec.getPathInfo("/test/info"), equalTo("/info"));
spec = new RegexPathSpec("^/[Tt]est(/.*)?$");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test/info"));
assertThat(spec.getPathInfo("/test/info"), nullValue());
}
@Test
public void testMiddleSpecNoGrouping()
{

View File

@ -22,7 +22,9 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Tests for URI Template Path Specs
@ -142,6 +144,20 @@ public class UriTemplatePathSpecTest
assertEquals("b", mapped.get("var"), "Spec.pathParams[var]");
}
@Test
public void testPathInfo()
{
UriTemplatePathSpec spec = new UriTemplatePathSpec("/test/{var}");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test"));
assertThat(spec.getPathInfo("/test/info"), equalTo("info"));
spec = new UriTemplatePathSpec("/{x}/test/{y}");
assertTrue(spec.matches("/try/test/info"));
assertThat(spec.getPathMatch("/try/test/info"), equalTo("/try/test/info"));
assertThat(spec.getPathInfo("/try/test/info"), nullValue());
}
@Test
public void testOneVarPathSpec()
{