This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.sling-mock-1.5.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit ec0efeee9f6b8a3e87e03511ff09aefb3f009a01 Author: Stefan Seifert <[email protected]> AuthorDate: Thu Sep 3 09:20:32 2015 +0000 SLING-4994 Support InputStream, ContentLenght, ContentType and CharacterEncoding in Request git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1700965 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/servlet/MockSlingHttpServletRequest.java | 87 +++++++++++++++------- .../servlet/MockSlingHttpServletResponse.java | 2 +- .../servlet/MockSlingHttpServletRequestTest.java | 31 ++++++++ 3 files changed, 94 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java index b7b8f21..5fa3068 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java @@ -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 extends SlingAdaptable implements Sling 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 extends SlingAdaptable implements Sling 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 extends SlingAdaptable implements Sling } @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(); } @@ -685,11 +727,6 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling } @Override - public void setCharacterEncoding(String env) { - throw new UnsupportedOperationException(); - } - - @Override public boolean authenticate(HttpServletResponse response) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.java b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.java index e48846f..6b9e7f8 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.java @@ -35,7 +35,7 @@ import org.apache.sling.api.adapter.SlingAdaptable; */ 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; diff --git a/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java b/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java index 4c379e4..3f0c52b 100644 --- a/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java +++ b/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java @@ -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 MockSlingHttpServletRequestTest { 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())); + } + } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
