Skip to content

Commit eb4c4fc

Browse files
pujaganijimevans
andauthored
[dotnet] Add constructor for creating a Cookie instance with all optional arguments (#9361)
Co-authored-by: jimevans <[email protected]>
1 parent e6cd2a8 commit eb4c4fc

File tree

3 files changed

+77
-29
lines changed

3 files changed

+77
-29
lines changed

dotnet/src/webdriver/Cookie.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// </copyright>
1818

1919
using System;
20+
using System.Linq;
2021
using System.Collections.Generic;
2122
using System.Globalization;
2223
using Newtonsoft.Json;
@@ -35,8 +36,11 @@ public class Cookie
3536
private string cookieValue;
3637
private string cookiePath;
3738
private string cookieDomain;
39+
private bool isHttpOnly;
3840
private string sameSite;
41+
private bool secure;
3942
private DateTime? cookieExpiry;
43+
private readonly string[] sameSiteValues = {"Strict", "Lax", "None"};
4044

4145
/// <summary>
4246
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
@@ -98,6 +102,44 @@ public Cookie(string name, string value, string path, DateTime? expiry)
98102
{
99103
}
100104

105+
/// <summary>
106+
/// Initializes a new instance of the <see cref="ReturnedCookie"/> class with a specific name,
107+
/// value, domain, path and expiration date.
108+
/// </summary>
109+
/// <param name="name">The name of the cookie.</param>
110+
/// <param name="value">The value of the cookie.</param>
111+
/// <param name="domain">The domain of the cookie.</param>
112+
/// <param name="path">The path of the cookie.</param>
113+
/// <param name="expiry">The expiration date of the cookie.</param>
114+
/// <param name="isSecure"><see langword="true"/> if the cookie is secure; otherwise <see langword="false"/></param>
115+
/// <param name="isHttpOnly"><see langword="true"/> if the cookie is an HTTP-only cookie; otherwise <see langword="false"/></param>
116+
/// <param name="sameSite">The SameSite value of cookie.</param>
117+
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
118+
/// or if it contains a semi-colon.</exception>
119+
/// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
120+
/// <exception cref="ArgumentNullException">If the same site value is not valid or same site value is "None" but secure is set to false.</exception>
121+
public Cookie(string name, string value, string domain, string path, DateTime? expiry, bool secure, bool isHttpOnly, string sameSite)
122+
: this(name, value, domain, path, expiry)
123+
{
124+
this.isHttpOnly = isHttpOnly;
125+
this.secure = secure;
126+
127+
if (!string.IsNullOrEmpty(sameSite))
128+
{
129+
if (!sameSiteValues.Contains(sameSite))
130+
{
131+
throw new ArgumentException("Invalid sameSite cookie value. It should either \"Lax\", \"Strict\" or \"None\" ", "sameSite");
132+
}
133+
134+
if ("None".Equals(sameSite) && !this.secure)
135+
{
136+
throw new ArgumentException("Invalid cookie configuration: SameSite=None must be Secure");
137+
}
138+
139+
this.sameSite = sameSite;
140+
}
141+
}
142+
101143
/// <summary>
102144
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
103145
/// value, and path.
@@ -168,7 +210,7 @@ public virtual string Path
168210
[JsonProperty("secure")]
169211
public virtual bool Secure
170212
{
171-
get { return false; }
213+
get { return this.secure; }
172214
}
173215

174216
/// <summary>
@@ -177,7 +219,8 @@ public virtual bool Secure
177219
[JsonProperty("httpOnly")]
178220
public virtual bool IsHttpOnly
179221
{
180-
get { return false; }
222+
get { return this.isHttpOnly; }
223+
181224
}
182225

183226
/// <summary>
@@ -187,7 +230,6 @@ public virtual bool IsHttpOnly
187230
public virtual string SameSite
188231
{
189232
get { return this.sameSite; }
190-
protected set { this.sameSite = value; }
191233
}
192234

193235
/// <summary>
@@ -287,7 +329,8 @@ public override string ToString()
287329
return this.cookieName + "=" + this.cookieValue
288330
+ (this.cookieExpiry == null ? string.Empty : "; expires=" + this.cookieExpiry.Value.ToUniversalTime().ToString("ddd MM dd yyyy hh:mm:ss UTC", CultureInfo.InvariantCulture))
289331
+ (string.IsNullOrEmpty(this.cookiePath) ? string.Empty : "; path=" + this.cookiePath)
290-
+ (string.IsNullOrEmpty(this.cookieDomain) ? string.Empty : "; domain=" + this.cookieDomain);
332+
+ (string.IsNullOrEmpty(this.cookieDomain) ? string.Empty : "; domain=" + this.cookieDomain)
333+
+ "; isHttpOnly= " + this.isHttpOnly + "; secure= " + this.secure + (string.IsNullOrEmpty(this.sameSite) ? string.Empty : "; sameSite=" + this.sameSite);
291334
}
292335

