Skip to content

Commit 866f991

Browse files
committed
Document: fix document.cookie ordering
1 parent f33f3c8 commit 866f991

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

src/main/java/org/htmlunit/javascript/host/dom/Document.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import java.net.URL;
2929
import java.time.ZoneId;
3030
import java.time.format.DateTimeFormatter;
31+
import java.util.ArrayList;
3132
import java.util.Date;
3233
import java.util.HashMap;
3334
import java.util.HashSet;
35+
import java.util.List;
3436
import java.util.Locale;
3537
import java.util.Map;
3638
import java.util.Set;
@@ -1133,10 +1135,23 @@ public String getCookie() {
11331135

11341136
final StringBuilder builder = new StringBuilder();
11351137
final Set<Cookie> cookies = sgmlPage.getWebClient().getCookies(sgmlPage.getUrl());
1138+
final List<Cookie> visibleCookies = new ArrayList<>();
11361139
for (final Cookie cookie : cookies) {
11371140
if (cookie.isHttpOnly()) {
11381141
continue;
11391142
}
1143+
visibleCookies.add(cookie);
1144+
}
1145+
1146+
visibleCookies.sort((a, b) -> {
1147+
final String pathA = a.getPath();
1148+
final String pathB = b.getPath();
1149+
final int lenA = pathA == null ? 0 : pathA.length();
1150+
final int lenB = pathB == null ? 0 : pathB.length();
1151+
return Integer.compare(lenB, lenA);
1152+
});
1153+
1154+
for (final Cookie cookie : visibleCookies) {
11401155
if (builder.length() != 0) {
11411156
builder.append("; ");
11421157
}

src/test/java/org/htmlunit/CookieManagerTest.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,50 @@ public void orderFromServer() throws Exception {
9494
* @throws Exception if the test fails
9595
*/
9696
@Test
97+
@Alerts({"b=2; a=1",
98+
"c=1; d=2",
99+
"lA=2; lB=3; sA=1; sB=4"})
97100
public void orderCookiesByPath_fromJs() throws Exception {
98101
final String html = DOCTYPE_HTML
99-
+ "<html><body><script>\n"
100-
+ "document.cookie = 'exampleCookie=rootPath;path=/';\n"
101-
+ "document.cookie = 'exampleCookie=currentPath;path=/testpages/';\n"
102-
+ "</script>\n"
102+
+ "<html><head><script>\n"
103+
+ LOG_TITLE_FUNCTION
104+
+ " function clear(n, p) {\n"
105+
+ " document.cookie = n + '=; path=' + p + '; max-age=0';\n"
106+
+ " }\n"
107+
+ " function test() {\n"
108+
+ " document.cookie = 'a=1; path=/';\n"
109+
+ " document.cookie = 'b=2; path=/testpages';\n"
110+
+ " log(document.cookie);\n"
111+
+ " clear('a', '/'); clear('b', '/testpages');\n"
112+
+ " document.cookie = 'c=1; path=/testpages';\n"
113+
+ " document.cookie = 'd=2; path=/testpages';\n"
114+
+ " log(document.cookie);\n"
115+
+ " clear('c', '/testpages'); clear('d', '/testpages');\n"
116+
+ " document.cookie = 'sA=1; path=/';\n"
117+
+ " document.cookie = 'lA=2; path=/testpages';\n"
118+
+ " document.cookie = 'lB=3; path=/testpages';\n"
119+
+ " document.cookie = 'sB=4; path=/';\n"
120+
+ " log(document.cookie);\n"
121+
+ " }\n"
122+
+ "</script></head>\n"
123+
+ "<body onload='test()'>\n"
103124
+ "<a href='/testpages/next.html'>next page</a>\n"
104125
+ "</body></html>";
105126

127+
final URL pageUrl = new URL(URL_FIRST, "testpages/test.html");
128+
getMockWebConnection().setResponse(pageUrl, html);
106129
getMockWebConnection().setDefaultResponse("");
107130

108131
final WebDriver webDriver = getWebDriver();
109132
webDriver.manage().deleteAllCookies();
110133

111-
loadPage2(html);
134+
loadPage2(pageUrl, StandardCharsets.ISO_8859_1);
135+
verifyTitle2(getWebDriver(), getExpectedAlerts());
136+
112137
webDriver.findElement(By.linkText("next page")).click();
113138

114139
final WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
115-
assertEquals("exampleCookie=currentPath; exampleCookie=rootPath",
140+
assertEquals("lA=2; lB=3; sA=1; sB=4",
116141
lastRequest.getAdditionalHeaders().get(HttpHeader.COOKIE));
117142
}
118143

0 commit comments

Comments
 (0)