Skip to content

Commit

Permalink
[dotnet] Refactor Cookie object constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Oct 18, 2021
1 parent ee59a57 commit fc99bef
Showing 1 changed file with 64 additions and 68 deletions.
132 changes: 64 additions & 68 deletions dotnet/src/webdriver/Cookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,54 +36,38 @@ public class Cookie
private string cookieValue;
private string cookiePath;
private string cookieDomain;
private bool isHttpOnly;
private string sameSite;
private bool isHttpOnly;
private bool secure;
private DateTime? cookieExpiry;
private readonly string[] sameSiteValues = {"Strict", "Lax", "None"};

/// <summary>
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name and value.
/// </summary>
/// <param name="name">The name of the cookie.</param>
/// <param name="value">The value of the cookie.</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 is <see langword="null"/>.</exception>
public Cookie(string name, string value)
: this(name, value, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
/// value, domain, path and expiration date.
/// value, and path.
/// </summary>
/// <param name="name">The name of the cookie.</param>
/// <param name="value">The value of the cookie.</param>
/// <param name="domain">The domain of the cookie.</param>
/// <param name="path">The path of the cookie.</param>
/// <param name="expiry">The expiration date of the cookie.</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 is <see langword="null"/>.</exception>
public Cookie(string name, string value, string domain, string path, DateTime? expiry)
public Cookie(string name, string value, string path)
: this(name, value, path, null)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Cookie name cannot be null or empty string", "name");
}

if (value == null)
{
throw new ArgumentNullException("value", "Cookie value cannot be null");
}

if (name.IndexOf(';') != -1)
{
throw new ArgumentException("Cookie names cannot contain a ';': " + name, "name");
}

this.cookieName = name;
this.cookieValue = value;
if (!string.IsNullOrEmpty(path))
{
this.cookiePath = path;
}

this.cookieDomain = StripPort(domain);

if (expiry != null)
{
this.cookieExpiry = expiry;
}
}

/// <summary>
Expand All @@ -102,6 +86,23 @@ public Cookie(string name, string value, string path, DateTime? expiry)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
/// value, domain, path and expiration date.
/// </summary>
/// <param name="name">The name of the cookie.</param>
/// <param name="value">The value of the cookie.</param>
/// <param name="domain">The domain of the cookie.</param>
/// <param name="path">The path of the cookie.</param>
/// <param name="expiry">The expiration date of the cookie.</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 is <see langword="null"/>.</exception>
public Cookie(string name, string value, string domain, string path, DateTime? expiry)
: this(name, value, domain, path, expiry, false, false, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ReturnedCookie"/> class with a specific name,
/// value, domain, path and expiration date.
Expand All @@ -119,8 +120,36 @@ public Cookie(string name, string value, string path, DateTime? expiry)
/// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If the same site value is not valid or same site value is "None" but secure is set to false.</exception>
public Cookie(string name, string value, string domain, string path, DateTime? expiry, bool secure, bool isHttpOnly, string sameSite)
: this(name, value, domain, path, expiry)
{
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Cookie name cannot be null or empty string", "name");
}

if (value == null)
{
throw new ArgumentNullException("value", "Cookie value cannot be null");
}

if (name.IndexOf(';') != -1)
{
throw new ArgumentException("Cookie names cannot contain a ';': " + name, "name");
}

this.cookieName = name;
this.cookieValue = value;
if (!string.IsNullOrEmpty(path))
{
this.cookiePath = path;
}

this.cookieDomain = StripPort(domain);

if (expiry != null)
{
this.cookieExpiry = expiry;
}

this.isHttpOnly = isHttpOnly;
this.secure = secure;

Expand All @@ -131,42 +160,9 @@ public Cookie(string name, string value, string domain, string path, DateTime? e
throw new ArgumentException("Invalid sameSite cookie value. It should either \"Lax\", \"Strict\" or \"None\" ", "sameSite");
}

if ("None".Equals(sameSite) && !this.secure)
{
throw new ArgumentException("Invalid cookie configuration: SameSite=None must be Secure");
}

this.sameSite = sameSite;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
/// value, and path.
/// </summary>
/// <param name="name">The name of the cookie.</param>
/// <param name="value">The value of the cookie.</param>
/// <param name="path">The path of the cookie.</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 is <see langword="null"/>.</exception>
public Cookie(string name, string value, string path)
: this(name, value, path, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name and value.
/// </summary>
/// <param name="name">The name of the cookie.</param>
/// <param name="value">The value of the cookie.</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 is <see langword="null"/>.</exception>
public Cookie(string name, string value)
: this(name, value, null, null)
{
}

/// <summary>
/// Gets the name of the cookie.
Expand Down

0 comments on commit fc99bef

Please sign in to comment.