Skip to content

Commit

Permalink
[dotnet] Guard for cookie deletion when name is empty (#15074)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko authored Jan 13, 2025
1 parent 5b02e61 commit bd225cc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
13 changes: 7 additions & 6 deletions dotnet/src/webdriver/CookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ public void AddCookie(Cookie cookie)
/// Delete the cookie by passing in the name of the cookie
/// </summary>
/// <param name="name">The name of the cookie that is in the browser</param>
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="name"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public void DeleteCookieNamed(string name)
{
if (name is null)
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentNullException(nameof(name));
throw new ArgumentException("Cookie name cannot be null or empty", nameof(name));
}

Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("name", name);
Dictionary<string, object> parameters = new() { { "name", name } };

driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
}

Expand Down Expand Up @@ -115,11 +115,12 @@ public void DeleteAllCookies()
/// </summary>
/// <param name="name">name of the cookie that needs to be returned</param>
/// <returns>A Cookie from the name; or <see langword="null"/> if not found.</returns>
/// <exception cref="ArgumentException">If <paramref name="name"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public Cookie? GetCookieNamed(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Cookie name cannot be empty", nameof(name));
throw new ArgumentException("Cookie name cannot be null or empty", nameof(name));
}

try
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/ICookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public interface ICookieJar
/// Deletes the cookie with the specified name from the page.
/// </summary>
/// <param name="name">The name of the cookie to be deleted.</param>
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="name"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
void DeleteCookieNamed(string name);

/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions dotnet/test/common/CookieImplementationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,28 @@ public void DeleteAllCookiesDifferentUrls()
AssertCookieIsPresentWithName(cookie2.Name);
}

[Test]
[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
public void ShouldThrowWhenGetInvalidCookieByName(string cookieName)
{
var getCookieAction = () => driver.Manage().Cookies.GetCookieNamed(cookieName);

Assert.That(getCookieAction, Throws.ArgumentException);
}

[Test]
[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
public void ShouldThrowWhenDeleteInvalidCookieByName(string cookieName)
{
var deleteCookieAction = () => driver.Manage().Cookies.DeleteCookieNamed(cookieName);

Assert.That(deleteCookieAction, Throws.ArgumentException);
}

//------------------------------------------------------------------
// Tests below here are not included in the Java test suite
//------------------------------------------------------------------
Expand Down

0 comments on commit bd225cc

Please sign in to comment.