From 2b60c9fd879f13e757ed50bce94f40ab403f02d6 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Mon, 13 Jan 2025 21:02:40 +0300 Subject: [PATCH 1/3] [dotnet] Guard for cookie deletion when name is empty --- dotnet/src/webdriver/CookieJar.cs | 11 ++++++----- dotnet/src/webdriver/ICookieJar.cs | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index 0f2695b96a2de..b8e7b31795573 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -74,16 +74,16 @@ public void AddCookie(Cookie cookie) /// Delete the cookie by passing in the name of the cookie /// /// The name of the cookie that is in the browser - /// If is . + /// If is or . 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 empty", nameof(name)); } - Dictionary parameters = new Dictionary(); - parameters.Add("name", name); + Dictionary parameters = new() { { "name", name } }; + driver.InternalExecute(DriverCommand.DeleteCookie, parameters); } @@ -115,6 +115,7 @@ public void DeleteAllCookies() /// /// name of the cookie that needs to be returned /// A Cookie from the name; or if not found. + /// If is or . public Cookie? GetCookieNamed(string name) { if (string.IsNullOrWhiteSpace(name)) diff --git a/dotnet/src/webdriver/ICookieJar.cs b/dotnet/src/webdriver/ICookieJar.cs index f9eb69a4824bc..b73a3085c6aba 100644 --- a/dotnet/src/webdriver/ICookieJar.cs +++ b/dotnet/src/webdriver/ICookieJar.cs @@ -61,7 +61,7 @@ public interface ICookieJar /// Deletes the cookie with the specified name from the page. /// /// The name of the cookie to be deleted. - /// If is . + /// If is or . void DeleteCookieNamed(string name); /// From 3714ad6a75c57751921971df1f5b492688a195ab Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Mon, 13 Jan 2025 21:08:39 +0300 Subject: [PATCH 2/3] Null or empty in exception message --- dotnet/src/webdriver/CookieJar.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index b8e7b31795573..470d81653cdff 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -79,7 +79,7 @@ public void DeleteCookieNamed(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)); } Dictionary parameters = new() { { "name", name } }; @@ -120,7 +120,7 @@ public void DeleteAllCookies() { 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 From 5cb8e3e48522584fafc33ec05fdc639a5b46b254 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Mon, 13 Jan 2025 22:37:43 +0300 Subject: [PATCH 3/3] Add tests --- .../test/common/CookieImplementationTest.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/dotnet/test/common/CookieImplementationTest.cs b/dotnet/test/common/CookieImplementationTest.cs index a329895d79b7d..2cc8a3c5cc2e8 100644 --- a/dotnet/test/common/CookieImplementationTest.cs +++ b/dotnet/test/common/CookieImplementationTest.cs @@ -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 //------------------------------------------------------------------