[Httpunit-commit] CVS: httpunit/test/com/meterware/servletunit StatelessTest.java,1.10,1.11
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-03-17 17:37:12
|
Update of /cvsroot/httpunit/httpunit/test/com/meterware/servletunit In directory sc8-pr-cvs1:/tmp/cvs-serv1827/test/com/meterware/servletunit Modified Files: StatelessTest.java Log Message: Support Content-Length header Index: StatelessTest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/servletunit/StatelessTest.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- StatelessTest.java 3 Jan 2003 16:12:35 -0000 1.10 +++ StatelessTest.java 17 Mar 2003 17:37:05 -0000 1.11 @@ -19,16 +19,9 @@ * DEALINGS IN THE SOFTWARE. * *******************************************************************************************************************/ -import com.meterware.httpunit.GetMethodWebRequest; -import com.meterware.httpunit.HttpNotFoundException; -import com.meterware.httpunit.PostMethodWebRequest; -import com.meterware.httpunit.WebRequest; -import com.meterware.httpunit.WebResponse; -import com.meterware.httpunit.WebForm; - -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintWriter; +import com.meterware.httpunit.*; + +import java.io.*; import java.net.HttpURLConnection; import javax.servlet.ServletException; @@ -194,6 +187,21 @@ } + public void testStreamBasedPost() throws Exception { + ServletRunner sr = new ServletRunner(); + sr.registerServlet( "ReportData", BodyEcho.class.getName() ); + + String sourceData = "This is an interesting test\nWith two lines"; + InputStream source = new ByteArrayInputStream( sourceData.getBytes( "iso-8859-1" ) ); + + WebClient wc = sr.newClient(); + WebRequest wr = new PostMethodWebRequest( "http://localhost/ReportData", source, "text/sample" ); + WebResponse response = wc.getResponse( wr ); + assertEquals( "Body response", sourceData.length() + "\n" + sourceData, response.getText() ); + assertEquals( "Content-type", "text/sample", response.getContentType() ); + } + + public void testRequestInputStream() throws Exception { ServletRunner sr = new ServletRunner(); WebRequest request = new PostMethodWebRequest( "http://localhost/servlet/" + ParameterServlet.class.getName() ); @@ -280,6 +288,30 @@ } + + static class BodyEcho extends HttpServlet { + /** + * Returns a resource object as a result of a get request. + **/ + protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { + int length = req.getIntHeader( "Content-length" ); + String contentType = req.getHeader( "Content-type" ); + resp.setContentType( contentType ); + + InputStreamReader isr = new InputStreamReader( req.getInputStream() ); + BufferedReader br = new BufferedReader( isr ); + resp.getWriter().print( length ); + + String line = br.readLine(); + while (line != null) { + resp.getWriter().print( "\n" ); + resp.getWriter().print( line ); + line = br.readLine(); + } + resp.getWriter().flush(); + resp.getWriter().close(); + } + } static class FormSubmissionServlet extends HttpServlet { |