Thread: [Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit WebClient.java,1.27,1.28 WebForm.java,1.5
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-08-23 19:33:16
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv15716/src/com/meterware/httpunit Modified Files: WebClient.java WebForm.java WebLink.java WebRequest.java WebRequestSource.java WebResponse.java Log Message: Added support for javascript: URLs Index: WebClient.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebClient.java,v retrieving revision 1.27 retrieving revision 1.28 diff -u -r1.27 -r1.28 --- WebClient.java 21 Aug 2002 19:06:27 -0000 1.27 +++ WebClient.java 23 Aug 2002 19:33:12 -0000 1.28 @@ -24,14 +24,11 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLStreamHandler; +import java.net.URLConnection; -import java.util.Dictionary; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Vector; -import java.util.Iterator; -import java.util.List; -import java.util.ArrayList; +import java.util.*; import org.xml.sax.SAXException; @@ -80,16 +77,39 @@ * @exception SAXException thrown if there is an error parsing the retrieved page **/ public WebResponse getResponse( WebRequest request ) throws MalformedURLException, IOException, SAXException { - if (request.getURLString().startsWith( "about:" )) return WebResponse.BLANK_RESPONSE; - tellListeners( request ); - WebResponse response = newResponse( request ); - tellListeners( response ); - updateClient( response ); + + WebResponse response = getNextPage( request ); + if (response != null) { + tellListeners( response ); + updateClient( response ); + } + return getFrameContents( request.getTarget() ); } + /** + * Evaluates the request and returns the desired page. Will return null if the current target is to be unaffected. + */ + private WebResponse getNextPage( WebRequest request ) throws IOException { + String urlString = request.getURLString().trim(); + if (urlString.startsWith( "about:" )) { + return WebResponse.BLANK_RESPONSE; + } else if (!urlString.startsWith( "javascript:" )) { + return newResponse( request ); + } else { + WebRequestSource wrs = request.getWebRequestSource(); + String result = (wrs == null) ? getCurrentPage().getScriptableObject().evaluateURL( urlString ) + : wrs.getScriptableDelegate().evaluateURL( urlString ); + if (result == null) return null; + + URL url = new URL( "javascript", null, -1, urlString.substring( "javascript:".length() ), JAVASCRIPT_STREAM_HANDLER ); + return new DefaultWebResponse( this, request.getTarget(), url, result ); + } + } + + private void tellListeners( WebRequest request ) { List listeners; @@ -255,6 +275,23 @@ /** + * Returns the next javascript alert without removing it from the queue. + */ + public String getNextAlert() { + return _alerts.isEmpty() ? null : (String) _alerts.getFirst(); + } + + + /** + * Returns the next javascript alert and removes it from the queue. + */ + public String popNextAlert() { + if (_alerts.isEmpty()) throw new IllegalStateException( "Tried to pop a non-existent alert" ); + return (String) _alerts.removeFirst(); + } + + + /** * Specifies the object which will respond to all dialogs. **/ public void setDialogResponder( DialogResponder responder ) { @@ -269,6 +306,12 @@ if (HttpUnitOptions.isAcceptGzip()) { setHeaderField( "Accept-Encoding", "gzip" ); } + + try { + _frameContents.updateFrames( new DefaultWebResponse( this, null, WebResponse.BLANK_HTML ) ); + } catch (IOException e) { + } catch (SAXException e) { + } } @@ -354,8 +397,14 @@ } + void postAlert( String message ) { + _alerts.addLast( message ); + } + //------------------------------------------ private members ------------------------------------- + /** The list of alerts generated by JavaScript. **/ + private LinkedList _alerts = new LinkedList(); /** The currently defined cookies. **/ private Hashtable _cookies = new Hashtable(); @@ -374,6 +423,8 @@ private DialogResponder _dialogResponder = new DialogAdapter(); + private static URLStreamHandler JAVASCRIPT_STREAM_HANDLER = new JavascriptURLStreamHandler(); + /** * Examines the headers in the response and throws an exception if appropriate. @@ -482,6 +533,19 @@ **/ public String getMethod() { return "GET"; + } +} + + + + +//================================================================================================== + + +class JavascriptURLStreamHandler extends URLStreamHandler { + + protected URLConnection openConnection( URL u ) throws IOException { + return null; } } Index: WebForm.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebForm.java,v retrieving revision 1.59 retrieving revision 1.60 diff -u -r1.59 -r1.60 --- WebForm.java 21 Aug 2002 03:45:01 -0000 1.59 +++ WebForm.java 23 Aug 2002 19:33:12 -0000 1.60 @@ -334,6 +334,7 @@ return _scriptable; } + //---------------------------------- WebRequestSource methods -------------------------------- /** @@ -375,6 +376,15 @@ **/ public WebRequest getRequest() { return getRequest( (SubmitButton) null ); + } + + + /** + * Returns the scriptable delegate. + */ + + ScriptableDelegate getScriptableDelegate() { + return getScriptableObject(); } Index: WebLink.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebLink.java,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- WebLink.java 8 Aug 2002 20:47:21 -0000 1.24 +++ WebLink.java 23 Aug 2002 19:33:13 -0000 1.25 @@ -96,15 +96,6 @@ } - /** - * Returns an object which provides scripting access to this link. - **/ - public Scriptable getScriptableObject() { - if (_scriptable == null) _scriptable = new Scriptable(); - return _scriptable; - } - - //----------------------------------------- WebRequestSource methods --------------------------------------------------- @@ -117,6 +108,14 @@ /** + * Returns the scriptable delegate. + */ + ScriptableDelegate getScriptableDelegate() { + return getScriptableObject(); + } + + + /** * Returns an array containing the names of any parameters defined as part of this link's URL. **/ public String[] getParameterNames() { @@ -262,6 +261,15 @@ **/ WebLink( WebResponse response, URL baseURL, String parentTarget, Node node ) { super( response, node, baseURL, NodeUtils.getNodeAttribute( node, "href" ), parentTarget ); + } + + + /** + * Returns an object which provides scripting access to this link. + **/ + Scriptable getScriptableObject() { + if (_scriptable == null) _scriptable = new Scriptable(); + return _scriptable; } Index: WebRequest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequest.java,v retrieving revision 1.44 retrieving revision 1.45 diff -u -r1.44 -r1.45 --- WebRequest.java 16 Aug 2002 17:24:00 -0000 1.44 +++ WebRequest.java 23 Aug 2002 19:33:13 -0000 1.45 @@ -288,6 +288,7 @@ protected WebRequest( WebRequestSource requestSource ) { this( requestSource.getBaseURL(), requestSource.getRelativeURL(), requestSource.getTarget(), newParameterHolder( requestSource ) ); + _webRequestSource = requestSource; setHeaderField( "Referer", requestSource.getBaseURL().toExternalForm() ); } @@ -306,7 +307,7 @@ **/ private WebRequest( URL urlBase, String urlString, String target, ParameterHolder parameterHolder ) { _urlBase = urlBase; - _urlString = escape( urlString ); + _urlString = urlString.toLowerCase().startsWith( "http" ) ? escape( urlString ) : urlString; _target = target; _parameterHolder = parameterHolder; } @@ -421,6 +422,11 @@ final static String TOP_FRAME = "_top"; + WebRequestSource getWebRequestSource() { + return _webRequestSource; + } + + Hashtable getHeaderDictionary() { if (_headers == null) { _headers = new Hashtable(); @@ -448,6 +454,7 @@ private String _urlString; private String _target = TOP_FRAME; private Hashtable _headers; + private WebRequestSource _webRequestSource; private boolean _httpsProtocolSupportEnabled; Index: WebRequestSource.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequestSource.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- WebRequestSource.java 13 Aug 2002 17:57:47 -0000 1.12 +++ WebRequestSource.java 23 Aug 2002 19:33:13 -0000 1.13 @@ -20,6 +20,8 @@ *******************************************************************************************************************/ package com.meterware.httpunit; +import com.meterware.httpunit.scripting.ScriptableDelegate; + import org.w3c.dom.Node; import org.xml.sax.SAXException; @@ -87,6 +89,13 @@ **/ abstract public String[] getParameterValues( String name ); + + + /** + * Returns the scriptable delegate. + */ + abstract + ScriptableDelegate getScriptableDelegate(); /** Index: WebResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v retrieving revision 1.76 retrieving revision 1.77 diff -u -r1.76 -r1.77 --- WebResponse.java 22 Aug 2002 16:19:54 -0000 1.76 +++ WebResponse.java 23 Aug 2002 19:33:13 -0000 1.77 @@ -475,27 +475,27 @@ //---------------------------------------- JavaScript methods ---------------------------------------- - private LinkedList _alerts = new LinkedList(); - /** * Returns the next javascript alert without removing it from the queue. + * @deprecated use WebClient#getNextAlert. Will be removed in next release. */ public String getNextAlert() { - return _alerts.isEmpty() ? null : (String) _alerts.getFirst(); + return _client.getNextAlert(); } /** * Returns the next javascript alert and removes it from the queue. + * @deprecated use WebClient#popNextAlert. Will be removed in next release. */ public String popNextAlert() { - if (_alerts.isEmpty()) throw new IllegalStateException( "Tried to pop a non-existent alert" ); - return (String) _alerts.removeFirst(); + return _client.popNextAlert(); } public Scriptable getScriptableObject() { - return new Scriptable(); + if (_scriptable == null) _scriptable = new Scriptable(); + return _scriptable; } @@ -511,7 +511,7 @@ public class Scriptable extends ScriptableDelegate { public void alert( String message ) { - _alerts.addLast( message ); + _client.postAlert( message ); } @@ -612,8 +612,8 @@ //------------------------------------------ package members ------------------------------------------------ - - final static WebResponse BLANK_RESPONSE = new DefaultWebResponse( "<html><head></head><body></body></html>" ); + final static String BLANK_HTML = "<html><head><title>Blank</title></head><body></body></html>"; + final static WebResponse BLANK_RESPONSE = new DefaultWebResponse( BLANK_HTML ); /** @@ -698,6 +698,8 @@ private WebClient _client; + private Scriptable _scriptable; + protected void loadResponseText() throws IOException { if (_responseText != null) throw new IllegalStateException( "May only invoke loadResponseText once" ); @@ -1087,7 +1089,17 @@ DefaultWebResponse( String text ) { - super( null, "", null ); + this( null, null, text ); + } + + + DefaultWebResponse( WebClient client, URL url, String text ) { + this( client, WebRequest.TOP_FRAME, url, text ); + } + + + DefaultWebResponse( WebClient client, String target, URL url, String text ) { + super( client, target, url ); _responseText = text; } |