[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit NodeUtils.java,1.4,1.5 WebConversation.ja
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2000-10-04 15:45:51
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory slayer.i.sourceforge.net:/tmp/cvs-serv23587/src/com/meterware/httpunit Modified Files: NodeUtils.java WebConversation.java WebResponse.java Log Message: Added frame support Index: NodeUtils.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/NodeUtils.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- NodeUtils.java 2000/10/02 16:32:10 1.4 +++ NodeUtils.java 2000/10/04 15:45:47 1.5 @@ -43,17 +43,24 @@ public static int getAttributeValue( Node node, String attributeName, int defaultValue ) { NamedNodeMap nnm = node.getAttributes(); - Node span = nnm.getNamedItem( attributeName ); - if (span == null) { + Node attribute = nnm.getNamedItem( attributeName ); + if (attribute == null) { return defaultValue; } else try { - return Integer.parseInt( span.getNodeValue() ); + return Integer.parseInt( attribute.getNodeValue() ); } catch (NumberFormatException e) { return defaultValue; } } + public static String getNodeAttribute( Node node, String attributeName ) { + NamedNodeMap nnm = node.getAttributes(); + Node attribute = nnm.getNamedItem( attributeName ); + return (attribute == null) ? "" : attribute.getNodeValue(); + } + + /** * Converts the DOM trees rooted at the specified nodes to text, ignoring * any HTML tags. @@ -91,13 +98,6 @@ } } return sb.toString(); - } - - - private static String getNodeAttribute( Node node, String attributeName ) { - NamedNodeMap nnm = node.getAttributes(); - Node attribute = nnm.getNamedItem( attributeName ); - return (attribute == null) ? "" : attribute.getNodeValue(); } Index: WebConversation.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebConversation.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- WebConversation.java 2000/10/02 16:32:10 1.11 +++ WebConversation.java 2000/10/04 15:45:47 1.12 @@ -43,10 +43,36 @@ public WebConversation() { } + + /** + * Returns the name of the currently active frames. + **/ + public String[] getFrameNames() { + Vector names = new Vector(); + for (Enumeration e = _frameContents.keys(); e.hasMoreElements();) { + names.addElement( e.nextElement() ); + } + + String[] result = new String[ names.size() ]; + names.copyInto( result ); + return result; + } + + /** + * Returns the response associated with the specified frame name. + **/ + public WebResponse getFrameContents( String frameName ) { + WebResponse response = (WebResponse) _frameContents.get( frameName ); + if (response == null) throw new NoSuchFrameException( frameName ); + return response; + } + + + /** * Submits a GET method request and returns a response. **/ - public WebResponse getResponse( String urlString ) throws MalformedURLException, IOException { + public WebResponse getResponse( String urlString ) throws MalformedURLException, IOException, SAXException { return getResponse( new GetMethodWebRequest( urlString ) ); } @@ -55,7 +81,7 @@ * 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 { + public WebResponse getResponse( WebRequest request ) throws MalformedURLException, IOException, SAXException { HttpURLConnection connection = (HttpURLConnection) openConnection( request.getURL() ); request.completeRequest( connection ); updateCookies( connection ); @@ -70,11 +96,27 @@ throw new HttpNotFoundException( request.getURLString() ); } else { WebResponse result = new WebResponse( this, request.getTarget(), request.getURL(), connection ); + removeSubFrames( request.getTarget() ); + _frameContents.put( request.getTarget(), result ); + _subFrames.put( request.getTarget(), result.getFrameNames() ); + WebRequest[] requests = result.getFrameRequests(); + for (int i = 0; i < requests.length; i++) getResponse( requests[i] ); return result; } } + private void removeSubFrames( String targetName ) { + String[] names = (String[]) _subFrames.get( targetName ); + if (names == null) return; + for (int i = 0; i < names.length; i++) { + removeSubFrames( names[i] ); + _frameContents.remove( names[i] ); + _subFrames.remove( names[i] ); + } + } + + /** * Defines a cookie to be sent to the server on every request. **/ @@ -141,6 +183,14 @@ /** The authorization header value. **/ private String _authorization; + + /** A map of frame names to current contents. **/ + private Hashtable _frameContents = new Hashtable(); + + + /** A map of frame names to frames nested within them. **/ + private Hashtable _subFrames = new Hashtable(); + static { HttpURLConnection.setFollowRedirects( false ); @@ -202,4 +252,21 @@ super( baseRequest, relativeURL ); } +} + + + +class NoSuchFrameException extends RuntimeException { + + NoSuchFrameException( String frameName ) { + _frameName = frameName; + } + + + public String getMessage() { + return "No frame named " + _frameName + " is currently active"; + } + + + private String _frameName; } Index: WebResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- WebResponse.java 2000/10/02 16:32:10 1.15 +++ WebResponse.java 2000/10/04 15:45:47 1.16 @@ -79,7 +79,7 @@ NodeList frames = NodeUtils.getElementsByTagName( getReceivedPage().getDOM(), "frame" ); String[] result = new String[ frames.getLength() ]; for (int i = 0; i < result.length; i++) { - result[i] = getValue( frames.item(i).getAttributes().getNamedItem( "name" ) ); + result[i] = NodeUtils.getNodeAttribute( frames.item(i), "name" ); } return result; @@ -89,19 +89,16 @@ /** * Returns the frames found in the page in the order in which they appear. **/ - public WebFrame[] getFrames() throws SAXException { + public WebRequest[] getFrameRequests() throws SAXException { NodeList frames = NodeUtils.getElementsByTagName( getReceivedPage().getDOM(), "frame" ); - WebFrame[] result = new WebFrame[ frames.getLength() ]; + WebRequest[] result = new WebRequest[ frames.getLength() ]; for (int i = 0; i < result.length; i++) { - result[i] = new WebFrame( frames.item(i) ); + result[i] = new GetMethodWebRequest( this.getURL(), + NodeUtils.getNodeAttribute( frames.item(i), "src" ), + NodeUtils.getNodeAttribute( frames.item(i), "name" ) ); } return result; - } - - - private String getValue( Node node ) { - return node == null ? "" : node.getNodeValue(); } |