[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit InvocationContextFactory.java,1.3,1.4
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-10-17 23:50:54
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit
In directory usw-pr-cvs1:/tmp/cvs-serv3856/src/com/meterware/servletunit
Modified Files:
InvocationContextFactory.java InvocationContextImpl.java
ServletRunner.java ServletUnitClient.java
Log Message:
Added support for associating cookies with servers
Index: InvocationContextFactory.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/InvocationContextFactory.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- InvocationContextFactory.java 1 Aug 2002 14:58:59 -0000 1.3
+++ InvocationContextFactory.java 17 Oct 2002 23:50:49 -0000 1.4
@@ -25,19 +25,17 @@
import java.net.MalformedURLException;
import java.util.Dictionary;
-import javax.servlet.http.Cookie;
-
/**
* An interface for an object which acts as a factory of InvocationContexts
*
- * @author <a href="mailto:rus...@ac...">Russell Gold</a>
+ * @author <a href="mailto:rus...@ht...">Russell Gold</a>
**/
public interface InvocationContextFactory {
/**
* Creates and returns a new invocation context to test calling of servlet methods.
**/
- public InvocationContext newInvocation( ServletUnitClient client, WebRequest request, Cookie[] clientCookies, Dictionary clientHeaders, byte[] messageBody ) throws IOException, MalformedURLException;
+ public InvocationContext newInvocation( ServletUnitClient client, WebRequest request, Dictionary clientHeaders, byte[] messageBody ) throws IOException, MalformedURLException;
}
Index: InvocationContextImpl.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/InvocationContextImpl.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- InvocationContextImpl.java 1 Aug 2002 14:58:59 -0000 1.7
+++ InvocationContextImpl.java 17 Oct 2002 23:50:49 -0000 1.8
@@ -19,19 +19,19 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
+import com.meterware.httpunit.HttpException;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
-import com.meterware.httpunit.Base64;
-import com.meterware.httpunit.HttpException;
+import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
-import java.io.IOException;
import java.util.Dictionary;
+import java.util.StringTokenizer;
+import java.util.Vector;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
-
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -130,7 +130,7 @@
* Constructs a servlet invocation context for a specified servlet container,
* request, and cookie headers.
**/
- InvocationContextImpl( ServletUnitClient client, ServletRunner runner, WebRequest request, Cookie[] cookies, Dictionary clientHeaders, byte[] messageBody ) throws IOException, MalformedURLException {
+ InvocationContextImpl( ServletUnitClient client, ServletRunner runner, WebRequest request, Dictionary clientHeaders, byte[] messageBody ) throws IOException, MalformedURLException {
_client = client;
_application = runner.getApplication();
_requestURL = request.getURL();
@@ -138,6 +138,7 @@
_request = new ServletUnitHttpRequest( _application.getServletRequest( _requestURL ), request, runner.getContext(),
clientHeaders, messageBody );
+ Cookie[] cookies = getCookies( clientHeaders );
for (int i = 0; i < cookies.length; i++) _request.addCookie( cookies[i] );
if (_application.usesBasicAuthentication()) _request.readBasicAuthentication();
@@ -151,6 +152,9 @@
//------------------------------ private members ---------------------------------------
+ final private static Cookie[] NO_COOKIES = new Cookie[0];
+
+
private ServletUnitClient _client;
private WebApplication _application;
@@ -161,6 +165,25 @@
private Servlet _servlet;
private WebResponse _webResponse;
+
+
+ private Cookie[] getCookies( Dictionary clientHeaders ) {
+ String cookieHeader = (String) clientHeaders.get( "Cookie" );
+ if (cookieHeader == null) return NO_COOKIES;
+ Vector cookies = new Vector();
+
+ StringTokenizer st = new StringTokenizer( cookieHeader, "=;" );
+ while (st.hasMoreTokens()) {
+ String name = st.nextToken();
+ if (st.hasMoreTokens()) {
+ String value = st.nextToken();
+ cookies.addElement( new Cookie( name, value ) );
+ }
+ }
+ Cookie[] results = new Cookie[ cookies.size() ];
+ cookies.copyInto( results );
+ return results;
+ }
}
Index: ServletRunner.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletRunner.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- ServletRunner.java 1 Aug 2002 14:58:59 -0000 1.18
+++ ServletRunner.java 17 Oct 2002 23:50:49 -0000 1.19
@@ -185,8 +185,8 @@
private ServletUnitContext _context;
private InvocationContextFactory _factory = new InvocationContextFactory() {
- public InvocationContext newInvocation( ServletUnitClient client, WebRequest request, Cookie[] clientCookies, Dictionary clientHeaders, byte[] messageBody ) throws IOException, MalformedURLException {
- return new InvocationContextImpl( client, ServletRunner.this, request, clientCookies, clientHeaders, messageBody );
+ public InvocationContext newInvocation( ServletUnitClient client, WebRequest request, Dictionary clientHeaders, byte[] messageBody ) throws IOException, MalformedURLException {
+ return new InvocationContextImpl( client, ServletRunner.this, request, clientHeaders, messageBody );
}
};
Index: ServletUnitClient.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitClient.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- ServletUnitClient.java 4 Oct 2002 00:40:28 -0000 1.9
+++ ServletUnitClient.java 17 Oct 2002 23:50:50 -0000 1.10
@@ -21,20 +21,12 @@
*******************************************************************************************************************/
import com.meterware.httpunit.*;
-import java.io.IOException;
import java.io.ByteArrayOutputStream;
-
+import java.io.IOException;
import java.net.MalformedURLException;
-import java.net.URL;
-
-import java.util.StringTokenizer;
-import java.util.Vector;
-import java.util.Hashtable;
import javax.servlet.ServletException;
-import javax.servlet.http.Cookie;
-
import org.xml.sax.SAXException;
/**
@@ -72,7 +64,7 @@
public InvocationContext newInvocation( WebRequest request ) throws IOException, MalformedURLException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writeMessageBody( request, baos );
- return _invocationContextFactory.newInvocation( this, request, getCookies(), this.getHeaderFields(), baos.toByteArray() );
+ return _invocationContextFactory.newInvocation( this, request, getHeaderFields( request.getURL() ), baos.toByteArray() );
}
@@ -111,33 +103,11 @@
private InvocationContextFactory _invocationContextFactory;
- final private static Cookie[] NO_COOKIES = new Cookie[0];
-
-
//--------------------------------- package methods ---------------------------------------
private ServletUnitClient( InvocationContextFactory factory ) {
_invocationContextFactory = factory;
- }
-
-
- private Cookie[] getCookies() {
- String cookieHeader = (String) getHeaderFields().get( "Cookie" );
- if (cookieHeader == null) return NO_COOKIES;
- Vector cookies = new Vector();
-
- StringTokenizer st = new StringTokenizer( cookieHeader, "=;" );
- while (st.hasMoreTokens()) {
- String name = st.nextToken();
- if (st.hasMoreTokens()) {
- String value = st.nextToken();
- cookies.addElement( new Cookie( name, value ) );
- }
- }
- Cookie[] results = new Cookie[ cookies.size() ];
- cookies.copyInto( results );
- return results;
}
}
|