293336
/// <summary>

dotnet/src/webdriver/Internal/ReturnedCookie.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ namespace OpenQA.Selenium.Internal
2626
/// </summary>
2727
public class ReturnedCookie : Cookie
2828
{
29-
private bool isSecure;
30-
private bool isHttpOnly;
31-
29+
3230
/// <summary>
3331
/// Initializes a new instance of the <see cref="ReturnedCookie"/> class with a specific name,
3432
/// value, domain, path and expiration date.
@@ -64,27 +62,9 @@ public ReturnedCookie(string name, string value, string domain, string path, Dat
6462
/// or if it contains a semi-colon.</exception>
6563
/// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
6664
public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly, string sameSite)
67-
: base(name, value, domain, path, expiry)
68-
{
69-
this.isSecure = isSecure;
70-
this.isHttpOnly = isHttpOnly;
71-
this.SameSite = sameSite;
72-
}
73-
74-
/// <summary>
75-
/// Gets a value indicating whether the cookie is secure.
76-
/// </summary>
77-
public override bool Secure
78-
{
79-
get { return this.isSecure; }
80-
}
81-
82-
/// <summary>
83-
/// Gets a value indicating whether the cookie is an HTTP-only cookie.
84-
/// </summary>
85-
public override bool IsHttpOnly
65+
: base(name, value, domain, path, expiry, isSecure, isHttpOnly, sameSite)
8666
{
87-
get { return this.isHttpOnly; }
67+
8868
}
8969

9070
/// <summary>
@@ -97,8 +77,9 @@ public override string ToString()
9777
+ (this.Expiry == null ? string.Empty : "; expires=" + this.Expiry.Value.ToUniversalTime().ToString("ddd MM/dd/yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture))
9878
+ (string.IsNullOrEmpty(this.Path) ? string.Empty : "; path=" + this.Path)
9979
+ (string.IsNullOrEmpty(this.Domain) ? string.Empty : "; domain=" + this.Domain)
100-
+ (this.isSecure ? "; secure" : string.Empty)
101-
+ (this.isHttpOnly ? "; httpOnly" : string.Empty);
80+
+ (this.Secure ? "; secure" : string.Empty)
81+
+ (this.IsHttpOnly ? "; httpOnly" : string.Empty)
82+
+ (string.IsNullOrEmpty(this.SameSite) ? string.Empty : "; sameSite=" + this.SameSite);
10283
}
10384
}
10485
}

dotnet/test/common/CookieTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ public void ShouldThrowAnExceptionWhenTheNameIsNull()
2525
Assert.That(() => new ReturnedCookie(null, "value", null, null, DateTime.Now, false, false), Throws.InstanceOf<ArgumentException>());
2626
}
2727

28+
[Test]
29+
public void ShouldThrowAnExceptionWhenSameSiteIsWrong()
30+
{
31+
Assert.That(() => new ReturnedCookie("name", "value", "" , "/", DateTime.Now, true, true, "Wrong"), Throws.InstanceOf<ArgumentException>());
32+
}
33+
34+
[Test]
35+
public void ShouldThrowAnExceptionWhenSameSiteIsNoneButNotSecure()
36+
{
37+
Assert.That(() => new ReturnedCookie("name", "value", "", "/", DateTime.Now, false, true, "None"), Throws.InstanceOf<ArgumentException>());
38+
}
39+
40+
[Test]
41+
public void CookiesShouldAllowOptionalParametersToBeSet()
42+
{
43+
DateTime expiry = DateTime.Now;
44+
Cookie cookie = new Cookie ("name", "value", "test.com", "/", expiry, true, true, "None");
45+
Assert.That(cookie.Domain, Is.EqualTo("test.com"));
46+
Assert.That(cookie.Path, Is.EqualTo("/"));
47+
Assert.That(cookie.IsHttpOnly, Is.True);
48+
Assert.That(cookie.Secure, Is.True);
49+
Assert.That(cookie.SameSite, Is.EqualTo("None"));
50+
}
51+
2852
[Test]
2953
public void CookiesShouldAllowSecureToBeSet()
3054
{

0 commit comments

Comments
 (0)