diff --git a/README.md b/README.md index f7c12f8..6c5256c 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/index.js b/index.js index 9c468ae..f98ff8a 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/test.js b/test/test.js index 91dfb42..be82f55 100644 --- a/test/test.js +++ b/test/test.js @@ -297,7 +297,7 @@ 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() @@ -305,7 +305,18 @@ describe('new Cookies(req, res, [options])', function () { .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) }) })