[Httpunit-commit] CVS: httpunit/test/com/meterware/servletunit HttpServletRequestTest.java,1.13,1.14
Brought to you by:
russgold
Update of /cvsroot/httpunit/httpunit/test/com/meterware/servletunit
In directory sc8-pr-cvs1:/tmp/cvs-serv7442/test/com/meterware/servletunit
Modified Files:
HttpServletRequestTest.java NavigationTest.java
RequestContextTest.java RequestDispatcherTest.java
WebXMLTest.java
Log Message:
Implemented request property changes for RequestDispatcher calls
Index: HttpServletRequestTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/servletunit/HttpServletRequestTest.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- HttpServletRequestTest.java 13 Feb 2003 15:37:01 -0000 1.13
+++ HttpServletRequestTest.java 13 Feb 2003 20:25:56 -0000 1.14
@@ -339,7 +339,7 @@
private final static byte[] NO_MESSAGE_BODY = new byte[0];
- private final static ServletRequest NULL_SERVLET_REQUEST = new ServletRequest() {
+ private final static ServletMetaData NULL_SERVLET_REQUEST = new ServletMetaData() {
public Servlet getServlet() throws ServletException {
return null;
Index: NavigationTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/servletunit/NavigationTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- NavigationTest.java 6 Feb 2003 01:33:11 -0000 1.4
+++ NavigationTest.java 13 Feb 2003 20:25:56 -0000 1.5
@@ -55,9 +55,9 @@
sr.registerServlet( "origin", OriginServlet.class.getName() );
WebClient wc = sr.newClient();
- WebResponse response = wc.getResponse( "http://localhost/origin" );
+ WebResponse response = wc.getResponse( "http://localhost/origin?color=green" );
assertNotNull( "No response received", response );
- assertEquals( "requested resource", TargetServlet.RESPONSE_TEXT, response.getText() );
+ assertEquals( "Expected response", "color=null: path=/target", response.getText() );
assertEquals( "Returned cookie count", 0, response.getNewCookieNames().length );
}
@@ -70,14 +70,26 @@
ServletRunner sr = new ServletRunner( wxs.asInputStream(), "/context" );
WebClient wc = sr.newClient();
- WebResponse response = wc.getResponse( "http://localhost/context/origin" );
+ WebResponse response = wc.getResponse( "http://localhost/context/origin?color=green" );
assertNotNull( "No response received", response );
- assertEquals( "requested resource", TargetServlet.RESPONSE_TEXT, response.getText() );
+ assertEquals( "Expected response", "color=green: path=/context/target", response.getText() );
assertEquals( "Returned cookie count", 0, response.getNewCookieNames().length );
}
+ public void testInclude() throws Exception {
+ WebXMLString wxs = new WebXMLString();
+ wxs.addServlet( "/target", TargetServlet.class );
+ wxs.addServlet( "/origin", IncluderServlet.class );
+ ServletRunner sr = new ServletRunner( wxs.asInputStream(), "/context" );
+
+ WebClient wc = sr.newClient();
+ WebResponse response = wc.getResponse( "http://localhost/context/origin?color=green" );
+ assertNotNull( "No response received", response );
+ assertEquals( "Expected response", "expecting: color=blue: path=/context/origin", response.getText() );
+ assertEquals( "Returned cookie count", 0, response.getNewCookieNames().length );
+ }
static class OriginServlet extends HttpServlet {
@@ -103,20 +115,19 @@
static final String PREFIX = "expecting: ";
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException,IOException {
- resp.getOutputStream().print( PREFIX );
- getServletContext().getRequestDispatcher( "/target" ).include( req, resp );
+ resp.getWriter().print( PREFIX );
+ getServletContext().getRequestDispatcher( "/target?color=blue" ).include( req, resp );
}
}
static class TargetServlet extends HttpServlet {
- static String RESPONSE_TEXT = "the desired content\r\n";
-
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException,IOException {
resp.setContentType( "text/plain" );
PrintWriter pw = resp.getWriter();
- pw.print( RESPONSE_TEXT );
+ pw.print( "color=" + req.getParameter( "color" ) );
+ pw.print( ": path=" + req.getRequestURI() );
pw.close();
}
Index: RequestContextTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/servletunit/RequestContextTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RequestContextTest.java 12 Feb 2003 13:47:23 -0000 1.1
+++ RequestContextTest.java 13 Feb 2003 20:25:56 -0000 1.2
@@ -22,6 +22,19 @@
import com.meterware.httpunit.HttpUnitTest;
import java.net.URL;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Locale;
+import java.security.Principal;
+import java.io.UnsupportedEncodingException;
+import java.io.IOException;
+import java.io.BufferedReader;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpSession;
+import javax.servlet.ServletInputStream;
+import javax.servlet.RequestDispatcher;
import junit.framework.TestSuite;
@@ -63,12 +76,270 @@
* Verify parsing of a query string.
*/
public void testParameterOverride() throws Exception {
- RequestContext rc1 = new RequestContext( new URL( "http://localhost/basic?param=red¶m1=old¶m=blue" ) );
- RequestContext rc2 = new RequestContext( new URL( "http://localhost/second?param=yellow¶m2=fast" ));
- rc2.setParentContext( rc1 );
- assertMatchingSet( "parameter names", new String[] { "param", "param1", "param2" }, rc2.getParameterNames() );
- assertMatchingSet( "param values", new String[] { "yellow" }, rc2.getParameterValues( "param" ) );
- assertEquals( "param1 value", "old", ((String[]) rc2.getParameterMap().get( "param1"))[0] );
+ HttpServletRequest request = new DummyHttpServletRequest( new URL( "http://localhost/basic?param=red¶m1=old¶m=blue" ) );
+ RequestContext context = new RequestContext( new URL( "http://localhost/second?param=yellow¶m2=fast" ));
+ context.setParentRequest( request );
+ assertMatchingSet( "parameter names", new String[] { "param", "param1", "param2" }, context.getParameterNames() );
+ assertMatchingSet( "param values", new String[] { "yellow" }, context.getParameterValues( "param" ) );
+ assertEquals( "param1 value", "old", ((String[]) context.getParameterMap().get( "param1"))[0] );
+ }
+
+
+ class DummyHttpServletRequest implements HttpServletRequest {
+
+ private RequestContext _requestContext;
+
+
+ public DummyHttpServletRequest( URL requestURL ) {
+ _requestContext = new RequestContext( requestURL );
+ }
+
+
+ public String getAuthType() {
+ return null;
+ }
+
+
+ public Cookie[] getCookies() {
+ return new Cookie[0];
+ }
+
+
+ public long getDateHeader( String s ) {
+ return 0;
+ }
+
+
+ public String getHeader( String s ) {
+ return null;
+ }
+
+
+ public Enumeration getHeaders( String s ) {
+ return null;
+ }
+
+
+ public Enumeration getHeaderNames() {
+ return null;
+ }
+
+
+ public int getIntHeader( String s ) {
+ return 0;
+ }
+
+
+ public String getMethod() {
+ return null;
+ }
+
+
+ public String getPathInfo() {
+ return null;
+ }
+
+
+ public String getPathTranslated() {
+ return null;
+ }
+
+
+ public String getContextPath() {
+ return null;
+ }
+
+
+ public String getQueryString() {
+ return null;
+ }
+
+
+ public String getRemoteUser() {
+ return null;
+ }
+
+
+ public boolean isUserInRole( String s ) {
+ return false;
+ }
+
+
+ public Principal getUserPrincipal() {
+ return null;
+ }
+
+
+ public String getRequestedSessionId() {
+ return null;
+ }
+
+
+ public String getRequestURI() {
+ return null;
+ }
+
+
+ public StringBuffer getRequestURL() {
+ return null;
+ }
+
+
+ public String getServletPath() {
+ return null;
+ }
+
+
+ public HttpSession getSession( boolean b ) {
+ return null;
+ }
+
+
+ public HttpSession getSession() {
+ return null;
+ }
+
+
+ public boolean isRequestedSessionIdValid() {
+ return false;
+ }
+
+
+ public boolean isRequestedSessionIdFromCookie() {
+ return false;
+ }
+
+
+ public boolean isRequestedSessionIdFromURL() {
+ return false;
+ }
+
+
+ public boolean isRequestedSessionIdFromUrl() {
+ return false;
+ }
+
+
+ public Object getAttribute( String s ) {
+ return null;
+ }
+
+
+ public Enumeration getAttributeNames() {
+ return null;
+ }
+
+
+ public String getCharacterEncoding() {
+ return null;
+ }
+
+
+ public void setCharacterEncoding( String s ) throws UnsupportedEncodingException {
+ }
+
+
+ public int getContentLength() {
+ return 0;
+ }
+
+
+ public String getContentType() {
+ return null;
+ }
+
+
+ public ServletInputStream getInputStream() throws IOException {
+ return null;
+ }
+
+
+ public String getParameter( String s ) {
+ return _requestContext.getParameter( s );
+ }
+
+
+ public Enumeration getParameterNames() {
+ return _requestContext.getParameterNames();
+ }
+
+
+ public String[] getParameterValues( String s ) {
+ return _requestContext.getParameterValues( s );
+ }
+
+
+ public Map getParameterMap() {
+ return _requestContext.getParameterMap();
+ }
+
+
+ public String getProtocol() {
+ return null;
+ }
+
+
+ public String getScheme() {
+ return null;
+ }
+
+
+ public String getServerName() {
+ return null;
+ }
+
+
+ public int getServerPort() {
+ return 0;
+ }
+
+
+ public BufferedReader getReader() throws IOException {
+ return null;
+ }
+
+
+ public String getRemoteAddr() {
+ return null;
+ }
+
+
+ public String getRemoteHost() {
+ return null;
+ }
+
+
+ public void setAttribute( String s, Object o ) {
+ }
+
+
+ public void removeAttribute( String s ) {
+ }
+
+
+ public Locale getLocale() {
+ return null;
+ }
+
+
+ public Enumeration getLocales() {
+ return null;
+ }
+
+
+ public boolean isSecure() {
+ return false;
+ }
+
+
+ public RequestDispatcher getRequestDispatcher( String s ) {
+ return null;
+ }
+
+
+ public String getRealPath( String s ) {
+ return null;
+ }
}
Index: RequestDispatcherTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/servletunit/RequestDispatcherTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RequestDispatcherTest.java 12 Feb 2003 13:47:24 -0000 1.1
+++ RequestDispatcherTest.java 13 Feb 2003 20:25:56 -0000 1.2
@@ -1,10 +1,28 @@
package com.meterware.servletunit;
-
+/********************************************************************************************************************
+* $Id$
+*
+* Copyright (c) 2003, Russell Gold
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or substantial portions
+* of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
+* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+* DEALINGS IN THE SOFTWARE.
+*
+*******************************************************************************************************************/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
-import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -14,10 +32,6 @@
import junit.framework.Test;
import junit.framework.TestSuite;
-import com.meterware.httpunit.GetMethodWebRequest;
-import com.meterware.httpunit.WebRequest;
-import com.meterware.httpunit.WebResponse;
-
public class RequestDispatcherTest extends TestCase {
@@ -61,6 +75,7 @@
InvocationContext ic = _runner.newClient().newInvocation( "http://localhost/sample/" + outerServletName + "?param=original¶m1=first" );
final HttpServletRequest request = ic.getRequest();
+ final HttpServletResponse response = ic.getResponse();
RequestDispatcherServlet servlet = (RequestDispatcherServlet) ic.getServlet();
RequestDispatcher rd = servlet.getServletContext().getRequestDispatcher( "/" + innerServletName + "?param=revised¶m2=new" );
@@ -68,21 +83,26 @@
assertEquals( "param1", "first", request.getParameter( "param1" ) );
assertNull( "param2 should not be defined", request.getParameter( "param2" ) );
- ic.pushIncludedContext( rd );
+ ic.pushIncludeRequest( rd, request, response );
+
+ final HttpServletRequest innerRequest = ic.getRequest();
+ assertEquals( "param in included servlet", "revised", innerRequest.getParameter( "param" ) );
+ assertEquals( "param1 in included servlet", "first", innerRequest.getParameter( "param1" ) );
+ assertEquals( "param2 in included servlet", "new", innerRequest.getParameter( "param2" ) );
- assertEquals( "param in included servlet", "revised", request.getParameter( "param" ) );
- assertEquals( "param1 in included servlet", "first", request.getParameter( "param1" ) );
- assertEquals( "param2 in included servlet", "new", request.getParameter( "param2" ) );
+ assertEquals( "Included servlet class", IncludedServlet.class, ic.getServlet().getClass() );
- ic.popContext();
+ ic.popRequest();
- assertEquals( "reverted param", "original", request.getParameter( "param" ) );
- assertEquals( "reverted param1", "first", request.getParameter( "param1" ) );
- assertNull( "reverted param2 should not be defined", request.getParameter( "param2" ) );
+ final HttpServletRequest restoredRequest = ic.getRequest();
+ assertEquals( "reverted param", "original", restoredRequest.getParameter( "param" ) );
+ assertEquals( "reverted param1", "first", restoredRequest.getParameter( "param1" ) );
+ assertNull( "reverted param2 should not be defined", restoredRequest.getParameter( "param2" ) );
+ assertEquals( "Included servlet class", RequestDispatcherServlet.class, ic.getServlet().getClass() );
}
- public void notestRequestDispatcherIncludePaths() throws Exception {
+ public void testRequestDispatcherIncludePaths() throws Exception {
InvocationContext ic = _runner.newClient().newInvocation( "http://localhost/sample/" + outerServletName + "?param=original¶m1=first" );
final HttpServletRequest request = ic.getRequest();
@@ -95,51 +115,67 @@
assertNull( "path info not null attribute", request.getPathInfo() );
// assertEquals( "query string attribute", "param=original¶m1=first", request.getQueryString() ); TODO make this work
- ic.pushIncludedContext( rd );
+ final HttpServletResponse response = ic.getResponse();
+ ic.pushIncludeRequest( rd, request, response );
+
+ final HttpServletRequest innerRequest = ic.getRequest();
+ assertEquals( "request URI", "/sample/" + outerServletName, innerRequest.getRequestURI() );
+ assertEquals( "context path attribute", "/sample", innerRequest.getContextPath() );
+ assertEquals( "servlet path attribute", "/" + outerServletName, innerRequest.getServletPath() );
+ assertNull( "path info not null attribute", innerRequest.getPathInfo() );
+// assertEquals( "query string attribute", "param=original¶m1=first", innerRequest.getQueryString() );
+
+ assertEquals( "request URI attribute", "/sample/" + innerServletName, innerRequest.getAttribute( REQUEST_URI ) );
+ assertEquals( "context path attribute", "/sample", innerRequest.getAttribute( CONTEXT_PATH ) );
+ assertEquals( "servlet path attribute", "/" + innerServletName, innerRequest.getAttribute( SERVLET_PATH ) );
+ assertNull( "path info attribute not null", innerRequest.getAttribute( PATH_INFO ) );
+// assertEquals( "query string attribute", "param=revised¶m2=new", innerRequest.getAttribute( QUERY_STRING ) );
+
+ ic.popRequest();
+ final HttpServletRequest restoredRequest = ic.getRequest();
+
+ assertNull( "reverted URI attribute not null", restoredRequest.getAttribute( REQUEST_URI ) );
+ assertNull( "context path attribute not null", restoredRequest.getAttribute( CONTEXT_PATH ) );
+ assertNull( "servlet path attribute not null", restoredRequest.getAttribute( SERVLET_PATH ) );
+ assertNull( "path info attribute not null", restoredRequest.getAttribute( PATH_INFO ) );
+// assertNull( "query string attribute not null", "param=revised¶m2=new", restoredRequest.getAttribute( QUERY_STRING ) );
+ }
+
+
+
+
+ public void testRequestDispatcherForwardPaths() throws Exception {
+ InvocationContext ic = _runner.newClient().newInvocation( "http://localhost/sample/" + outerServletName + "?param=original¶m1=first" );
+
+ final HttpServletRequest request = ic.getRequest();
+ RequestDispatcherServlet servlet = (RequestDispatcherServlet) ic.getServlet();
+ RequestDispatcher rd = servlet.getServletContext().getRequestDispatcher( "/" + innerServletName + "?param=revised¶m2=new" );
assertEquals( "request URI", "/sample/" + outerServletName, request.getRequestURI() );
assertEquals( "context path attribute", "/sample", request.getContextPath() );
assertEquals( "servlet path attribute", "/" + outerServletName, request.getServletPath() );
assertNull( "path info not null attribute", request.getPathInfo() );
-// assertEquals( "query string attribute", "param=original¶m1=first", request.getQueryString() );
-
- assertEquals( "request URI attribute", "/sample/" + innerServletName, request.getAttribute( REQUEST_URI ) );
- assertEquals( "context path attribute", "/sample", request.getAttribute( CONTEXT_PATH ) );
- assertEquals( "servlet path attribute", "/" + innerServletName, request.getAttribute( SERVLET_PATH ) );
- assertNull( "path info attribute not null", request.getAttribute( PATH_INFO ) );
-// assertEquals( "query string attribute", "param=revised¶m2=new", request.getAttribute( QUERY_STRING ) );
-
- ic.popContext();
-
- assertNull( "reverted URI attribute not null", request.getAttribute( REQUEST_URI ) );
- assertNull( "context path attribute not null", request.getAttribute( CONTEXT_PATH ) );
- assertNull( "servlet path attribute not null", request.getAttribute( SERVLET_PATH ) );
- assertNull( "path info attribute not null", request.getAttribute( PATH_INFO ) );
-// assertNull( "query string attribute not null", "param=revised¶m2=new", request.getAttribute( QUERY_STRING ) );
- }
+// assertEquals( "query string attribute", "param=original¶m1=first", request.getQueryString() ); TODO make this work
+ final HttpServletResponse response = ic.getResponse();
+ ic.pushForwardRequest( rd, request, response );
- public void notestInitialAttributes() throws Exception {
-
- WebRequest request = new GetMethodWebRequest( "http://localhost/" + outerServletName );
- InvocationContext ic = _runner.newClient().newInvocation( request );
- HttpServletRequest servletRequest = ic.getRequest();
- servletRequest.setAttribute( REQUEST_URI, "r" );
- servletRequest.setAttribute( QUERY_STRING, "q" );
- servletRequest.setAttribute( SERVLET_PATH, "s" );
- Servlet servlet = ic.getServlet();
- servlet.service( ic.getRequest(), ic.getResponse() );
- WebResponse response = ic.getServletResponse();
-
- assertEquals( "Request URI", "r", servletRequest.getAttribute( REQUEST_URI ) );
- assertEquals( "Query String", "q", servletRequest.getAttribute( QUERY_STRING ) );
- assertEquals( "ServletPath", "s", servletRequest.getAttribute( SERVLET_PATH ) );
- assertNotNull( "No response received", response );
- assertEquals( "Response Code", 200, response.getResponseCode() );
- assertEquals( "content type", "text/plain", response.getContentType() );
- assertEquals( "requested resource", IncludedServlet.DESIRED_OUTPUT, response.getText() );
+ final HttpServletRequest innerRequest = ic.getRequest();
+ assertEquals( "request URI", "/sample/" + innerServletName, innerRequest.getRequestURI() );
+ assertEquals( "context path attribute", "/sample", innerRequest.getContextPath() );
+ assertEquals( "servlet path attribute", "/" + innerServletName, innerRequest.getServletPath() );
+ assertNull( "path info not null attribute", innerRequest.getPathInfo() );
+// assertEquals( "query string attribute", "param=original¶m1=first", innerRequest.getQueryString() );
+
+ ic.popRequest();
+ final HttpServletRequest restoredRequest = ic.getRequest();
+
+ assertNull( "reverted URI attribute not null", restoredRequest.getAttribute( REQUEST_URI ) );
+ assertNull( "context path attribute not null", restoredRequest.getAttribute( CONTEXT_PATH ) );
+ assertNull( "servlet path attribute not null", restoredRequest.getAttribute( SERVLET_PATH ) );
+ assertNull( "path info attribute not null", restoredRequest.getAttribute( PATH_INFO ) );
+// assertNull( "query string attribute not null", "param=revised¶m2=new", restoredRequest.getAttribute( QUERY_STRING ) );
}
-
static class RequestDispatcherServlet extends HttpServlet {
Index: WebXMLTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/servletunit/WebXMLTest.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- WebXMLTest.java 9 Oct 2002 19:15:29 -0000 1.13
+++ WebXMLTest.java 13 Feb 2003 20:25:56 -0000 1.14
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2001-2002, Russell Gold
+* Copyright (c) 2001-2003, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -27,6 +27,8 @@
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Properties;
+import java.util.List;
+import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -103,8 +105,10 @@
WebApplication app = new WebApplication( newDocument( wxs.asText() ) );
assertTrue( "Did not require authorization", app.requiresAuthorization( new URL( "http://localhost/SimpleServlet" ) ) );
assertTrue( "Should not require authorization", !app.requiresAuthorization( new URL( "http://localhost/FreeServlet" ) ) );
- assertTrue( "Should have access", app.roleMayAccess( "supervisor", new URL( "http://localhost/SimpleServlet" ) ) );
- assertTrue( "Should not have access", !app.roleMayAccess( "peon", new URL( "http://localhost/SimpleServlet" ) ) );
+
+ List roles = Arrays.asList( app.getPermittedRoles( new URL( "http://localhost/SimpleServlet" ) ) );
+ assertTrue( "Should have access", roles.contains( "supervisor" ) );
+ assertTrue( "Should not have access", !roles.contains( "peon" ) );
}
|