Skip to content

Commit 1b25a75

Browse files
committed
[dotnet] Add nullability to CookieJar
1 parent be90e2f commit 1b25a75

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

dotnet/src/webdriver/CookieJar.cs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,72 @@
2121
using System.Collections.Generic;
2222
using System.Collections.ObjectModel;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium
2527
{
2628
/// <summary>
2729
/// Defines an interface allowing the user to manipulate cookies on the current page.
2830
/// </summary>
29-
internal class CookieJar : ICookieJar
31+
internal sealed class CookieJar : ICookieJar
3032
{
31-
private WebDriver driver;
33+
private readonly WebDriver driver;
3234

3335
/// <summary>
3436
/// Initializes a new instance of the <see cref="CookieJar"/> class.
3537
/// </summary>
3638
/// <param name="driver">The driver that is currently in use</param>
39+
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> is <see langword="null"/>.</exception>
3740
public CookieJar(WebDriver driver)
3841
{
39-
this.driver = driver;
42+
this.driver = driver ?? throw new ArgumentNullException(nameof(driver));
4043
}
4144

4245
/// <summary>
4346
/// Gets all cookies defined for the current page.
4447
/// </summary>
4548
public ReadOnlyCollection<Cookie> AllCookies
4649
{
47-
get { return this.GetAllCookies(); }
50+
get
51+
{
52+
object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>()).Value;
53+
54+
try
55+
{
56+
List<Cookie> toReturn = new List<Cookie>();
57+
if (returned is object?[] cookies)
58+
{
59+
foreach (object? rawCookie in cookies)
60+
{
61+
if (rawCookie != null)
62+
{
63+
Cookie newCookie = Cookie.FromDictionary((Dictionary<string, object?>)rawCookie);
64+
toReturn.Add(newCookie);
65+
}
66+
}
67+
}
68+
69+
return new ReadOnlyCollection<Cookie>(toReturn);
70+
}
71+
catch (Exception e)
72+
{
73+
throw new WebDriverException("Unexpected problem getting cookies", e);
74+
}
75+
}
4876
}
4977

5078
/// <summary>
5179
/// Method for creating a cookie in the browser
5280
/// </summary>
5381
/// <param name="cookie"><see cref="Cookie"/> that represents a cookie in the browser</param>
82+
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
5483
public void AddCookie(Cookie cookie)
5584
{
85+
if (cookie is null)
86+
{
87+
throw new ArgumentNullException(nameof(cookie));
88+
}
89+
5690
Dictionary<string, object> parameters = new Dictionary<string, object>();
5791
parameters.Add("cookie", cookie);
5892
this.driver.InternalExecute(DriverCommand.AddCookie, parameters);
@@ -62,18 +96,21 @@ public void AddCookie(Cookie cookie)
6296
/// Delete the cookie by passing in the name of the cookie
6397
/// </summary>
6498
/// <param name="name">The name of the cookie that is in the browser</param>
65-
public void DeleteCookieNamed(string name)
99+
public void DeleteCookieNamed(string? name)
66100
{
67-
Dictionary<string, object> parameters = new Dictionary<string, object>();
68-
parameters.Add("name", name);
69-
this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
101+
if (name is not null)
102+
{
103+
Dictionary<string, object> parameters = new Dictionary<string, object>();
104+
parameters.Add("name", name);
105+
this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
106+
}
70107
}
71108

72109
/// <summary>
73110
/// Delete a cookie in the browser by passing in a copy of a cookie
74111
/// </summary>
75112
/// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
76-
public void DeleteCookie(Cookie cookie)
113+
public void DeleteCookie(Cookie? cookie)
77114
{
78115
if (cookie != null)
79116
{
@@ -94,10 +131,10 @@ public void DeleteAllCookies()
94131
/// </summary>
95132
/// <param name="name">name of the cookie that needs to be returned</param>
96133
/// <returns>A Cookie from the name</returns>
97-
public Cookie GetCookieNamed(string name)
134+
public Cookie? GetCookieNamed(string? name)
98135
{
99-
Cookie cookieToReturn = null;
100-
if (name != null)
136+
Cookie? cookieToReturn = null;
137+
if (name is not null)
101138
{
102139
ReadOnlyCollection<Cookie> allCookies = this.AllCookies;
103140
foreach (Cookie currentCookie in allCookies)
@@ -112,37 +149,5 @@ public Cookie GetCookieNamed(string name)
112149

113150
return cookieToReturn;
114151
}
115-
116-
/// <summary>
117-
/// Method for getting a Collection of Cookies that are present in the browser
118-
/// </summary>
119-
/// <returns>ReadOnlyCollection of Cookies in the browser</returns>
120-
private ReadOnlyCollection<Cookie> GetAllCookies()
121-
{
122-
List<Cookie> toReturn = new List<Cookie>();
123-
object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>()).Value;
124-
125-
try
126-
{
127-
object[] cookies = returned as object[];
128-
if (cookies != null)
129-
{
130-
foreach (object rawCookie in cookies)
131-
{
132-
Dictionary<string, object> cookieDictionary = rawCookie as Dictionary<string, object>;
133-
if (rawCookie != null)
134-
{
135-
toReturn.Add(Cookie.FromDictionary(cookieDictionary));
136-
}
137-
}
138-
}
139-
140-
return new ReadOnlyCollection<Cookie>(toReturn);
141-
}
142-
catch (Exception e)
143-
{
144-
throw new WebDriverException("Unexpected problem getting cookies", e);
145-
}
146-
}
147152
}
148153
}

dotnet/src/webdriver/ICookieJar.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System;
2021
using System.Collections.ObjectModel;
2122

23+
#nullable enable
24+
2225
namespace OpenQA.Selenium
2326
{
2427
/// <summary>
@@ -35,6 +38,7 @@ public interface ICookieJar
3538
/// Adds a cookie to the current page.
3639
/// </summary>
3740
/// <param name="cookie">The <see cref="Cookie"/> object to be added.</param>
41+
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
3842
void AddCookie(Cookie cookie);
3943

4044
/// <summary>
@@ -43,19 +47,19 @@ public interface ICookieJar
4347
/// <param name="name">The name of the cookie to retrieve.</param>
4448
/// <returns>The <see cref="Cookie"/> containing the name. Returns <see langword="null"/>
4549
/// if no cookie with the specified name is found.</returns>
46-
Cookie GetCookieNamed(string name);
50+
Cookie? GetCookieNamed(string? name);
4751

4852
/// <summary>
4953
/// Deletes the specified cookie from the page.
5054
/// </summary>
5155
/// <param name="cookie">The <see cref="Cookie"/> to be deleted.</param>
52-
void DeleteCookie(Cookie cookie);
56+
void DeleteCookie(Cookie? cookie);
5357

5458
/// <summary>
5559
/// Deletes the cookie with the specified name from the page.
5660
/// </summary>
5761
/// <param name="name">The name of the cookie to be deleted.</param>
58-
void DeleteCookieNamed(string name);
62+
void DeleteCookieNamed(string? name);
5963

6064
/// <summary>
6165
/// Deletes all cookies from the page.

0 commit comments

Comments
 (0)