Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send max-age header as well as expires if it is set #107

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ If the _value_ is omitted, an outbound header with an expired date is used to de

If the _options_ object is provided, it will be used to generate the outbound cookie header as follows:

* `maxAge`: a number representing the milliseconds from `Date.now()` for expiry
* `maxAge`: a number representing the milliseconds from `Date.now()` for expiry. If this and `expires` are both set this will take precedence. This value will be converted to seconds before being added to the Set-Cookie header
* `expires`: a `Date` object indicating the cookie's expiration date (expires at the end of session by default).
* `path`: a string indicating the path of the cookie (`/` by default).
* `domain`: a string indicating the domain of the cookie (no default).
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Cookie.prototype.toHeader = function() {

if (this.maxAge) this.expires = new Date(Date.now() + this.maxAge);

if (this.maxAge ) header += "; max-age=" + Math.round(this.maxAge / 1000)
if (this.path ) header += "; path=" + this.path
if (this.expires ) header += "; expires=" + this.expires.toUTCString()
if (this.domain ) header += "; domain=" + this.domain
Expand Down
15 changes: 13 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,26 @@ describe('new Cookies(req, res, [options])', function () {
.end(done)
})

it('should not set the "maxAge" attribute', function (done) {
it('should set the "maxAge" attribute', function (done) {
request(createServer(function (req, res, cookies) {
cookies.set('foo', 'bar', { maxAge: 86400000 })
res.end()
}))
.get('/')
.expect(200)
.expect(shouldSetCookieWithAttribute('foo', 'expires'))
.expect(shouldSetCookieWithoutAttribute('foo', 'maxAge'))
.expect(shouldSetCookieWithAttribute('foo', 'max-age'))
.end(done)
})

it('should convert the "maxAge" attribute to seconds', function (done) {
request(createServer(function (req, res, cookies) {
cookies.set('foo', 'bar', { maxAge: 86400001 })
res.end()
}))
.get('/')
.expect(200)
.expect(shouldSetCookieWithAttributeAndValue('foo', 'max-age', '86400'))
.end(done)
})
})
Expand Down