Skip to content

Commit 720ed63

Browse files
nvborisenkosandeepsuryaprasad
authored andcommitted
[dotnet] Guard for cookie deletion when name is empty (SeleniumHQ#15074)
1 parent 25dbd1b commit 720ed63

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

dotnet/src/webdriver/CookieJar.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ public void AddCookie(Cookie cookie)
7474
/// Delete the cookie by passing in the name of the cookie
7575
/// </summary>
7676
/// <param name="name">The name of the cookie that is in the browser</param>
77-
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
77+
/// <exception cref="ArgumentException">If <paramref name="name"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
7878
public void DeleteCookieNamed(string name)
7979
{
80-
if (name is null)
80+
if (string.IsNullOrWhiteSpace(name))
8181
{
82-
throw new ArgumentNullException(nameof(name));
82+
throw new ArgumentException("Cookie name cannot be null or empty", nameof(name));
8383
}
8484

85-
Dictionary<string, object> parameters = new Dictionary<string, object>();
86-
parameters.Add("name", name);
85+
Dictionary<string, object> parameters = new() { { "name", name } };
86+
8787
driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
8888
}
8989

@@ -115,11 +115,12 @@ public void DeleteAllCookies()
115115
/// </summary>
116116
/// <param name="name">name of the cookie that needs to be returned</param>
117117
/// <returns>A Cookie from the name; or <see langword="null"/> if not found.</returns>
118+
/// <exception cref="ArgumentException">If <paramref name="name"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
118119
public Cookie? GetCookieNamed(string name)
119120
{
120121
if (string.IsNullOrWhiteSpace(name))
121122
{
122-
throw new ArgumentException("Cookie name cannot be empty", nameof(name));
123+
throw new ArgumentException("Cookie name cannot be null or empty", nameof(name));
123124
}
124125

125126
try

dotnet/src/webdriver/ICookieJar.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public interface ICookieJar
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>
64+
/// <exception cref="ArgumentException">If <paramref name="name"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
6565
void DeleteCookieNamed(string name);
6666

6767
/// <summary>

dotnet/test/common/CookieImplementationTest.cs

+22
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,28 @@ public void DeleteAllCookiesDifferentUrls()
620620
AssertCookieIsPresentWithName(cookie2.Name);
621621
}
622622

623+
[Test]
624+
[TestCase(null)]
625+
[TestCase("")]
626+
[TestCase(" ")]
627+
public void ShouldThrowWhenGetInvalidCookieByName(string cookieName)
628+
{
629+
var getCookieAction = () => driver.Manage().Cookies.GetCookieNamed(cookieName);
630+
631+
Assert.That(getCookieAction, Throws.ArgumentException);
632+
}
633+
634+
[Test]
635+
[TestCase(null)]
636+
[TestCase("")]
637+
[TestCase(" ")]
638+
public void ShouldThrowWhenDeleteInvalidCookieByName(string cookieName)
639+
{
640+
var deleteCookieAction = () => driver.Manage().Cookies.DeleteCookieNamed(cookieName);
641+
642+
Assert.That(deleteCookieAction, Throws.ArgumentException);
643+
}
644+
623645
//------------------------------------------------------------------
624646
// Tests below here are not included in the Java test suite
625647
//------------------------------------------------------------------

0 commit comments

Comments
 (0)