From: <mgu...@us...> - 2013-01-24 09:49:21
|
Revision: 8032 http://sourceforge.net/p/htmlunit/code/8032 Author: mguillem Date: 2013-01-24 09:49:17 +0000 (Thu, 24 Jan 2013) Log Message: ----------- JavaScript: determine default path for cookies set with document.cookie from current URL. Issue 1458 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/CookieManagerTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-01-24 08:06:51 UTC (rev 8031) +++ trunk/htmlunit/src/changes/changes.xml 2013-01-24 09:49:17 UTC (rev 8032) @@ -8,6 +8,9 @@ <body> <release version="2.12" date="???" description="Bugfixes, CSS3 Selectors"> + <action type="fix" dev="mguillem" issue="1458"> + JavaScript: determine default path for cookies set with document.cookie from current URL. + </action> <action type="fix" dev="mguillem" issue="898"> JavaScript: place document before window in scope chain for event handlers defined in HTML attributes. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java 2013-01-24 08:06:51 UTC (rev 8031) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java 2013-01-24 09:49:17 UTC (rev 8032) @@ -896,7 +896,7 @@ // Default attribute values (note: HttpClient doesn't like null paths). final Map<String, Object> atts = new HashMap<String, Object>(); atts.put("domain", currentURL.getHost()); - atts.put("path", ""); + atts.put("path", getDefaultCookiePath(currentURL)); // Custom attribute values. while (st.hasMoreTokens()) { @@ -924,6 +924,23 @@ } /** + * Same logic than in CookieSpecBase#getDefaultPath which is protected. + */ + private static String getDefaultCookiePath(final URL url) { + String path = url.getPath(); + final int lastSlashIndex = path.lastIndexOf('/'); + if (lastSlashIndex >= 0) { + if (lastSlashIndex == 0) { + path = "/"; + } + else { + path = path.substring(0, lastSlashIndex); + } + } + return path; + } + + /** * Returns the value of the "images" property. * @return the value of the "images" property */ Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/CookieManagerTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/CookieManagerTest.java 2013-01-24 08:06:51 UTC (rev 8031) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/CookieManagerTest.java 2013-01-24 09:49:17 UTC (rev 8032) @@ -439,6 +439,7 @@ public void setCookieDifferentPath() throws Exception { final List<NameValuePair> responseHeader1 = new ArrayList<NameValuePair>(); responseHeader1.add(new NameValuePair("Set-Cookie", "first=1; path=/foo/blah")); + responseHeader1.add(new NameValuePair("Set-Cookie", "second=2; path=/other/path")); responseHeader1.add(new NameValuePair("Location", "/foo/blah")); getMockWebConnection().setDefaultResponse(HTML_ALERT_COOKIE); @@ -475,4 +476,49 @@ assertFalse(mgr.getCookie("second").isHttpOnly()); } } + + /** + * A cookie set with document.cookie without path applies only for the current path + * and overrides cookie previously set for this path. + * @throws Exception if the test fails + */ + @Test + @Alerts("first=new") + public void cookieSetFromJSWithoutPathUsesCurrentLocation() throws Exception { + final List<NameValuePair> responseHeader1 = new ArrayList<NameValuePair>(); + responseHeader1.add(new NameValuePair("Set-Cookie", "first=1")); + + final String html = "<head><body><script>\n" + + "document.cookie = 'first=new';\n" + + "location.replace('/a/b/-');\n" + + "</script></body></html>"; + + getMockWebConnection().setDefaultResponse(HTML_ALERT_COOKIE); + final URL firstUrl = new URL(getDefaultUrl(), "/a/b"); + getMockWebConnection().setResponse(firstUrl, html, 200, "Ok", "text/html", responseHeader1); + + loadPageWithAlerts2(firstUrl); + } + + /** + * A cookie set with document.cookie without path applies only for the current path. + * @throws Exception if the test fails + */ + @Test + @Alerts("first=1") + public void cookieSetFromJSWithoutPathUsesCurrentLocation2() throws Exception { + final List<NameValuePair> responseHeader1 = new ArrayList<NameValuePair>(); + responseHeader1.add(new NameValuePair("Set-Cookie", "first=1; path=/c")); + + final String html = "<head><body><script>\n" + + "document.cookie = 'first=new';\n" + + "location.replace('/c');\n" + + "</script></body></html>"; + + getMockWebConnection().setDefaultResponse(HTML_ALERT_COOKIE); + final URL firstUrl = new URL(getDefaultUrl(), "/a/b"); + getMockWebConnection().setResponse(firstUrl, html, 200, "Ok", "text/html", responseHeader1); + + loadPageWithAlerts2(firstUrl); + } } |