Skip to content

Commit

Permalink
Implement option for setting cookies in highest available domain.
Browse files Browse the repository at this point in the history
One could also say lowest, I guess.
  • Loading branch information
cramforce committed Jan 17, 2016
1 parent 9e7bfec commit c7d22dc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,40 @@ export function getCookie(win, name) {
* @param {string} name
* @param {string} value
* @param {time} expirationTime
* @param {{highestAvailableDomain:boolean}=} opt_options
* - highestAvailableDomain: If true, set the cookie at the widest domain
* scope allowed by the browser. E.g. on example.com if we are currently
* on www.example.com.
*/
export function setCookie(win, name, value, expirationTime) {
export function setCookie(win, name, value, expirationTime, opt_options) {
if (opt_options && opt_options.highestAvailableDomain) {
const parts = win.location.hostname.split('.');
let domain = parts[parts.length - 1];
for (let i = parts.length - 2; i >= 0; i--) {
domain = parts[i] + '.' + domain;
trySetCookie(win, name, value, expirationTime, domain);
if (getCookie(win, name) == value) {
return;
}
}
} else {
trySetCookie(win, name, value, expirationTime, undefined);
}
}

/**
* Attempt to set a cookie with the given params.
*
* @param {!Window} win
* @param {string} name
* @param {string} value
* @param {time} expirationTime
* @param {string|undefined} domain
*/
function trySetCookie(win, name, value, expirationTime, domain) {
win.document.cookie = encodeURIComponent(name) + '=' +
encodeURIComponent(value) +
'; path=/' +
(domain ? '; domain=' + domain : '') +
'; expires=' + new Date(expirationTime).toUTCString();
}
34 changes: 34 additions & 0 deletions test/functional/test-cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,38 @@ describe('getCookie', () => {
expect(doc.cookie).to.equal(
'c%261=v%261; path=/; expires=Fri, 13 Nov 2015 02:52:39 GMT');
});

it('should write the cookie to the right domain', () => {
function test(hostname, targetDomain, opt_noset) {
let cookie;
const doc = {
set cookie(val) {
if (val.indexOf('; domain=' + targetDomain) != -1) {
cookie = val;
}
},
get cookie() {
return cookie;
}
};
setCookie({document: doc, location: {hostname: hostname}},
'c&1', 'v&1', 1447383159853, {
highestAvailableDomain: true
});
if (opt_noset) {
expect(cookie).to.be.undefined;
} else {
expect(cookie).to.equal(
'c%261=v%261; path=/; domain=' + targetDomain +
'; expires=Fri, 13 Nov 2015 02:52:39 GMT');
}
}
test('www.example.com', 'example.com');
test('123.www.example.com', 'example.com');
test('example.com', 'example.com');
test('www.example.com', 'www.example.com');
test('123.www.example.com', '123.www.example.com');
test('www.example.net', 'example.com', true);
test('example.net', 'example.com', true);
});
});

0 comments on commit c7d22dc

Please sign in to comment.