Skip to content

Commit 74701ce

Browse files
committed
Update ICookieJar contract and implementation with null-handling parameter checks
1 parent 833eea0 commit 74701ce

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

dotnet/src/webdriver/CookieJar.cs

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,21 @@
2525

2626
namespace OpenQA.Selenium
2727
{
28-
/// <summary>
29-
/// Defines an interface allowing the user to manipulate cookies on the current page.
30-
/// </summary>
31-
internal sealed class CookieJar : ICookieJar
28+
internal sealed class CookieJar(WebDriver driver) : ICookieJar
3229
{
33-
private readonly WebDriver driver;
34-
35-
/// <summary>
36-
/// Initializes a new instance of the <see cref="CookieJar"/> class.
37-
/// </summary>
38-
/// <param name="driver">The driver that is currently in use</param>
39-
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> is <see langword="null"/>.</exception>
40-
public CookieJar(WebDriver driver)
41-
{
42-
this.driver = driver ?? throw new ArgumentNullException(nameof(driver));
43-
}
44-
4530
/// <summary>
4631
/// Gets all cookies defined for the current page.
4732
/// </summary>
4833
public ReadOnlyCollection<Cookie> AllCookies
4934
{
5035
get
5136
{
52-
object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>()).Value;
37+
Response response = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>());
5338

5439
try
5540
{
5641
List<Cookie> toReturn = new List<Cookie>();
57-
if (returned is object?[] cookies)
42+
if (response.Value is object?[] cookies)
5843
{
5944
foreach (object? rawCookie in cookies)
6045
{
@@ -89,66 +74,72 @@ public void AddCookie(Cookie cookie)
8974

9075
Dictionary<string, object> parameters = new Dictionary<string, object>();
9176
parameters.Add("cookie", cookie);
92-
this.driver.InternalExecute(DriverCommand.AddCookie, parameters);
77+
driver.InternalExecute(DriverCommand.AddCookie, parameters);
9378
}
9479

95-
#nullable disable
96-
9780
/// <summary>
9881
/// Delete the cookie by passing in the name of the cookie
9982
/// </summary>
10083
/// <param name="name">The name of the cookie that is in the browser</param>
84+
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
10185
public void DeleteCookieNamed(string name)
10286
{
87+
if (name is null)
88+
{
89+
throw new ArgumentNullException(nameof(name));
90+
}
91+
10392
Dictionary<string, object> parameters = new Dictionary<string, object>();
10493
parameters.Add("name", name);
105-
this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
94+
driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
10695
}
10796

108-
#nullable enable
109-
11097
/// <summary>
11198
/// Delete a cookie in the browser by passing in a copy of a cookie
11299
/// </summary>
113100
/// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
114-
public void DeleteCookie(Cookie? cookie)
101+
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
102+
public void DeleteCookie(Cookie cookie)
115103
{
116-
if (cookie != null)
104+
if (cookie is null)
117105
{
118-
this.DeleteCookieNamed(cookie.Name);
106+
throw new ArgumentNullException(nameof(cookie));
119107
}
108+
109+
this.DeleteCookieNamed(cookie.Name);
120110
}
121111

122112
/// <summary>
123113
/// Delete All Cookies that are present in the browser
124114
/// </summary>
125115
public void DeleteAllCookies()
126116
{
127-
this.driver.InternalExecute(DriverCommand.DeleteAllCookies, null);
117+
driver.InternalExecute(DriverCommand.DeleteAllCookies, null);
128118
}
129119

130120
/// <summary>
131121
/// Method for returning a getting a cookie by name
132122
/// </summary>
133123
/// <param name="name">name of the cookie that needs to be returned</param>
134-
/// <returns>A Cookie from the name</returns>
135-
public Cookie? GetCookieNamed(string? name)
124+
/// <returns>A Cookie from the name; or <see langword="null"/> if not found.</returns>
125+
public Cookie? GetCookieNamed(string name)
136126
{
137-
Cookie? cookieToReturn = null;
138-
if (name is not null)
127+
if (name is null)
139128
{
140-
ReadOnlyCollection<Cookie> allCookies = this.AllCookies;
141-
foreach (Cookie currentCookie in allCookies)
129+
throw new ArgumentNullException(nameof(name));
130+
}
131+
132+
133+
foreach (Cookie currentCookie in this.AllCookies)
134+
{
135+
if (name.Equals(currentCookie.Name))
142136
{
143-
if (name.Equals(currentCookie.Name))
144-
{
145-
cookieToReturn = currentCookie;
146-
break;
147-
}
137+
return currentCookie;
148138
}
139+
149140
}
150141

151-
return cookieToReturn;
142+
return null;
152143
}
153144
}
154145
}

dotnet/src/webdriver/ICookieJar.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,23 @@ public interface ICookieJar
4747
/// <param name="name">The name of the cookie to retrieve.</param>
4848
/// <returns>The <see cref="Cookie"/> containing the name. Returns <see langword="null"/>
4949
/// if no cookie with the specified name is found.</returns>
50-
Cookie? GetCookieNamed(string? name);
50+
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
51+
Cookie? GetCookieNamed(string name);
5152

5253
/// <summary>
5354
/// Deletes the specified cookie from the page.
5455
/// </summary>
5556
/// <param name="cookie">The <see cref="Cookie"/> to be deleted.</param>
56-
void DeleteCookie(Cookie? cookie);
57-
58-
#nullable disable
57+
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
58+
void DeleteCookie(Cookie cookie);
5959

6060
/// <summary>
6161
/// Deletes the cookie with the specified name from the page.
6262
/// </summary>
6363
/// <param name="name">The name of the cookie to be deleted.</param>
64+
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
6465
void DeleteCookieNamed(string name);
6566

66-
#nullable enable
67-
6867
/// <summary>
6968
/// Deletes all cookies from the page.
7069
/// </summary>

0 commit comments

Comments
 (0)