Skip to content

Commit

Permalink
Add: Adding new cookie sameSite (c-nodejs) (#7901)
Browse files Browse the repository at this point in the history
Adding new cookie sameSite

Signed-off-by: Sri Harsha <[email protected]>

Co-authored-by: Christian Bromann <[email protected]>
Co-authored-by: Tomer <[email protected]>
  • Loading branch information
3 people authored Mar 4, 2020
1 parent dbeafd2 commit 7cbe4ae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
21 changes: 19 additions & 2 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ class Options {
* invalid.
* @throws {TypeError} if `spec` is not a cookie object.
*/
addCookie({name, value, path, domain, secure, httpOnly, expiry}) {
addCookie({name, value, path, domain, secure, httpOnly, expiry, sameSite}) {
// We do not allow '=' or ';' in the name.
if (/[;=]/.test(name)) {
throw new error.InvalidArgumentError(
Expand All @@ -1131,6 +1131,11 @@ class Options {
expiry = Math.floor(date.getTime() / 1000);
}

if(sameSite && sameSite !== 'Strict' && sameSite !== 'Lax') {
throw new error.InvalidArgumentError(
`Invalid sameSite cookie value '${sameSite}'. It should be either "Lax" (or) "Strict" `);
}

return this.driver_.execute(
new command.Command(command.Name.ADD_COOKIE).
setParameter('cookie', {
Expand All @@ -1140,7 +1145,8 @@ class Options {
'domain': domain,
'secure': !!secure,
'httpOnly': !!httpOnly,
'expiry': expiry
'expiry': expiry,
'sameSite': sameSite
}));
}

Expand Down Expand Up @@ -1399,6 +1405,17 @@ Options.Cookie.prototype.httpOnly;
Options.Cookie.prototype.expiry;


/**
* When the cookie applies to a SameSite policy.
*
* When {@linkplain Options#addCookie() adding a cookie}, this may be specified
* as a {@link string} object which is either 'Lax' or 'Strict'.
*
*
* @type {(!Date|number|undefined)}
*/
Options.Cookie.prototype.sameSite;

/**
* An interface for managing the current window.
*
Expand Down
26 changes: 26 additions & 0 deletions javascript/node/selenium-webdriver/test/cookie_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ suite(function(env) {
await driver.sleep(expirationDelay);
await assertHasCookies();
});

it('can add same site cookie property to `Strict`', async function() {
let cookie = createSameSiteCookieSpec('Strict');
let childUrl = fileserver.whereIs('child/childPage.html');
await driver.get(childUrl);
await driver.manage().addCookie(cookie);
const actual = await driver.manage().getCookie(cookie.name);
assert.equal(actual.sameSite, "Strict");
});

it('can add same site cookie property to `Lax`', async function() {
let cookie = createSameSiteCookieSpec('Lax');
let childUrl = fileserver.whereIs('child/childPage.html');
await driver.get(childUrl);
await driver.manage().addCookie(cookie);
const actualCookie = await driver.manage().getCookie(cookie.name);
assert.equal(actualCookie.sameSite, "Lax");
});
});

function createCookieSpec(opt_options) {
Expand All @@ -181,6 +199,14 @@ suite(function(env) {
return spec;
}

function createSameSiteCookieSpec(sameSiteVal) {
return {
name: getRandomString(),
value: getRandomString(),
sameSite: sameSiteVal
};
}

function buildCookieMap(cookies) {
var map = {};
cookies.forEach(function(cookie) {
Expand Down

0 comments on commit 7cbe4ae

Please sign in to comment.