You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2020/01/29 15:48:51 UTC
[sling-org-apache-sling-servlets-resolver] branch master updated:
SLING-8110 - add a few missing tests
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git
The following commit(s) were added to refs/heads/master by this push:
new cab578f SLING-8110 - add a few missing tests
cab578f is described below
commit cab578f13c9c7c2e9479baf7d4bac691e10ace70
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed Jan 29 16:48:32 2020 +0100
SLING-8110 - add a few missing tests
---
.../internal/PathBasedServletAcceptor.java | 9 +++-
.../internal/PathBasedServletAcceptorTest.java | 57 ++++++++++++++++++++--
.../servlets/resolver/it/ServletSelectionIT.java | 19 +++++++-
3 files changed, 77 insertions(+), 8 deletions(-)
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptor.java b/src/main/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptor.java
index adcc150..1fea703 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptor.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptor.java
@@ -18,6 +18,8 @@
*/
package org.apache.sling.servlets.resolver.internal;
+import java.util.Arrays;
+
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
@@ -51,8 +53,8 @@ class PathBasedServletAcceptor {
// If the servlet properties have the "extpaths" option, check extension, selector etc.
boolean accepted = true;
- final Object extpaths = config.getServiceProperty(STRICT_PATHS_SERVICE_PROPERTY);
- if(extpaths != null && Boolean.valueOf(extpaths.toString())) {
+ final Object strictPaths = config.getServiceProperty(STRICT_PATHS_SERVICE_PROPERTY);
+ if(strictPaths != null && Boolean.valueOf(strictPaths.toString())) {
accepted =
accept(servletName, config, ServletResolverConstants.SLING_SERVLET_EXTENSIONS, request.getRequestPathInfo().getExtension())
&& accept(servletName, config, ServletResolverConstants.SLING_SERVLET_SELECTORS, request.getRequestPathInfo().getSelectors())
@@ -90,6 +92,9 @@ class PathBasedServletAcceptor {
return new String[] { (String)value };
} else if(value instanceof String []) {
return (String[]) value;
+ } else if(value instanceof Object []) {
+ final Object [] objArray = (Object[])value;
+ return Arrays.copyOf(objArray, objArray.length, String[].class);
}
return null;
}
diff --git a/src/test/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptorTest.java b/src/test/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptorTest.java
index 5bcc91f..5fd43aa 100644
--- a/src/test/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptorTest.java
+++ b/src/test/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptorTest.java
@@ -128,6 +128,30 @@ public class PathBasedServletAcceptorTest {
}
@Test
+ public void extensionNone() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_EXTENSIONS, "sp")
+ .assertAccept(false);
+ }
+
+ @Test
+ public void extensionNoMatchInN() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_EXTENSIONS, "one", "two")
+ .withExtension("somethingElse")
+ .assertAccept(false);
+ }
+
+ @Test
+ public void extensionMatchOneInN() {
+ new TestCase()
+ // test various ways of setting multiple properties
+ .withServiceProperty(ServletResolverTestSupport.P_EXTENSIONS, (Object)new String[] { "one", "two" })
+ .withExtension("one")
+ .assertAccept(true);
+ }
+
+ @Test
public void extensionPropertyNotSet() {
new TestCase()
.withExtension("somethingElse")
@@ -151,12 +175,26 @@ public class PathBasedServletAcceptorTest {
}
@Test
- public void selectorOneAmongSeveral() {
+ public void selectorOneFromNInN() {
new TestCase()
- .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "one")
- .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "two")
- .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "three")
- .withSelector("three")
+ .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "one", "two", "three")
+ .withSelectors("three", "and", "somethingElse")
+ .assertAccept(true);
+ }
+
+ @Test
+ public void selectorZeroInN() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "one", "two", "three")
+ .withExtension("three")
+ .assertAccept(false);
+ }
+
+ @Test
+ public void selectorOneInN() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "one", "two", "42")
+ .withSelectors("42")
.assertAccept(true);
}
@@ -201,6 +239,15 @@ public class PathBasedServletAcceptorTest {
}
@Test
+ public void testBooleanFalseStrict() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_STRICT_PATHS, false)
+ .withServiceProperty(ServletResolverTestSupport.P_METHODS, "meth")
+ .withMethod("somethingElse")
+ .assertAccept(true);
+ }
+
+ @Test
public void testNoSlingServletConfig() {
final Servlet s = mock(Servlet.class);
when(s.getServletConfig()).thenReturn(mock(ServletConfig.class));
diff --git a/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java b/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java
index 0f4cf5f..71b2827 100644
--- a/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java
+++ b/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java
@@ -75,6 +75,12 @@ public class ServletSelectionIT extends ServletResolverTestSupport {
.with(P_EXTENSIONS, "extPathsExt")
.with(P_SELECTORS, "extPathsSel")
.register(bundleContext);
+
+ new TestServlet("ExtPathsMultipleSelectors")
+ .with(P_PATHS, "/extpaths_multiple")
+ .with(P_STRICT_PATHS, "true")
+ .with(P_SELECTORS, new String[] { "one", "two" })
+ .register(bundleContext);
}
@Test
@@ -144,7 +150,7 @@ public class ServletSelectionIT extends ServletResolverTestSupport {
}
@Test
- public void testExtPathsServlet() throws Exception {
+ public void testExtPaths() throws Exception {
// We just check that the "extpaths" feature is wired in,
// the details of its logic are verified in unit tests
assertTestServlet("/extpaths", HttpServletResponse.SC_FORBIDDEN);
@@ -152,5 +158,16 @@ public class ServletSelectionIT extends ServletResolverTestSupport {
assertTestServlet(M_POST, "/extpaths.extPathsExt", HttpServletResponse.SC_FORBIDDEN);
assertTestServlet(M_GET, "/extpaths.extPathsSel.extPathsExt", HttpServletResponse.SC_FORBIDDEN);
assertTestServlet(M_POST, "/extpaths.extPathsSel.extPathsExt", "ExtPaths");
+ assertTestServlet(M_POST, "/extpaths.extPathsSel.extPathsExt/with/some/suffix", "ExtPaths");
+ }
+
+ @Test
+ public void testExtPathsMultipleSelectors() throws Exception {
+ assertTestServlet("/extpaths_multiple", HttpServletResponse.SC_FORBIDDEN);
+ assertTestServlet("/extpaths_multiple.one.html", "ExtPathsMultipleSelectors");
+ assertTestServlet("/extpaths_multiple.one.two.html", "ExtPathsMultipleSelectors");
+ assertTestServlet("/extpaths_multiple.two.three.html", "ExtPathsMultipleSelectors");
+ assertTestServlet("/extpaths_multiple.two.html", "ExtPathsMultipleSelectors");
+ assertTestServlet("/extpaths_multiple.three.html", HttpServletResponse.SC_FORBIDDEN);
}
}
\ No newline at end of file