vmassol 2002/09/26 14:27:50
Modified: framework/src/java/share/org/apache/cactus/server
AbstractWebTestCaller.java
AbstractWebTestController.java
framework/src/java/share/org/apache/cactus WebRequest.java
ServiceEnumeration.java
documentation/docs/xdocs todo.xml changes.xml
sample-servlet/src/unit/share/org/apache/cactus/unit
TestServletTestCase2.java
Added: framework/src/java/share/org/apache/cactus
HttpSessionCookie.java
Log:
Added new method: WebRequest.getSessionCookie() which connects to the server side
and retrieves the jsessionid cookie.
Revision Changes Path
1.9 +30 -1
jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractWebTestCaller.java
Index: AbstractWebTestCaller.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractWebTestCaller.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- AbstractWebTestCaller.java 28 Aug 2002 19:53:52 -0000 1.8
+++ AbstractWebTestCaller.java 26 Sep 2002 21:27:49 -0000 1.9
@@ -65,6 +65,7 @@
import org.apache.cactus.AbstractTestCase;
import org.apache.cactus.HttpServiceDefinition;
+import org.apache.cactus.ServiceEnumeration;
import org.apache.cactus.WebTestResult;
import org.apache.cactus.util.ClassLoaderUtils;
import org.apache.commons.logging.Log;
@@ -219,6 +220,34 @@
// Do not return any http response (not needed). It is enough to
// know this point has been reached ... it means the connection has
// been established !
+ }
+
+ /**
+ * Create an HTTP Session and returns the response that contains the
+ * HTTP session as a cookie (unless URL rewriting is used in which
+ * case the jsesssionid cookie is not returned).
+ *
+ * @exception ServletException if an unexpected error occurred
+ */
+ public void doCreateSession() throws ServletException
+ {
+ // Create an HTTP session
+ this.webImplicitObjects.getHttpServletRequest().getSession(true);
+
+ try
+ {
+ Writer writer = getResponseWriter();
+ writer.close();
+ }
+ catch (IOException e)
+ {
+ String message = "Error writing HTTP response back to client "
+ + "for service [" + ServiceEnumeration.CREATE_SESSION_SERVICE
+ + "]";
+
+ LOGGER.error(message, e);
+ throw new ServletException(message, e);
+ }
}
/**
1.7 +10 -1
jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractWebTestController.java
Index: AbstractWebTestController.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractWebTestController.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- AbstractWebTestController.java 26 Sep 2002 20:15:49 -0000 1.6
+++ AbstractWebTestController.java 26 Sep 2002 21:27:49 -0000 1.7
@@ -119,6 +119,8 @@
AbstractWebTestCaller caller = getTestCaller(webImplicitObjects);
+ // FIXME: will need a factory here real soon...
+
// Is it the call test method service ?
if (ServiceEnumeration.CALL_TEST_SERVICE.equals(serviceName))
{
@@ -137,6 +139,13 @@
else if (ServiceEnumeration.RUN_TEST_SERVICE.equals(serviceName))
{
caller.doRunTest();
+ }
+
+ // Is it the service to create an HTTP session?
+ else if (ServiceEnumeration.CREATE_SESSION_SERVICE.equals(
+ serviceName))
+ {
+ caller.doCreateSession();
}
else
{
1.8 +71 -1
jakarta-cactus/framework/src/java/share/org/apache/cactus/WebRequest.java
Index: WebRequest.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/WebRequest.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- WebRequest.java 26 Sep 2002 16:43:32 -0000 1.7
+++ WebRequest.java 26 Sep 2002 21:27:49 -0000 1.8
@@ -57,12 +57,17 @@
package org.apache.cactus;
import java.io.InputStream;
+import java.net.HttpURLConnection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
+import org.apache.cactus.client.ClientException;
+import org.apache.cactus.client.ConnectionHelper;
+import org.apache.cactus.client.ConnectionHelperFactory;
+import org.apache.cactus.client.WebResponseObjectFactory;
import org.apache.cactus.client.authentication.AbstractAuthentication;
import org.apache.cactus.util.ChainedRuntimeException;
import org.apache.cactus.util.WebConfiguration;
@@ -851,4 +856,69 @@
return buffer.toString();
}
+
+ /**
+ * Gets an HTTP session id by calling the server side and retrieving
+ * the jsessionid cookie in the HTTP response. This is achieved by
+ * calling the Cactus redirector used by the current test case.
+ *
+ * @return the HTTP session id as a <code>HttpSessionCookie</code> object
+ */
+ public HttpSessionCookie getSessionCookie()
+ {
+ ConnectionHelper helper = ConnectionHelperFactory.getConnectionHelper(
+ ((WebConfiguration) getConfiguration()).getRedirectorURL(),
+ getConfiguration());
+
+ WebRequest request = new WebRequest(getConfiguration());
+ request.addParameter(HttpServiceDefinition.SERVICE_NAME_PARAM,
+ ServiceEnumeration.CREATE_SESSION_SERVICE.toString(),
+ WebRequest.GET_METHOD);
+
+ HttpURLConnection resultConnection;
+ try
+ {
+ resultConnection = helper.connect(request);
+ }
+ catch (Throwable e)
+ {
+ throw new ChainedRuntimeException("Failed to connect to ["
+ + ((WebConfiguration) getConfiguration()).getRedirectorURL()
+ + "]", e);
+ }
+
+ WebResponse response;
+ try
+ {
+ response = (WebResponse) new WebResponseObjectFactory().
+ getResponseObject(WebResponse.class.getName(), request,
+ resultConnection);
+ }
+ catch (ClientException e)
+ {
+ throw new ChainedRuntimeException("Failed to connect to ["
+ + ((WebConfiguration) getConfiguration()).getRedirectorURL()
+ + "]", e);
+ }
+
+ Cookie cookie = response.getCookieIgnoreCase("jsessionid");
+
+ // FIXME: Add a constructor to the Cookie class that takes a Cookie
+ // as parameter.
+
+ HttpSessionCookie sessionCookie = null;
+
+ if (cookie != null)
+ {
+ sessionCookie = new HttpSessionCookie(cookie.getDomain(),
+ cookie.getName(), cookie.getValue());
+ sessionCookie.setComment(cookie.getComment());
+ sessionCookie.setExpiryDate(cookie.getExpiryDate());
+ sessionCookie.setPath(cookie.getPath());
+ sessionCookie.setSecure(cookie.isSecure());
+ }
+
+ return sessionCookie;
+ }
+
}
1.4 +8 -1
jakarta-cactus/framework/src/java/share/org/apache/cactus/ServiceEnumeration.java
Index: ServiceEnumeration.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/ServiceEnumeration.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ServiceEnumeration.java 26 Sep 2002 20:12:04 -0000 1.3
+++ ServiceEnumeration.java 26 Sep 2002 21:27:49 -0000 1.4
@@ -85,6 +85,13 @@
new ServiceEnumeration("RUN_TEST");
/**
+ * Service used to create an HTTP session so that it is returned
+ * in a cookie.
+ */
+ public static final ServiceEnumeration CREATE_SESSION_SERVICE =
+ new ServiceEnumeration("CREATE_SESSION");
+
+ /**
* The service's name
*/
private String name;
1.1
jakarta-cactus/framework/src/java/share/org/apache/cactus/HttpSessionCookie.java
Index: HttpSessionCookie.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
* Foundation" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.cactus;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
* @version $Id: $
*/
public class HttpSessionCookie extends Cookie
{
/**
* @see Cookie#Cookie()
*/
public HttpSessionCookie(String theDomain, String theName, String theValue)
{
super(theDomain, theName, theValue);
}
}
1.29 +0 -8 jakarta-cactus/documentation/docs/xdocs/todo.xml
Index: todo.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/todo.xml,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- todo.xml 15 Sep 2002 21:17:23 -0000 1.28
+++ todo.xml 26 Sep 2002 21:27:49 -0000 1.29
@@ -116,14 +116,6 @@
"getHttpServletRequest().xxx()". Deprecate the direct access. Needs
to be discussed on cactus-dev.
</action>
- <action>
- Provide support for generating Session cookies and URL Sessions
- (URL rewriting). A description of the mechanism has been
- posted in thread "How does cactus simulate a session" on the
- Cactus user mailing list. Thanks to
- <link href="mailto:[EMAIL PROTECTED]">Kyle W.
- Willkomm</link>.
- </action>
</category>
<category title="Ideas">
1.52 +9 -0 jakarta-cactus/documentation/docs/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/changes.xml,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -r1.51 -r1.52
--- changes.xml 26 Sep 2002 13:17:15 -0000 1.51
+++ changes.xml 26 Sep 2002 21:27:49 -0000 1.52
@@ -49,6 +49,15 @@
<release version="1.5" date="- in CVS">
<action dev="VMA" type="add">
+ Ability to get a real HTTP Session cookie before the start of the
+ test. This is achieved by calling the new
+ <code>WebRequest.getSessionCookie()</code> method which returns
+ a <code>HttpSessionCookie</code> object that you then add to the
+ HTTP request. Initially suggested by
+ <link href="mailto:[EMAIL PROTECTED]">Kyle W.
+ Willkomm</link>.
+ </action>
+ <action dev="VMA" type="add">
New <code>WebResponse.getCookieIgnoreCase(cookieName)</code> to get
the first cookie matching <code>cookieName</code> whatever the case
(case-insensitive).
1.12 +25 -1
jakarta-cactus/sample-servlet/src/unit/share/org/apache/cactus/unit/TestServletTestCase2.java
Index: TestServletTestCase2.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/sample-servlet/src/unit/share/org/apache/cactus/unit/TestServletTestCase2.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- TestServletTestCase2.java 26 Sep 2002 13:18:23 -0000 1.11
+++ TestServletTestCase2.java 26 Sep 2002 21:27:50 -0000 1.12
@@ -72,6 +72,7 @@
import junit.framework.Test;
import junit.framework.TestSuite;
+import org.apache.cactus.HttpSessionCookie;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.WebResponse;
@@ -889,6 +890,29 @@
public void endVerifyJsessionid(WebResponse theResponse)
{
assertNotNull(theResponse.getCookieIgnoreCase("jsessionid"));
+ }
+
+ //-------------------------------------------------------------------------
+
+ /**
+ * Verify that Cactus can provide us with a real HTTP session cookie.
+ *
+ * @param theRequest the request object that serves to initialize the
+ * HTTP connection to the server redirector.
+ */
+ public void beginCreateSessionCookie(WebRequest theRequest)
+ {
+ HttpSessionCookie sessionCookie = theRequest.getSessionCookie();
+ theRequest.addCookie(sessionCookie);
+ }
+
+ /**
+ * Verify that Cactus can provide us with a real HTTP session cookie.
+ */
+ public void testCreateSessionCookie()
+ {
+ assertFalse("A session should have been created prior to "
+ + "this request", session.isNew());
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>