You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:14:09 UTC

[sling-org-apache-sling-servlet-helpers] 02/06: SLING-5974 implement getServletPath(), getPathInfo(), getRequestURI(), getRequestURL() and getAuthType() in MockSlingHttpServletRequest

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.servlet-helpers-1.1.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlet-helpers.git

commit 12b5ebeeb3b75909e4b4e577ba8a2f084cfd59c4
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Aug 5 12:30:07 2016 +0000

    SLING-5974 implement getServletPath(), getPathInfo(), getRequestURI(), getRequestURL() and getAuthType() in MockSlingHttpServletRequest
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/servlet-helpers@1755308 13f79535-47bb-0310-9956-ffa450edef68
---
 .../MockSlingHttpServletRequest.java               | 93 ++++++++++++++++++----
 .../apache/sling/servlethelpers/package-info.java  |  2 +-
 .../MockSlingHttpServletRequestTest.java           | 60 ++++++++++++++
 3 files changed, 137 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequest.java b/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequest.java
index 1d7204d..b995e9e 100644
--- a/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequest.java
+++ b/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequest.java
@@ -81,11 +81,13 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling
     private Map<String, String[]> parameterMap = new LinkedHashMap<String, String[]>();
     private HttpSession session;
     private Resource resource;
+    private String authType;
     private String contextPath;
     private String queryString;
     private String scheme = "http";
     private String serverName = "localhost";
     private int serverPort = 80;
+    private String servletPath = StringUtils.EMPTY;
     private String method = HttpConstants.METHOD_GET;
     private final HeaderSupport headerSupport = new HeaderSupport();
     private final CookieSupport cookieSupport = new CookieSupport();
@@ -632,56 +634,113 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling
         this.remotePort = remotePort;
     }
 
-    
-    // --- unsupported operations ---
+    @Override
+    public String getServletPath() {
+        return this.servletPath;
+    }
+
+    public void setServletPath(String servletPath) {
+        this.servletPath = servletPath;
+    }
 
     @Override
