Skip to content

Commit

Permalink
Adding support to .NET bindings for HTTP-only cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Jun 23, 2015
1 parent 773f8d4 commit 460856f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
21 changes: 18 additions & 3 deletions dotnet/src/webdriver/Cookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ public virtual bool Secure
get { return false; }
}

/// <summary>
/// Gets a value indicating whether the cookie is an HTTP-only cookie.
/// </summary>
[JsonProperty("httpOnly")]
public virtual bool IsHttpOnly
{
get { return false; }
}

/// <summary>
/// Gets the expiration date of the cookie.
/// </summary>
Expand All @@ -195,7 +204,8 @@ public virtual bool Secure

DateTime zeroDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan span = this.cookieExpiry.Value.ToUniversalTime().Subtract(zeroDate);
return Convert.ToInt64(span.TotalSeconds);
long totalSeconds = Convert.ToInt64(span.TotalSeconds);
return totalSeconds;
}
}

Expand Down Expand Up @@ -249,7 +259,13 @@ public static Cookie FromDictionary(Dictionary<string, object> rawCookie)
secure = bool.Parse(rawCookie["secure"].ToString());
}

return new ReturnedCookie(name, value, domain, path, expires, secure);
bool isHttpOnly = false;
if (rawCookie.ContainsKey("httpOnly") && rawCookie["httpOnly"] != null)
{
isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString());
}

return new ReturnedCookie(name, value, domain, path, expires, secure, isHttpOnly);
}

/// <summary>
Expand All @@ -262,7 +278,6 @@ public override string ToString()
+ (this.cookieExpiry == null ? string.Empty : "; expires=" + this.cookieExpiry.Value.ToUniversalTime().ToString("ddd MM dd yyyy hh:mm:ss UTC", CultureInfo.InvariantCulture))
+ (string.IsNullOrEmpty(this.cookiePath) ? string.Empty : "; path=" + this.cookiePath)
+ (string.IsNullOrEmpty(this.cookieDomain) ? string.Empty : "; domain=" + this.cookieDomain);
//// + (isSecure ? ";secure;" : "");
}

/// <summary>
Expand Down
18 changes: 15 additions & 3 deletions dotnet/src/webdriver/Internal/ReturnedCookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace OpenQA.Selenium.Internal
public class ReturnedCookie : Cookie
{
private bool isSecure;
private bool isHttpOnly;

/// <summary>
/// Initializes a new instance of the <see cref="ReturnedCookie"/> class with a specific name,
Expand All @@ -41,23 +42,33 @@ public class ReturnedCookie : Cookie
/// <param name="path">The path of the cookie.</param>
/// <param name="expiry">The expiration date of the cookie.</param>
/// <param name="isSecure"><see langword="true"/> if the cookie is secure; otherwise <see langword="false"/></param>
/// <param name="isHttpOnly"><see langword="true"/> if the cookie is an HTTP-only cookie; otherwise <see langword="false"/></param>
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
/// or if it contains a semi-colon.</exception>
/// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure)
public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly)
: base(name, value, domain, path, expiry)
{
this.isSecure = isSecure;
this.isHttpOnly = isHttpOnly;
}

/// <summary>
/// Gets a value determining if the cookie is secure.
/// Gets a value indicating whether the cookie is secure.
/// </summary>
public override bool Secure
{
get { return this.isSecure; }
}

/// <summary>
/// Gets a value indicating whether the cookie is an HTTP-only cookie.
/// </summary>
public override bool IsHttpOnly
{
get { return this.isHttpOnly; }
}

/// <summary>
/// Creates and returns a string representation of the current cookie.
/// </summary>
Expand All @@ -68,7 +79,8 @@ public override string ToString()
+ (this.Expiry == null ? string.Empty : "; expires=" + this.Expiry.Value.ToUniversalTime().ToString("ddd MM/dd/yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture))
+ (string.IsNullOrEmpty(this.Path) ? string.Empty : "; path=" + this.Path)
+ (string.IsNullOrEmpty(this.Domain) ? string.Empty : "; domain=" + this.Domain)
+ (this.isSecure ? ";secure;" : string.Empty);
+ (this.isSecure ? "; secure" : string.Empty)
+ (this.isHttpOnly ? "; httpOnly" : string.Empty);
}
}
}
19 changes: 13 additions & 6 deletions dotnet/test/common/CookieTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,48 @@ public class CookieTest
[Test]
public void CanCreateAWellFormedCookie()
{
new ReturnedCookie("Fish", "cod", "", "", DateTime.Now, false);
new ReturnedCookie("Fish", "cod", "", "", DateTime.Now, false, false);
}

[Test]
[ExpectedException(typeof(ArgumentException))]
public void ShouldThrowAnExceptionWhenSemiColonExistsInTheCookieAttribute()
{
new ReturnedCookie("hi;hi", "value", null, null, DateTime.Now, false);
new ReturnedCookie("hi;hi", "value", null, null, DateTime.Now, false, false);
}

[Test]
[ExpectedException(typeof(ArgumentException))]
public void ShouldThrowAnExceptionWhenTheNameIsNull()
{
new ReturnedCookie(null, "value", null, null, DateTime.Now, false);
new ReturnedCookie(null, "value", null, null, DateTime.Now, false, false);
}

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowAnExceptionWhenTheValueIsNull()
{
new ReturnedCookie("name", null, null, null, DateTime.Now, false);
new ReturnedCookie("name", null, null, null, DateTime.Now, false, false);
}

[Test]
public void CookiesShouldAllowSecureToBeSet()
{
Cookie cookie = new ReturnedCookie("name", "value", "", "/", DateTime.Now, true);
Cookie cookie = new ReturnedCookie("name", "value", "", "/", DateTime.Now, true, false);
Assert.IsTrue(cookie.Secure);
}

[Test]
public void CookiesShouldAllowHttpOnlyToBeSet()
{
Cookie cookie = new ReturnedCookie("name", "value", "", "/", DateTime.Now, false, true);
Assert.IsTrue(cookie.IsHttpOnly);
}

[Test]
public void ShouldAllowExpiryToBeNull()
{
Cookie cookie = new ReturnedCookie("name", "value", "", "/", null, false);
Cookie cookie = new ReturnedCookie("name", "value", "", "/", null, false, false);
}
}
}

0 comments on commit 460856f

Please sign in to comment.