Log Message:
-----------
First cut at cookie support. Only reading is supported and even then it only works if the cookie was actually sent in the same request that got the page. If the cookie had been set on an earlier request then the values won't be visible here.
Modified Files:
--------------
/cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host:
DocumentTest.java
/cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host:
Document.java
Revision Data
-------------
Index: DocumentTest.java
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/DocumentTest.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- DocumentTest.java 10 Jun 2003 11:57:01 -0000 1.13
+++ DocumentTest.java 16 Jul 2003 13:34:40 -0000 1.14
@@ -1008,4 +1008,42 @@
"a", "b"} );
assertEquals( expectedAlerts, collectedAlerts );
}
+
+
+ public void testCookie_read() throws Exception {
+ final WebClient webClient = new WebClient();
+ final FakeWebConnection webConnection = new FakeWebConnection( webClient );
+
+ final String firstContent
+ = "<html><head><title>First</title><script>"
+ + "function doTest() {\n"
+ + " var cookieSet = document.cookie.split(';');\n"
+ + " var setSize = cookieSet.length;\n"
+ + " var crumbs;\n"
+ + " var x=0;\n"
+ + " for (x=0;((x<setSize)); x++) {\n"
+ + " crumbs = cookieSet[x].split('=');\n"
+ + " alert ( crumbs[0] );\n"
+ + " alert ( crumbs[1] );\n"
+ + " } \n"
+ + "}\n"
+ + "</script></head><body onload='doTest()'>"
+ + "</body></html>";
+
+ final List headers = Collections.singletonList(
+ new KeyValuePair("Set-Cookie", "one=two;three=four") );
+ webConnection.setResponse(
+ new URL("http://first"), firstContent, 200, "OK", "text/html", headers );
+ webClient.setWebConnection( webConnection );
+
+ final List collectedAlerts = new ArrayList();
+ webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
+
+ final HtmlPage firstPage = ( HtmlPage )webClient.getPage( new URL( "http://first" ) );
+ assertEquals( "First", firstPage.getTitleText() );
+
+ final List expectedAlerts = Arrays.asList( new String[] {
+ "one", "two", "three", "four"} );
+ assertEquals( expectedAlerts, collectedAlerts );
+ }
}
Index: Document.java
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/Document.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- Document.java 10 Jun 2003 11:56:59 -0000 1.21
+++ Document.java 16 Jul 2003 13:34:41 -0000 1.22
@@ -38,6 +38,7 @@
package com.gargoylesoftware.htmlunit.javascript.host;
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
+import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
@@ -179,7 +180,8 @@
*/
public String jsGet_cookie() {
getLog().debug("Document.cookie not supported: returning empty string");
- return "";
+ final WebResponse webResponse = ((HtmlPage)getHtmlElementOrDie()).getWebResponse();
+ return webResponse.getResponseHeaderValue("Set-Cookie");
}
|