-    public RequestProgressTracker getRequestProgressTracker() {
-        throw new UnsupportedOperationException();
+    public String getPathInfo() {
+        RequestPathInfo requestPathInfo = this.getRequestPathInfo();
+
+        if (StringUtils.isEmpty(requestPathInfo.getResourcePath())) {
+            return null;
+        }
+
+        StringBuilder pathInfo = new StringBuilder();
+
+        pathInfo.append(requestPathInfo.getResourcePath());
+
+        if (StringUtils.isNotEmpty(requestPathInfo.getSelectorString())) {
+            pathInfo.append('.');
+            pathInfo.append(requestPathInfo.getSelectorString());
+        }
+
+        if (StringUtils.isNotEmpty(requestPathInfo.getExtension())) {
+            pathInfo.append('.');
+            pathInfo.append(requestPathInfo.getExtension());
+        }
+
+        if (StringUtils.isNotEmpty(requestPathInfo.getSuffix())) {
+            pathInfo.append(requestPathInfo.getSuffix());
+        }
+
+        return pathInfo.toString();
     }
 
     @Override
-    public String getResponseContentType() {
-        throw new UnsupportedOperationException();
+    public String getRequestURI() {
+        StringBuilder requestUri = new StringBuilder();
+
+        if (StringUtils.isNotEmpty(this.getServletPath())) {
+            requestUri.append(this.getServletPath());
+        }
+
+        if (StringUtils.isNotEmpty(this.getPathInfo())) {
+            requestUri.append(this.getPathInfo());
+        }
+
+        if (StringUtils.isEmpty(requestUri)) {
+            return "/";
+        } else {
+            return requestUri.toString();
+        }
     }
 
     @Override
-    public Enumeration<String> getResponseContentTypes() {
-        throw new UnsupportedOperationException();
+    public StringBuffer getRequestURL() {
+        StringBuffer requestUrl = new StringBuffer();
+
+        requestUrl.append(this.getScheme());
+        requestUrl.append("://");
+        requestUrl.append(getServerName());
+        if ((StringUtils.equals(this.getScheme(), "http") && this.getServerPort() != 80) ||
+                (StringUtils.equals(this.getScheme(), "https") && this.getServerPort() != 443)) {
+            requestUrl.append(':');
+            requestUrl.append(getServerPort());
+        }
+        requestUrl.append(getRequestURI());
+
+        return requestUrl;
     }
 
     @Override
     public String getAuthType() {
-        throw new UnsupportedOperationException();
+        return this.authType;
     }
 
-    @Override
-    public String getPathInfo() {
-        throw new UnsupportedOperationException();
+    public void setAuthType(String authType) {
+        this.authType = authType;
     }
 
+    // --- unsupported operations ---
+
     @Override
-    public String getPathTranslated() {
+    public RequestProgressTracker getRequestProgressTracker() {
         throw new UnsupportedOperationException();
     }
 
     @Override
-    public String getRequestURI() {
+    public String getResponseContentType() {
         throw new UnsupportedOperationException();
     }
 
     @Override
-    public StringBuffer getRequestURL() {
+    public Enumeration<String> getResponseContentTypes() {
         throw new UnsupportedOperationException();
     }
 
     @Override
-    public String getRequestedSessionId() {
+    public String getPathTranslated() {
         throw new UnsupportedOperationException();
     }
 
     @Override
-    public String getServletPath() {
+    public String getRequestedSessionId() {
         throw new UnsupportedOperationException();
     }
 
diff --git a/src/main/java/org/apache/sling/servlethelpers/package-info.java b/src/main/java/org/apache/sling/servlethelpers/package-info.java
index 1497ab6..0bfb804 100644
--- a/src/main/java/org/apache/sling/servlethelpers/package-info.java
+++ b/src/main/java/org/apache/sling/servlethelpers/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Mock implementation of selected Servlet-related Sling APIs.
  */
-@aQute.bnd.annotation.Version("1.0")
+@aQute.bnd.annotation.Version("1.1")
 package org.apache.sling.servlethelpers;
diff --git a/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java b/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
index 51b4155..e1bd3b6 100644
--- a/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
+++ b/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
@@ -88,6 +88,66 @@ public class MockSlingHttpServletRequestTest {
     }
 
     @Test
+    public void testPathInfo() {
+        MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
+        requestPathInfo.setResourcePath("/content/resource");
+        requestPathInfo.setExtension("html");
+        requestPathInfo.setSelectorString("a1.a2");
+        requestPathInfo.setSuffix("/content/another/resource.html");
+
+        assertEquals("/content/resource.a1.a2.html/content/another/resource.html", request.getPathInfo());
+
+        requestPathInfo.setSelectorString(null);
+
+        assertEquals("/content/resource.html/content/another/resource.html", request.getPathInfo());
+
+        requestPathInfo.setSuffix(null);
+
+        assertEquals("/content/resource.html", request.getPathInfo());
+
+        requestPathInfo.setResourcePath(null);
+
+        assertNull(request.getPathInfo());
+    }
+
+    @Test
+    public void testRequestUri() {
+        MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
+        requestPathInfo.setResourcePath("/content/resource");
+        requestPathInfo.setExtension("html");
+        requestPathInfo.setSelectorString("a1.a2");
+        requestPathInfo.setSuffix("/content/another/resource.html");
+
+        assertEquals("/content/resource.a1.a2.html/content/another/resource.html", request.getRequestURI());
+
+        request.setServletPath("/my");
+
+        assertEquals("/my/content/resource.a1.a2.html/content/another/resource.html", request.getRequestURI());
+    }
+
+    @Test
+    public void testRequestUrl() {
+        MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
+        requestPathInfo.setResourcePath("/content/resource");
+        requestPathInfo.setExtension("html");
+
+        assertEquals("http://localhost/content/resource.html", request.getRequestURL().toString());
+
+        request.setServerPort(8080);
+
+        assertEquals("http://localhost:8080/content/resource.html", request.getRequestURL().toString());
+
+        request.setScheme("https");
+        request.setServerPort(443);
+
+        assertEquals("https://localhost/content/resource.html", request.getRequestURL().toString());
+
+        request.setServerPort(8443);
+
+        assertEquals("https://localhost:8443/content/resource.html", request.getRequestURL().toString());
+    }
+
+    @Test
     public void testRequestPathInfo() {
         assertNotNull(request.getRequestPathInfo());
     }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.