Skip to content
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
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,27 @@ function Cookie(name, value, attrs) {
throw new TypeError('argument name is invalid');
}

if (value && (!fieldContentRegExp.test(value) || RESTRICTED_VALUE_CHARS_REGEXP.test(value))) {
throw new TypeError('argument value is invalid');
this.name = name

var isNullValue = value == null

if (value == null) {
this.value = ''
this.maxAge = null
this.expires = new Date(0)
} else {
this.value = String(value)
}

this.name = name
this.value = value || ""
if (this.value && (!fieldContentRegExp.test(this.value) || RESTRICTED_VALUE_CHARS_REGEXP.test(this.value))) {
throw new TypeError('argument value is invalid');
}

for (var name in attrs) {
this[name] = attrs[name]
}

if (!this.value) {
if (isNullValue) {
this.expires = new Date(0)
this.maxAge = null
}
Expand Down Expand Up @@ -208,7 +217,7 @@ Cookie.prototype.toString = function() {
Cookie.prototype.toHeader = function() {
var header = this.toString()

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

if (this.path ) header += "; path=" + this.path
if (this.expires ) header += "; expires=" + this.expires.toUTCString()
Expand Down
27 changes: 27 additions & 0 deletions test/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ describe('new Cookie(name, value, [options])', function () {
assert.equal(cookie.constructor, cookies.Cookie)
})

it('should preserve zero value', function () {
var cookie = new cookies.Cookie('foo', 0)
assert.strictEqual(cookie.toHeader(), 'foo=0; path=/; httponly')
})

it('should preserve empty value', function () {
var cookie = new cookies.Cookie('foo', '')
assert.strictEqual(cookie.toHeader(), 'foo=; path=/; httponly')
})

it('should throw on invalid name', function () {
assert.throws(function () {
new cookies.Cookie('foo\n', 'bar')
Expand Down Expand Up @@ -79,6 +89,23 @@ describe('new Cookie(name, value, [options])', function () {
new cookies.Cookie('foo', 'bar', { maxAge: NaN })
}, /option maxAge is invalid/)
})

it('should set expires for zero maxAge', function () {
var before = Date.now()
var cookie = new cookies.Cookie('foo', 'bar', { maxAge: 0 })
var header = cookie.toHeader()
var parts = header.split('; ')
var expiresPart = parts.filter(function (part) {
return part.indexOf('expires=') === 0
})[0]
assert.ok(expiresPart, 'should include expires attribute')

var match = /expires=(.+)/.exec(expiresPart)
var expires = Date.parse(match[1])

assert.ok(match)
assert.ok(expires >= (before - 1000))
})
})

describe('partitioned', function () {
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ describe('new Cookies(req, res, [options])', function () {
.end(done)
})

it('should set numeric zero value', function (done) {
request(createServer(setCookieHandler('foo', 0)))
.get('/')
.expect(200)
.expect(shouldSetCookieToValue('foo', '0'))
.end(done)
})

it('should set empty string value', function (done) {
request(createServer(setCookieHandler('foo', '')))
.get('/')
.expect(200)
.expect(shouldSetCookieCount(1))
.expect(shouldSetCookieToValue('foo', ''))
.expect(shouldSetCookieWithoutAttribute('foo', 'expires'))
.end(done)
})

describe('when value is falsy', function () {
it('should delete cookie', function (done) {
request(createServer(setCookieHandler('foo', null)))
Expand Down