sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sseif...@apache.org
Subject svn commit: r1700965 - in /sling/trunk/testing/mocks/sling-mock/src: main/java/org/apache/sling/testing/mock/sling/servlet/ test/java/org/apache/sling/testing/mock/sling/servlet/
Date Thu, 03 Sep 2015 09:20:33 GMT
Author: sseifert
Date: Thu Sep  3 09:20:32 2015
New Revision: 1700965

URL: http://svn.apache.org/r1700965
Log:
SLING-4994 Support InputStream, ContentLenght, ContentType and CharacterEncoding in Request

Modified:
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.java
    sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java

Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java?rev=1700965&r1=1700964&r2=1700965&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java
(original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java
Thu Sep  3 09:20:32 2015
@@ -18,7 +18,12 @@
  */
 package org.apache.sling.testing.mock.sling.servlet;
 
+import static org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse.CHARSET_SEPARATOR;
+
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
@@ -88,6 +93,9 @@ public class MockSlingHttpServletRequest
     private String method = HttpConstants.METHOD_GET;
     private final HeaderSupport headerSupport = new HeaderSupport();
     private final CookieSupport cookieSupport = new CookieSupport();
+    private String contentType;
+    private String characterEncoding;
+    private byte[] content;
     
     private static final ResourceBundle EMPTY_RESOURCE_BUNDLE = new ListResourceBundle()
{
         @Override
@@ -506,6 +514,60 @@ public class MockSlingHttpServletRequest
         return params;
     }
 
+    @Override
+    public String getCharacterEncoding() {
+        return this.characterEncoding;
+    }
+
+    @Override
+    public void setCharacterEncoding(String charset) {
+        this.characterEncoding = charset;
+    }
+
+    @Override
+    public String getContentType() {
+        if (this.contentType == null) {
+            return null;
+        } else {
+            return this.contentType
+                    + (StringUtils.isNotBlank(characterEncoding) ? CHARSET_SEPARATOR + characterEncoding
: "");
+        }
+    }
+    
+    public void setContentType(String type) {
+        this.contentType = type;
+        if (StringUtils.contains(this.contentType, CHARSET_SEPARATOR)) {
+            this.characterEncoding = StringUtils.substringAfter(this.contentType, CHARSET_SEPARATOR);
+            this.contentType = StringUtils.substringBefore(this.contentType, CHARSET_SEPARATOR);
+        }
+    }
+
+    @Override
+    public ServletInputStream getInputStream() {
+        if (content == null) {
+            return null;
+        }
+        return new ServletInputStream() {
+            private final InputStream is = new ByteArrayInputStream(content);
+            @Override
+            public int read() throws IOException {
+                return is.read();
+            }
+        };  
+    }
+
+    @Override
+    public int getContentLength() {
+        if (content == null) {
+            return 0;
+        }
+        return content.length;
+    }
+    
+    public void setContent(byte[] content) {
+        this.content = content;
+    }
+
 
     // --- unsupported operations ---
 
@@ -610,26 +672,6 @@ public class MockSlingHttpServletRequest
     }
 
     @Override
-    public String getCharacterEncoding() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Override
-    public int getContentLength() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Override
-    public String getContentType() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Override
-    public ServletInputStream getInputStream() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Override
     public String getLocalAddr() {
         throw new UnsupportedOperationException();
     }
@@ -684,11 +726,6 @@ public class MockSlingHttpServletRequest
         throw new UnsupportedOperationException();
     }
 
-    @Override
-    public void setCharacterEncoding(String env) {
-        throw new UnsupportedOperationException();
-    }
-
     @Override
     public boolean authenticate(HttpServletResponse response) {
         throw new UnsupportedOperationException();

Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.java?rev=1700965&r1=1700964&r2=1700965&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.java
(original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.java
Thu Sep  3 09:20:32 2015
@@ -35,7 +35,7 @@ import org.apache.sling.api.adapter.Slin
  */
 public class MockSlingHttpServletResponse extends SlingAdaptable implements SlingHttpServletResponse
{
 
-    private static final String CHARSET_SEPARATOR = ";charset=";
+    static final String CHARSET_SEPARATOR = ";charset=";
 
     private String contentType;
     private String characterEncoding;

Modified: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java?rev=1700965&r1=1700964&r2=1700965&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java
(original)
+++ sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java
Thu Sep  3 09:20:32 2015
@@ -41,6 +41,7 @@ import java.util.ResourceBundle;
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpSession;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.CharEncoding;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -266,4 +267,34 @@ public class MockSlingHttpServletRequest
         assertNull(request.getRequestParameters("unknown"));
     }
 
+    @Test
+    public void testContentTypeCharset() throws Exception {
+        assertNull(request.getContentType());
+        assertNull(request.getCharacterEncoding());
+
+        request.setContentType("image/gif");
+        assertEquals("image/gif", request.getContentType());
+        assertNull(request.getCharacterEncoding());
+        
+        request.setContentType("text/plain;charset=UTF-8");
+        assertEquals("text/plain;charset=UTF-8", request.getContentType());
+        assertEquals(CharEncoding.UTF_8, request.getCharacterEncoding());
+        
+        request.setCharacterEncoding(CharEncoding.ISO_8859_1);
+        assertEquals("text/plain;charset=ISO-8859-1", request.getContentType());
+        assertEquals(CharEncoding.ISO_8859_1, request.getCharacterEncoding());
+    }
+
+    @Test
+    public void testContent() throws Exception {
+        assertEquals(0, request.getContentLength());
+        assertNull(request.getInputStream());
+        
+        byte[] data = new byte[] { 0x01,0x02,0x03 };
+        request.setContent(data);
+
+        assertEquals(data.length, request.getContentLength());
+        assertArrayEquals(data, IOUtils.toByteArray(request.getInputStream()));
+    }
+    
 }



Mime
View raw message