Skip to content

Commit

Permalink
Adding httpOnly cookie flag to Java binding
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Mar 10, 2014
1 parent 6af0f09 commit 6118261
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
6 changes: 1 addition & 5 deletions .idea/ant.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 34 additions & 5 deletions java/client/src/org/openqa/selenium/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public class Cookie {
private final String domain;
private final Date expiry;
private final boolean isSecure;
private final boolean isHttpOnly;

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

/**
* Creates an insecure cookie.
* Creates an insecure non-httpOnly cookie.
*
* @param name The name of the cookie; may not be null or an empty string.
* @param value The cookie value; may not be null.
Expand All @@ -57,7 +58,7 @@ public Cookie(String name, String value, String domain, String path, Date expiry
}

/**
* Creates a cookie.
* Creates a non-httpOnly cookie.
*
* @param name The name of the cookie; may not be null or an empty string.
* @param value The cookie value; may not be null.
Expand All @@ -68,13 +69,31 @@ public Cookie(String name, String value, String domain, String path, Date expiry
* @param isSecure Whether this cookie requires a secure connection.
*/
public Cookie(String name, String value, String domain, String path, Date expiry,
boolean isSecure) {
boolean isSecure) {
this(name, value, domain, path, expiry, isSecure, false);
}

/**
* Creates a cookie.
*
* @param name The name of the cookie; may not be null or an empty string.
* @param value The cookie value; may not be null.
* @param domain The domain the cookie is visible to.
* @param path The path the cookie is visible to. If left blank or set to null, will be set to
* "/".
* @param expiry The cookie's expiration date; may be null.
* @param isSecure Whether this cookie requires a secure connection.
* @param isHttpOnly Whether this cookie is a httpOnly cooke.
*/
public Cookie(String name, String value, String domain, String path, Date expiry,
boolean isSecure, boolean isHttpOnly) {
this.name = name;
this.value = value;
this.path = path == null || "".equals(path) ? "/" : path;

this.domain = stripPort(domain);
this.isSecure = isSecure;
this.isHttpOnly = isHttpOnly;

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

public boolean isHttpOnly() {
return isHttpOnly;
}

public Date getExpiry() {
return expiry;
}
Expand Down Expand Up @@ -193,6 +216,7 @@ public static class Builder {
private String domain;
private Date expiry;
private boolean secure;
private boolean httpOnly;

public Builder(String name, String value) {
this.name = name;
Expand All @@ -219,8 +243,13 @@ public Builder isSecure(boolean secure) {
return this;
}

public Builder isHttpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
}

public Cookie build() {
return new Cookie(name, value, domain, path, expiry, secure);
return new Cookie(name, value, domain, path, expiry, secure, httpOnly);
}
}
}
12 changes: 12 additions & 0 deletions java/client/test/org/openqa/selenium/CookieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,16 @@ public void testSecureDefaultsToFalse() {
Cookie cookie = new Cookie("name", "value");
assertFalse(cookie.isSecure());
}

@Test
public void testCookiesShouldAllowHttpOnlyToBeSet() {
Cookie cookie = new Cookie("name", "value", "", "/", new Date(), false, true);
assertTrue(cookie.isHttpOnly());
}

@Test
public void testHttpOnlyDefaultsToFalse() {
Cookie cookie = new Cookie("name", "value");
assertFalse(cookie.isHttpOnly());
}
}

0 comments on commit 6118261

Please sign in to comment.