Skip to content

Commit 6118261

Browse files
committed
Adding httpOnly cookie flag to Java binding
1 parent 6af0f09 commit 6118261

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

.idea/ant.xml

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/client/src/org/openqa/selenium/Cookie.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public class Cookie {
2626
private final String domain;
2727
private final Date expiry;
2828
private final boolean isSecure;
29+
private final boolean isHttpOnly;
2930

3031
/**
31-
* Creates an insecure cookie with no domain specified.
32+
* Creates an insecure non-httpOnly cookie with no domain specified.
3233
*
3334
* @param name The name of the cookie; may not be null or an empty string.
3435
* @param value The cookie value; may not be null.
@@ -42,7 +43,7 @@ public Cookie(String name, String value, String path, Date expiry) {
4243
}
4344

4445
/**
45-
* Creates an insecure cookie.
46+
* Creates an insecure non-httpOnly cookie.
4647
*
4748
* @param name The name of the cookie; may not be null or an empty string.
4849
* @param value The cookie value; may not be null.
@@ -57,7 +58,7 @@ public Cookie(String name, String value, String domain, String path, Date expiry
5758
}
5859

5960
/**
60-
* Creates a cookie.
61+
* Creates a non-httpOnly cookie.
6162
*
6263
* @param name The name of the cookie; may not be null or an empty string.
6364
* @param value The cookie value; may not be null.
@@ -68,13 +69,31 @@ public Cookie(String name, String value, String domain, String path, Date expiry
6869
* @param isSecure Whether this cookie requires a secure connection.
6970
*/
7071
public Cookie(String name, String value, String domain, String path, Date expiry,
71-
boolean isSecure) {
72+
boolean isSecure) {
73+
this(name, value, domain, path, expiry, isSecure, false);
74+
}
75+
76+
/**
77+
* Creates a cookie.
78+
*
79+
* @param name The name of the cookie; may not be null or an empty string.
80+
* @param value The cookie value; may not be null.
81+
* @param domain The domain the cookie is visible to.
82+
* @param path The path the cookie is visible to. If left blank or set to null, will be set to
83+
* "/".
84+
* @param expiry The cookie's expiration date; may be null.
85+
* @param isSecure Whether this cookie requires a secure connection.
86+
* @param isHttpOnly Whether this cookie is a httpOnly cooke.
87+
*/
88+
public Cookie(String name, String value, String domain, String path, Date expiry,
89+
boolean isSecure, boolean isHttpOnly) {
7290
this.name = name;
7391
this.value = value;
7492
this.path = path == null || "".equals(path) ? "/" : path;
7593

7694
this.domain = stripPort(domain);
7795
this.isSecure = isSecure;
96+
this.isHttpOnly = isHttpOnly;
7897

7998
if (expiry != null) {
8099
// Expiration date is specified in seconds since (UTC) epoch time, so truncate the date.
@@ -125,6 +144,10 @@ public boolean isSecure() {
125144
return isSecure;
126145
}
127146

147+
public boolean isHttpOnly() {
148+
return isHttpOnly;
149+
}
150+
128151
public Date getExpiry() {
129152
return expiry;
130153
}
@@ -193,6 +216,7 @@ public static class Builder {
193216
private String domain;
194217
private Date expiry;
195218
private boolean secure;
219+
private boolean httpOnly;
196220

197221
public Builder(String name, String value) {
198222
this.name = name;
@@ -219,8 +243,13 @@ public Builder isSecure(boolean secure) {
219243
return this;
220244
}
221245

246+
public Builder isHttpOnly(boolean httpOnly) {
247+
this.httpOnly = httpOnly;
248+
return this;
249+
}
250+
222251
public Cookie build() {
223-
return new Cookie(name, value, domain, path, expiry, secure);
252+
return new Cookie(name, value, domain, path, expiry, secure, httpOnly);
224253
}
225254
}
226255
}

java/client/test/org/openqa/selenium/CookieTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,16 @@ public void testSecureDefaultsToFalse() {
6464
Cookie cookie = new Cookie("name", "value");
6565
assertFalse(cookie.isSecure());
6666
}
67+
68+
@Test
69+
public void testCookiesShouldAllowHttpOnlyToBeSet() {
70+
Cookie cookie = new Cookie("name", "value", "", "/", new Date(), false, true);
71+
assertTrue(cookie.isHttpOnly());
72+
}
73+
74+
@Test
75+
public void testHttpOnlyDefaultsToFalse() {
76+
Cookie cookie = new Cookie("name", "value");
77+
assertFalse(cookie.isHttpOnly());
78+
}
6779
}

0 commit comments

Comments
 (0)