[Httpunit-commit] CVS: httpunit/test/com/meterware/servletunit StatelessTest.java,1.6,1.7 WebXMLTest
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-05-21 20:43:33
|
Update of /cvsroot/httpunit/httpunit/test/com/meterware/servletunit In directory usw-pr-cvs1:/tmp/cvs-serv8706/test/com/meterware/servletunit Modified Files: StatelessTest.java WebXMLTest.java Log Message: Some cleanup and refactoring Index: StatelessTest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/servletunit/StatelessTest.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- StatelessTest.java 16 May 2002 19:04:13 -0000 1.6 +++ StatelessTest.java 21 May 2002 20:43:31 -0000 1.7 @@ -78,7 +78,7 @@ public void testServletAccessByClassName() throws Exception { ServletRunner sr = new ServletRunner(); - WebRequest request = new GetMethodWebRequest( "http://localhost/" + SimpleGetServlet.class.getName() ); + WebRequest request = new GetMethodWebRequest( "http://localhost/servlet/" + SimpleGetServlet.class.getName() ); WebResponse response = sr.getResponse( request ); assertNotNull( "No response received", response ); assertEquals( "content type", "text/html", response.getContentType() ); @@ -155,6 +155,20 @@ assertNotNull( "No response received", response ); assertEquals( "content type", "text/plain", response.getContentType() ); assertEquals( "requested resource", "You posted red", response.getText() ); + } + + + public void testRequestInputStream() throws Exception { + ServletRunner sr = new ServletRunner(); + WebRequest request = new PostMethodWebRequest( "http://localhost/servlet/" + ParameterServlet.class.getName() ); + request.setParameter( "color", "green" ); + final String expectedBody = "color=green"; + InvocationContext ic = sr.newClient().newInvocation( request ); + assertEquals( "Message body type", "application/x-www-form-urlencoded", ic.getRequest().getContentType() ); + InputStream is = ic.getRequest().getInputStream(); + byte[] buffer = new byte[ expectedBody.length() ]; + assertEquals( "Input stream length", buffer.length, is.read( buffer ) ); + assertEquals( "Message body", expectedBody, new String( buffer ) ); } Index: WebXMLTest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/servletunit/WebXMLTest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- WebXMLTest.java 16 May 2002 19:04:13 -0000 1.8 +++ WebXMLTest.java 21 May 2002 20:43:31 -0000 1.9 @@ -257,32 +257,36 @@ public void testServletMapping() throws Exception { WebXMLString wxs = new WebXMLString(); - wxs.addServlet("/foo/bar/*", Servlet1.class); - wxs.addServlet("/baz/*", Servlet2.class); - wxs.addServlet("/catalog", Servlet3.class); - wxs.addServlet("*.bop", Servlet4.class); - ServletRunner sr = new ServletRunner( toInputStream( wxs.asText() )); + wxs.addServlet( "/foo/bar/*", Servlet1.class ); + wxs.addServlet( "/baz/*", Servlet2.class ); + wxs.addServlet( "/catalog", Servlet3.class ); + wxs.addServlet( "*.bop", Servlet4.class ); + ServletRunner sr = new ServletRunner( toInputStream( wxs.asText() ) ); ServletUnitClient wc = sr.newClient(); - InvocationContext ic = wc.newInvocation("http://localhost/foo/bar/index.html"); - assertTrue(ic.getServlet() instanceof Servlet1); - ic = wc.newInvocation("http://localhost/foo/bar/index.bop"); - assertTrue(ic.getServlet() instanceof Servlet1); - ic = wc.newInvocation("http://localhost/baz"); - assertTrue(ic.getServlet() instanceof Servlet2); - ic = wc.newInvocation("http://localhost/baz/index.html"); - assertTrue(ic.getServlet() instanceof Servlet2); - ic = wc.newInvocation("http://localhost/catalog"); - assertTrue(ic.getServlet() instanceof Servlet3); + + checkMapping( wc, "http://localhost/foo/bar/index.html", Servlet1.class, "/foo/bar", "/index.html" ); + checkMapping( wc, "http://localhost/foo/bar/index.bop", Servlet1.class, "/foo/bar", "/index.bop" ); + checkMapping( wc, "http://localhost/baz", Servlet2.class, "/baz", null ); + checkMapping( wc, "http://localhost/baz/index.html", Servlet2.class, "/baz", "/index.html" ); + checkMapping( wc, "http://localhost/catalog", Servlet3.class, "/catalog", null ); + checkMapping( wc, "http://localhost/catalog/racecar.bop", Servlet4.class, "/catalog/racecar.bop", null ); + checkMapping( wc, "http://localhost/index.bop", Servlet4.class, "/index.bop", null ); + try { - ic = wc.newInvocation("http://localhost/catalog/index.html"); - ic.getServlet(); - fail("Should have gotten a 404"); - } catch (HttpNotFoundException e) {} - ic = wc.newInvocation("http://localhost/catalog/racecar.bop"); - assertTrue(ic.getServlet() instanceof Servlet4); - ic = wc.newInvocation("http://localhost/index.bop"); - assertTrue(ic.getServlet() instanceof Servlet4); + wc.newInvocation( "http://localhost/catalog/index.html" ).getServlet(); + fail( "Should have gotten a 404" ); + } catch (HttpNotFoundException e) { + } } + + + private void checkMapping( ServletUnitClient wc, final String url, final Class servletClass, final String expectedPath, final String expectedInfo ) throws IOException, ServletException { + InvocationContext ic = wc.newInvocation( url ); + assertTrue( servletClass.isInstance( ic.getServlet() ) ); +// assertEquals( "ServletPath", expectedPath, ic.getRequest().getServletPath() ); +// assertEquals( "ServletInfo", expectedInfo, ic.getRequest().getPathInfo() ); + } + //=============================================================================================================== |