Skip to content

Commit fc99bef

Browse files
committed
[dotnet] Refactor Cookie object constructors
1 parent ee59a57 commit fc99bef

File tree

1 file changed

+64
-68
lines changed

1 file changed

+64
-68
lines changed

dotnet/src/webdriver/Cookie.cs

Lines changed: 64 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -36,54 +36,38 @@ public class Cookie
3636
private string cookieValue;
3737
private string cookiePath;
3838
private string cookieDomain;
39-
private bool isHttpOnly;
4039
private string sameSite;
40+
private bool isHttpOnly;
4141
private bool secure;
4242
private DateTime? cookieExpiry;
4343
private readonly string[] sameSiteValues = {"Strict", "Lax", "None"};
4444

45+
/// <summary>
46+
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name and value.
47+
/// </summary>
48+
/// <param name="name">The name of the cookie.</param>
49+
/// <param name="value">The value of the cookie.</param>
50+
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
51+
/// or if it contains a semi-colon.</exception>
52+
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
53+
public Cookie(string name, string value)
54+
: this(name, value, null)
55+
{
56+
}
57+
4558
/// <summary>
4659
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
47-
/// value, domain, path and expiration date.
60+
/// value, and path.
4861
/// </summary>
4962
/// <param name="name">The name of the cookie.</param>
5063
/// <param name="value">The value of the cookie.</param>
51-
/// <param name="domain">The domain of the cookie.</param>
5264
/// <param name="path">The path of the cookie.</param>
53-
/// <param name="expiry">The expiration date of the cookie.</param>
5465
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
5566
/// or if it contains a semi-colon.</exception>
5667
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
57-
public Cookie(string name, string value, string domain, string path, DateTime? expiry)
68+
public Cookie(string name, string value, string path)
69+
: this(name, value, path, null)
5870
{
59-
if (string.IsNullOrEmpty(name))
60-
{
61-
throw new ArgumentException("Cookie name cannot be null or empty string", "name");
62-
}
63-
64-
if (value == null)
65-
{
66-
throw new ArgumentNullException("value", "Cookie value cannot be null");
67-
}
68-
69-
if (name.IndexOf(';') != -1)
70-
{
71-
throw new ArgumentException("Cookie names cannot contain a ';': " + name, "name");
72-
}
73-
74-
this.cookieName = name;
75-
this.cookieValue = value;
76-
if (!string.IsNullOrEmpty(path))
77-
{
78-
this.cookiePath = path;
79-
}
80-
81-
this.cookieDomain = StripPort(domain);
82-
83-
if (expiry != null)
84-
{
85-
this.cookieExpiry = expiry;
86-
}
8771
}
8872

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

89+
/// <summary>
90+
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
91+
/// value, domain, path and expiration date.
92+
/// </summary>
93+
/// <param name="name">The name of the cookie.</param>
94+
/// <param name="value">The value of the cookie.</param>
95+
/// <param name="domain">The domain of the cookie.</param>
96+
/// <param name="path">The path of the cookie.</param>
97+
/// <param name="expiry">The expiration date of the cookie.</param>
98+
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
99+
/// or if it contains a semi-colon.</exception>
100+
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
101+
public Cookie(string name, string value, string domain, string path, DateTime? expiry)
102+
: this(name, value, domain, path, expiry, false, false, null)
103+
{
104+
}
105+
105106
/// <summary>
106107
/// Initializes a new instance of the <see cref="ReturnedCookie"/> class with a specific name,
107108
/// value, domain, path and expiration date.
@@ -119,8 +120,36 @@ public Cookie(string name, string value, string path, DateTime? expiry)
119120
/// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
120121
/// <exception cref="ArgumentNullException">If the same site value is not valid or same site value is "None" but secure is set to false.</exception>
121122
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-
{
123+
{
124+
if (string.IsNullOrEmpty(name))
125+
{
126+
throw new ArgumentException("Cookie name cannot be null or empty string", "name");
127+
}
128+
129+
if (value == null)
130+
{
131+
throw new ArgumentNullException("value", "Cookie value cannot be null");
132+
}
133+
134+
if (name.IndexOf(';') != -1)
135+
{
136+
throw new ArgumentException("Cookie names cannot contain a ';': " + name, "name");
137+
}
138+
139+
this.cookieName = name;
140+
this.cookieValue = value;
141+
if (!string.IsNullOrEmpty(path))
142+
{
143+
this.cookiePath = path;
144+
}
145+
146+
this.cookieDomain = StripPort(domain);
147+
148+
if (expiry != null)
149+
{
150+
this.cookieExpiry = expiry;
151+
}
152+
124153
this.isHttpOnly = isHttpOnly;
125154
this.secure = secure;
126155

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

134-
if ("None".Equals(sameSite) && !this.secure)
135-
{
136-
throw new ArgumentException("Invalid cookie configuration: SameSite=None must be Secure");
137-
}
138-
139163
this.sameSite = sameSite;
140164
}
141165
}
142-
143-
/// <summary>
144-
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
145-
/// value, and path.
146-
/// </summary>
147-
/// <param name="name">The name of the cookie.</param>
148-
/// <param name="value">The value of the cookie.</param>
149-
/// <param name="path">The path of the cookie.</param>
150-
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
151-
/// or if it contains a semi-colon.</exception>
152-
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
153-
public Cookie(string name, string value, string path)
154-
: this(name, value, path, null)
155-
{
156-
}
157-
158-
/// <summary>
159-
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name and value.
160-
/// </summary>
161-
/// <param name="name">The name of the cookie.</param>
162-
/// <param name="value">The value of the cookie.</param>
163-
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
164-
/// or if it contains a semi-colon.</exception>
165-
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
166-
public Cookie(string name, string value)
167-
: this(name, value, null, null)
168-
{
169-
}
170166

171167
/// <summary>
172168
/// Gets the name of the cookie.

0 commit comments

Comments
 (0)