Skip to content

Commit e3c84a0

Browse files
harsha509sandeepsuryaprasad
authored andcommitted
[JS] Add detailed error message for invalid cookie name validation in getCookie method
1 parent 551467f commit e3c84a0

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

javascript/node/selenium-webdriver/lib/webdriver.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,25 +1957,24 @@ class Options {
19571957
* WebDriver wire protocol.
19581958
*
19591959
* @param {string} name The name of the cookie to retrieve.
1960+
* @throws {InvalidArgumentError} - If the cookie name is empty or invalid.
19601961
* @return {!Promise<?Options.Cookie>} A promise that will be resolved
19611962
* with the named cookie
19621963
* @throws {error.NoSuchCookieError} if there is no such cookie.
19631964
*/
19641965
async getCookie(name) {
1966+
// Validate the cookie name is non-empty and properly trimmed.
1967+
if (!name?.trim()) {
1968+
throw new error.InvalidArgumentError('Cookie name cannot be empty')
1969+
}
1970+
19651971
try {
19661972
const cookie = await this.driver_.execute(new command.Command(command.Name.GET_COOKIE).setParameter('name', name))
19671973
return cookie
19681974
} catch (err) {
19691975
if (!(err instanceof error.UnknownCommandError) && !(err instanceof error.UnsupportedOperationError)) {
19701976
throw err
19711977
}
1972-
1973-
const cookies = await this.getCookies()
1974-
for (let cookie of cookies) {
1975-
if (cookie && cookie['name'] === name) {
1976-
return cookie
1977-
}
1978-
}
19791978
return null
19801979
}
19811980
}

javascript/node/selenium-webdriver/test/cookie_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ suite(function (env) {
5454
})
5555
})
5656

57+
it('throw error if name is null', async function () {
58+
const cookie = createCookieSpec()
59+
await driver.manage().addCookie(cookie)
60+
await assert.rejects(async () => await driver.manage().getCookie(null), {
61+
name: 'InvalidArgumentError',
62+
message: `Cookie name cannot be empty`,
63+
})
64+
})
65+
5766
it('can get all cookies', async function () {
5867
const cookie1 = createCookieSpec()
5968
const cookie2 = createCookieSpec()

0 commit comments

Comments
 (0)