[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit WebConversation.java,1.2,1.3
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2000-08-21 21:36:24
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory slayer.i.sourceforge.net:/tmp/cvs-serv14793/src/com/meterware/httpunit Modified Files: WebConversation.java Log Message: Added addCookie, setUserAgent Index: WebConversation.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebConversation.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -r1.2 -r1.3 *** WebConversation.java 2000/08/09 17:28:40 1.2 --- WebConversation.java 2000/08/21 21:36:22 1.3 *************** *** 1 **** ! package com.meterware.httpunit; /******************************************************************************************************************** * $Id$ * * Copyright (c) 2000, 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.*; import java.net.*; import java.util.*; import org.xml.sax.*; /** * The context for a series of HTTP requests. This class manages cookies used to maintain * session context, computes relative URLs, and generally emulates the browser behavior * needed to build an automated test of a web site. **/ public class WebConversation { /** * Creates a new web conversation. **/ public WebConversation() { } /** * Submits a web request and returns a response, using all state developed so far as stored in * cookies as requested by the server. **/ public WebResponse getResponse( WebRequest request ) throws! MalformedURLException, IOException { URLConnection connection = openConnection( request.getURL() ); request.completeRequest( connection ); updateCookies( connection ); if (connection.getHeaderField( "Location" ) != null) { return getResponse( new RedirectWebRequest( request, connection.getHeaderField( "Location" ) ) ); } else { WebResponse result = new WebResponse( this, request.getURL(), connection.getInputStream() ); return result; } } //---------------------------------- private members -------------------------------- private Hashtable _cookies = new Hashtable(); static { HttpURLConnection.setFollowRedirects( false ); } private URLConnection openConnection( URL url ) throws MalformedURLException, IOException { URLConnection connection = url.openConnection(); connection.setUseCaches( false ); sendCookies( connection ); return connection; } private void sendCookies( URLConnection connection ) { if (_cookies.size() == 0) return; StringBuffer sb = new StringBuffer(); for (Enumeration e = _cookies.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); sb.append( name ).append( '=' ).append( _cookies.get( name ) ); if (e.hasMoreElements()) sb.append( ';' ); } connection.setRequestProperty( "Cookie", sb.toString() ); } private void updateCookies( URLConnection connection ) { for (int i = 1; true; i++) { String key = connection.getHeaderFieldKey( i ); if (key == null) break; if (!key.equalsIgnoreCase( "Set-Cookie" )) continue; StringTokenizer st = new StringTokenizer( connection.getHeaderField( i ), "=;" ); String name = st.nextToken(); String value = st.nextToken(); _cookies.put( name, value ); }; } } class RedirectWebRequest extends WebRequest { ! RedirectWebRequest( WebRequest baseRequest, String relativeURL ) throws MalformedURLException { super( baseRequest, relativeURL ); } public URL getURL() throws MalformedURLException { return new URL( getURLBase(), getURLString() ); } } \ No newline at end of file --- 1 ---- ! package com.meterware.httpunit; /******************************************************************************************************************** * $Id$ * * Copyright (c) 2000, 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.*; import java.net.*; import java.util.*; import org.xml.sax.*; /** * The context for a series of HTTP requests. This class manages cookies used to maintain * session context, computes relative URLs, and generally emulates the browser behavior * needed to build an automated test of a web site. * * @author Russell Gold * @author Jan Ohrstrom **/ public class WebConversation { /** * Creates a new web conversation. **/ public WebConversation() { } /** * Submits a web request and returns a response, using all state developed so far as stored in * cookies as requested by the server. **/ public ! WebResponse getResponse( WebRequest request ) throws MalformedURLException, IOException { URLConnection connection = openConnection( request.getURL() ); request.completeRequest( connection ); updateCookies( connection ); if (connection.getHeaderField( "Location" ) != null) { return getResponse( new RedirectWebRequest( request, connection.getHeaderField( "Location" ) ) ); } else { WebResponse result = new WebResponse( this, request.getURL(), connection.getInputStream() ); return result; } } /** * Defines a cookie to be sent to the server on every request. **/ public void addCookie(String name, String value) { _cookies.put( name, value ); } /** * Specifies the user agent identification. Used to trigger browser-specific server behavior. **/ public void setUserAgent(String userAgent) { _userAgent = userAgent; } /** * Returns the current user agent setting. **/ public String getUserAgent() { return _userAgent; } //---------------------------------- private members -------------------------------- /** The currently defined cookies. **/ private Hashtable _cookies = new Hashtable(); /** The current user agent. **/ private String _userAgent; static { HttpURLConnection.setFollowRedirects( false ); } private URLConnection openConnection( URL url ) throws MalformedURLException, IOException { URLConnection connection = url.openConnection(); connection.setUseCaches( false ); sendUserAgent( connection ); sendCookies( connection ); return connection; } private void sendUserAgent ( URLConnection connection ) { if (getUserAgent() == null) return; connection.setRequestProperty( "User-Agent" , getUserAgent() ); } private void sendCookies( URLConnection connection ) { if (_cookies.size() == 0) return; StringBuffer! sb = new StringBuffer(); for (Enumeration e = _cookies.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); sb.append( name ).append( '=' ).append( _cookies.get( name ) ); if (e.hasMoreElements()) sb.append( ';' ); } connection.setRequestProperty( "Cookie", sb.toString() ); } private void updateCookies( URLConnection connection ) { for (int i = 1; true; i++) { String key = connection.getHeaderFieldKey( i ); if (key == null) break; if (!key.equalsIgnoreCase( "Set-Cookie" )) continue; StringTokenizer st = new StringTokenizer( connection.getHeaderField( i ), "=;" ); String name = st.nextToken(); String value = st.nextToken(); _cookies.put( name, value ); }; } } class RedirectWebRequest extends WebRequest { RedirectWebRequest( WebRequest baseRequest, String relativeURL ) throws MalformedURLException { super( baseRequest, relativeURL ); } public URL getURL() throws MalformedURLException { return new URL( getURLBase(), getURLString() ); } } \ No newline at end of file |