From: <no...@us...> - 2003-07-16 20:11:27
|
Log Message: ----------- Added support for reading Document.cookie. Modified Files: -------------- /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit: FakeWebConnection.java /cvsroot/htmlunit/htmlunit/src/xdocs: changes.xml /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host: DocumentTest.java /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit: WebConnection.java HttpWebConnection.java WebClient.java /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host: Document.java Revision Data ------------- Index: FakeWebConnection.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/FakeWebConnection.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- FakeWebConnection.java 16 Jul 2003 13:33:17 -0000 1.4 +++ FakeWebConnection.java 16 Jul 2003 20:11:20 -0000 1.5 @@ -46,6 +46,8 @@ import java.util.List; import java.util.Map; +import org.apache.commons.httpclient.HttpState; + /** * A fake HttpProcessor for testing purposes only * @@ -97,7 +99,7 @@ private SubmitMethod lastMethod_; private List lastParameters_; - + private HttpState httpState_ = new HttpState(); /** * Create an instance @@ -246,6 +248,16 @@ */ public void setContent( final String content ) { setDefaultResponse(content, 200, "OK", "text/html"); + } + + + /** + * Return the {@link HttpState} that is being used for a given domain + * @param url The url from which the domain will be determined + * @return The state or null if no state can be found for this domain. + */ + public HttpState getStateForUrl( final URL url ) { + return httpState_; } } Index: changes.xml =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/xdocs/changes.xml,v retrieving revision 1.113 retrieving revision 1.114 diff -u -d -r1.113 -r1.114 --- changes.xml 4 Jul 2003 15:21:02 -0000 1.113 +++ changes.xml 16 Jul 2003 20:11:20 -0000 1.114 @@ -130,6 +130,9 @@ <action type="new" dev="mbowler"> Added support for the property Window.name </action> + <action type="new" dev="mbowler"> + Added read-only support for Document.cookie. + </action> </release> </body> Index: DocumentTest.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/DocumentTest.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- DocumentTest.java 16 Jul 2003 13:34:40 -0000 1.14 +++ DocumentTest.java 16 Jul 2003 20:11:20 -0000 1.15 @@ -51,6 +51,9 @@ import java.util.Collections; import java.util.List; +import org.apache.commons.httpclient.Cookie; +import org.apache.commons.httpclient.HttpState; + /** * Tests for Document * @@ -1030,12 +1033,15 @@ + "</script></head><body onload='doTest()'>" + "</body></html>"; - final List headers = Collections.singletonList( - new KeyValuePair("Set-Cookie", "one=two;three=four") ); + final URL url = new URL("http://first"); webConnection.setResponse( - new URL("http://first"), firstContent, 200, "OK", "text/html", headers ); + url, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); webClient.setWebConnection( webConnection ); + final HttpState state = webConnection.getStateForUrl(url); + state.addCookie( new Cookie("first", "one", "two") ); + state.addCookie( new Cookie("first", "three", "four") ); + final List collectedAlerts = new ArrayList(); webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); Index: WebConnection.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebConnection.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- WebConnection.java 10 Jun 2003 11:56:56 -0000 1.6 +++ WebConnection.java 16 Jul 2003 20:11:21 -0000 1.7 @@ -42,6 +42,8 @@ import java.util.List; import java.util.Map; +import org.apache.commons.httpclient.HttpState; + /** * An object that handles the actual communication portion of page * retrieval/submission <p /> @@ -127,5 +129,13 @@ public final int getProxyPort() { return proxyPort_; } + + + /** + * Return the {@link HttpState} that is being used for a given domain + * @param url The url from which the domain will be determined + * @return The state or null if no state can be found for this domain. + */ + public abstract HttpState getStateForUrl( final URL url ); } Index: HttpWebConnection.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- HttpWebConnection.java 28 Jun 2003 17:24:28 -0000 1.11 +++ HttpWebConnection.java 16 Jul 2003 20:11:21 -0000 1.12 @@ -264,7 +264,6 @@ HttpClient client = ( HttpClient )httpClients_.get( key ); if( client == null ) { client = new HttpClient(); - client.getState().setCookiePolicy( CookiePolicy.COMPATIBILITY ); // Disable informational messages from httpclient final Log log = LogFactory.getLog("httpclient.wire"); @@ -273,17 +272,17 @@ } // Disable certificate caching within HttpClient - client.setState( - new HttpState() { - public void setCredentials( - final String realm, final Credentials credentials ) { - } - + final HttpState httpState = new HttpState() { + public void setCredentials( + final String realm, final Credentials credentials ) { + } - public Credentials getCredentials( final String realm ) { - return null; - } - } ); + public Credentials getCredentials( final String realm ) { + return null; + } + }; + client.setState(httpState); + httpState.setCookiePolicy( CookiePolicy.COMPATIBILITY ); final HostConfiguration hostConfiguration = new HostConfiguration(); final URI uri; @@ -302,7 +301,7 @@ // If two clients are part of the same domain then they should share the same // state (ie cookies) - final HttpState sharedState = getStateForDomain( url.getHost() ); + final HttpState sharedState = getStateForUrl( url ); if( sharedState != null ) { client.setState(sharedState); } @@ -312,7 +311,13 @@ } - private synchronized HttpState getStateForDomain( final String domain ) { + /** + * Return the {@link HttpState} that is being used for a given domain + * @param url The url from which the domain will be determined + * @return The state or null if no state can be found for this domain. + */ + public synchronized HttpState getStateForUrl( final URL url ) { + final String domain = url.getHost(); int index = domain.lastIndexOf('.'); if( index != -1 ) { index = domain.lastIndexOf(".", index-1); Index: WebClient.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebClient.java,v retrieving revision 1.44 retrieving revision 1.45 diff -u -d -r1.44 -r1.45 --- WebClient.java 10 Jun 2003 11:56:56 -0000 1.44 +++ WebClient.java 16 Jul 2003 20:11:21 -0000 1.45 @@ -56,6 +56,7 @@ import java.util.Map; import java.util.Set; import java.util.StringTokenizer; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -167,11 +168,11 @@ /** - * Return the object that will resolve all url requests - * - * @return The UrlResolver + * <p>Return the object that will resolve all url requests<p> + * <p>INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK</p> + * @return The connection that will be used. */ - private WebConnection getWebConnection() { + public synchronized WebConnection getWebConnection() { if( webConnection_ == null ) { if( proxyHost_ == null ) { webConnection_ = new HttpWebConnection( this ); Index: Document.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/Document.java,v retrieving revision 1.22 retrieving revision 1.23 diff -u -d -r1.22 -r1.23 --- Document.java 16 Jul 2003 13:34:41 -0000 1.22 +++ Document.java 16 Jul 2003 20:11:21 -0000 1.23 @@ -38,17 +38,22 @@ package com.gargoylesoftware.htmlunit.javascript.host; import com.gargoylesoftware.htmlunit.ElementNotFoundException; -import com.gargoylesoftware.htmlunit.WebResponse; +import com.gargoylesoftware.htmlunit.WebConnection; import com.gargoylesoftware.htmlunit.WebWindow; import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.javascript.DocumentAllArray; + +import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.ListIterator; + +import org.apache.commons.httpclient.Cookie; +import org.apache.commons.httpclient.HttpState; import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; import org.mozilla.javascript.NativeArray; @@ -174,14 +179,31 @@ } + private HttpState getHttpState() { + final HtmlPage htmlPage = getHtmlPage(); + final WebConnection connection = htmlPage.getWebClient().getWebConnection(); + final URL url = htmlPage.getWebResponse().getUrl(); + + return connection.getStateForUrl( url ); + } /** * Return the cookie attribute. Currently hardcoded to return an empty string * @return The cookie attribute */ public String jsGet_cookie() { - getLog().debug("Document.cookie not supported: returning empty string"); - final WebResponse webResponse = ((HtmlPage)getHtmlElementOrDie()).getWebResponse(); - return webResponse.getResponseHeaderValue("Set-Cookie"); + final HttpState state = getHttpState(); + final Cookie[] cookies = state.getCookies(); + final StringBuffer buffer = new StringBuffer(); + + for( int i=0; i<cookies.length; i++ ) { + if( i != 0 ) { + buffer.append(";"); + } + buffer.append( cookies[i].getName() ); + buffer.append( "=" ); + buffer.append( cookies[i].getValue() ); + } + return buffer.toString(); } |