PATCH: ServletUnitHttpRequest.getReader

Gordon Henriksen <[email protected]>
Newsgroups gmane.comp.web.httpunit.devel
Message-ID <[email protected]>
Please find attached a patch to implement  
com.meterware.servletunit.ServletUnitHttpRequest.getReader.

— G


P.S. — I couldn't reach the CVS servers, so this is against the 1.6.2  
release. Let me know if it doesn't apply cleanly to the trunk.
getReader.patch (application/octet-stream, 3.6 KB)
diff -ru httpunit-1.6.2/src/com/meterware/servletunit/ServletUnitHttpRequest.java httpunit-1.6.2-getReader/src/com/meterware/servletunit/ServletUnitHttpRequest.java
--- httpunit-1.6.2/src/com/meterware/servletunit/ServletUnitHttpRequest.java	2006-04-11 20:17:21.000000000 -0400
+++ httpunit-1.6.2-getReader/src/com/meterware/servletunit/ServletUnitHttpRequest.java	2006-04-11 21:10:41.000000000 -0400
@@ -28,6 +28,7 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
 import java.net.MalformedURLException;
 import java.util.*;
 
@@ -334,6 +335,9 @@
      */
     public ServletInputStream getInputStream() throws IOException {
         if (_inputStream == null) {
+            if (_reader != null) {
+                throw new IllegalStateException( "The getReader method has been called on this request." );
+            }
             _inputStream = new ServletInputStreamImpl( _messageBody );
         }
         return _inputStream;
@@ -464,7 +468,17 @@
      **/
     public BufferedReader getReader() throws IOException {
         if (_reader == null) {
-            _reader = new BufferedReader( new InputStreamReader( getInputStream(), getCharacterEncoding() ) );
+            if (_inputStream != null) {
+                throw new IllegalStateException( "The getInputStream method has been called on this request." );
+            }
+            String encoding = getCharacterEncoding();
+            Charset charset;
+            if (encoding == null) {
+                charset = Charset.defaultCharset();
+            } else {
+                charset = Charset.forName( encoding );
+            }
+            _reader = new BufferedReader( new InputStreamReader( new ServletInputStreamImpl( _messageBody ), charset ) );
         }
         return _reader;
     }
diff -ru httpunit-1.6.2/test/com/meterware/servletunit/HttpServletRequestTest.java httpunit-1.6.2-getReader/test/com/meterware/servletunit/HttpServletRequestTest.java
--- httpunit-1.6.2/test/com/meterware/servletunit/HttpServletRequestTest.java	2006-03-26 21:16:46.000000000 -0500
+++ httpunit-1.6.2-getReader/test/com/meterware/servletunit/HttpServletRequestTest.java	2006-04-11 21:07:24.000000000 -0400
@@ -21,6 +21,7 @@
 *******************************************************************************************************************/
 import java.util.*;
 import java.net.MalformedURLException;
+import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.ByteArrayInputStream;
 import java.io.BufferedInputStream;
@@ -206,6 +207,20 @@
     }
 
 
+    public void testRequestReader() throws Exception {
+        String body = "12345678901234567890";
+        InputStream stream = new ByteArrayInputStream( body.getBytes( "UTF-8" ) );
+        WebRequest wr = new PutMethodWebRequest( "http://localhost/simple", stream, "text/plain" );
+        HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), body.getBytes() );
+
+        BufferedReader r = request.getReader();
+        char[] buffer = new char[ request.getContentLength() ];
+        assertEquals( "Chars read", body.length(), r.read( buffer ) );
+        assertEquals( "EOF", -1, r.read( buffer ) );
+        assertEquals( "Request content", body, new String(buffer) );
+    }
+
+
     public void testDefaultAttributes() throws Exception {
         WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" );
         HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY );
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.