Skip to content

Commit

Permalink
feat: Send max-age header as well as expires if it is set(#27)
Browse files Browse the repository at this point in the history
Co-authored-by: yancoding <junyan@junyandeMacBook-Pro.local>
  • Loading branch information
Junyan and yancoding committed Jun 22, 2020
1 parent 9d1e77a commit 4417dda
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/cookie.js
Expand Up @@ -44,8 +44,11 @@ class Cookie {
toHeader() {
let header = this.toString();
const attrs = this.attrs;
if (attrs.maxAge) attrs.expires = new Date(Date.now() + attrs.maxAge);
if (attrs.path) header += '; path=' + attrs.path;
if (attrs.maxAge) {
header += '; max-age=' + Math.round(attrs.maxAge / 1000);
attrs.expires = new Date(Date.now() + attrs.maxAge);
}
if (attrs.expires) header += '; expires=' + attrs.expires.toUTCString();
if (attrs.domain) header += '; domain=' + attrs.domain;
if (attrs.sameSite) header += '; samesite=' + (attrs.sameSite === true ? 'strict' : attrs.sameSite.toLowerCase());
Expand Down
2 changes: 1 addition & 1 deletion test/lib/cookie.test.js
Expand Up @@ -34,7 +34,7 @@ describe('test/lib/cookie.test.js', () => {
domain: 'eggjs.org',
path: '/',
httpOnly: true,
}).toHeader().match(/^name=value; path=\/; expires=(.*?)GMT; domain=eggjs\.org; secure; httponly$/));
}).toHeader().match(/^name=value; path=\/; max-age=1; expires=(.*?)GMT; domain=eggjs\.org; secure; httponly$/));
});

it('donnot set path when set path to null', () => {
Expand Down

0 comments on commit 4417dda

Please sign in to comment.