Skip to content

Commit

Permalink
tests: fix assert call arguments
Browse files Browse the repository at this point in the history
fixes #124
  • Loading branch information
dougwilson committed Apr 19, 2021
1 parent eb95ed7 commit 0b51953
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 121 deletions.
57 changes: 25 additions & 32 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,55 @@ test('argument validation', function() {
});

test('basic', function() {
assert.deepEqual({ foo: 'bar' }, cookie.parse('foo=bar'));
assert.deepEqual({ foo: '123' }, cookie.parse('foo=123'));
assert.deepEqual(cookie.parse('foo=bar'), { foo: 'bar' })
assert.deepEqual(cookie.parse('foo=123'), { foo: '123' })
});

test('ignore spaces', function() {
assert.deepEqual({ FOO: 'bar', baz: 'raz' },
cookie.parse('FOO = bar; baz = raz'));
assert.deepEqual(cookie.parse('FOO = bar; baz = raz'),
{ FOO: 'bar', baz: 'raz' })
});

test('escaping', function() {
assert.deepEqual({ foo: 'bar=123456789&name=Magic+Mouse' },
cookie.parse('foo="bar=123456789&name=Magic+Mouse"'));
assert.deepEqual(cookie.parse('foo="bar=123456789&name=Magic+Mouse"'),
{ foo: 'bar=123456789&name=Magic+Mouse' })

assert.deepEqual({ email: ' ",;/' },
cookie.parse('email=%20%22%2c%3b%2f'));
assert.deepEqual(cookie.parse('email=%20%22%2c%3b%2f'), { email: ' ",;/' })
});

test('ignore escaping error and return original value', function() {
assert.deepEqual({ foo: '%1', bar: 'bar' }, cookie.parse('foo=%1;bar=bar'));
assert.deepEqual(cookie.parse('foo=%1;bar=bar'), { foo: '%1', bar: 'bar' })
});

test('ignore non values', function() {
assert.deepEqual({ foo: '%1', bar: 'bar' }, cookie.parse('foo=%1;bar=bar;HttpOnly;Secure'));
assert.deepEqual(cookie.parse('foo=%1;bar=bar;HttpOnly;Secure'),
{ foo: '%1', bar: 'bar' })
});

test('unencoded', function() {
assert.deepEqual({ foo: 'bar=123456789&name=Magic+Mouse' },
cookie.parse('foo="bar=123456789&name=Magic+Mouse"',{
decode: function(value) { return value; }
}));
assert.deepEqual(cookie.parse('foo="bar=123456789&name=Magic+Mouse"', {
decode: function (v) { return v }
}), { foo: 'bar=123456789&name=Magic+Mouse' })

assert.deepEqual({ email: '%20%22%2c%3b%2f' },
cookie.parse('email=%20%22%2c%3b%2f',{
decode: function(value) { return value; }
}));
assert.deepEqual(cookie.parse('email=%20%22%2c%3b%2f', {
decode: function (v) { return v }
}), { email: '%20%22%2c%3b%2f' })
});

test('dates', function() {
assert.deepEqual({ priority: 'true', Path: '/', expires: 'Wed, 29 Jan 2014 17:43:25 GMT' },
cookie.parse('priority=true; expires=Wed, 29 Jan 2014 17:43:25 GMT; Path=/',{
decode: function(value) { return value; }
}));
assert.deepEqual(cookie.parse('priority=true; expires=Wed, 29 Jan 2014 17:43:25 GMT; Path=/', {
decode: function (v) { return v }
}), { priority: 'true', Path: '/', expires: 'Wed, 29 Jan 2014 17:43:25 GMT' })
});

test('missing value', function() {
assert.deepEqual({ bar: '1', fizz: '', buzz: '2' },
cookie.parse('foo; bar=1; fizz= ; buzz=2',{
decode: function(value) { return value; }
}));
assert.deepEqual(cookie.parse('foo; bar=1; fizz= ; buzz=2', {
decode: function (v) { return v }
}), { bar: '1', fizz: '', buzz: '2' })
});

test('assign only once', function() {
assert.deepEqual({ foo: '%1', bar: 'bar' },
cookie.parse('foo=%1;bar=bar;foo=boo'));
assert.deepEqual({ foo: 'false', bar: 'bar' },
cookie.parse('foo=false;bar=bar;foo=true'));
assert.deepEqual({ foo: '', bar: 'bar' },
cookie.parse('foo=;bar=bar;foo=boo'));
assert.deepEqual(cookie.parse('foo=%1;bar=bar;foo=boo'), { foo: '%1', bar: 'bar' })
assert.deepEqual(cookie.parse('foo=false;bar=bar;foo=true'), { foo: 'false', bar: 'bar' })
assert.deepEqual(cookie.parse('foo=;bar=bar;foo=boo'), { foo: '', bar: 'bar' })
});
121 changes: 32 additions & 89 deletions test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,35 @@ var cookie = require('..');
suite('serialize');

test('basic', function() {
assert.equal('foo=bar', cookie.serialize('foo', 'bar'));
assert.equal('foo=bar%20baz', cookie.serialize('foo', 'bar baz'));
assert.equal('foo=', cookie.serialize('foo', ''));
assert.equal(cookie.serialize('foo', 'bar'), 'foo=bar')
assert.equal(cookie.serialize('foo', 'bar baz'), 'foo=bar%20baz')
assert.equal(cookie.serialize('foo', ''), 'foo=')
assert.throws(cookie.serialize.bind(cookie, 'foo\n', 'bar'), /argument name is invalid/);
assert.throws(cookie.serialize.bind(cookie, 'foo\u280a', 'bar'), /argument name is invalid/);
assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {encode: 42}), /option encode is invalid/);
});

test('path', function() {
assert.equal('foo=bar; Path=/', cookie.serialize('foo', 'bar', {
path: '/'
}));

assert.equal(cookie.serialize('foo', 'bar', { path: '/' }), 'foo=bar; Path=/')
assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
path: '/\n'
}), /option path is invalid/);
});

test('secure', function() {
assert.equal('foo=bar; Secure', cookie.serialize('foo', 'bar', {
secure: true
}));

assert.equal('foo=bar', cookie.serialize('foo', 'bar', {
secure: false
}));
assert.equal(cookie.serialize('foo', 'bar', { secure: true }), 'foo=bar; Secure')
assert.equal(cookie.serialize('foo', 'bar', { secure: false }), 'foo=bar')
});

test('domain', function() {
assert.equal('foo=bar; Domain=example.com', cookie.serialize('foo', 'bar', {
domain: 'example.com'
}));

assert.equal(cookie.serialize('foo', 'bar', { domain: 'example.com' }), 'foo=bar; Domain=example.com')
assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
domain: 'example.com\n'
}), /option domain is invalid/);
});

test('httpOnly', function() {
assert.equal('foo=bar; HttpOnly', cookie.serialize('foo', 'bar', {
httpOnly: true
}));
assert.equal(cookie.serialize('foo', 'bar', { httpOnly: true }), 'foo=bar; HttpOnly')
});

test('maxAge', function() {
Expand All @@ -63,100 +50,56 @@ test('maxAge', function() {
})
}, /option maxAge is invalid/)

assert.equal('foo=bar; Max-Age=1000', cookie.serialize('foo', 'bar', {
maxAge: 1000
}));

assert.equal('foo=bar; Max-Age=1000', cookie.serialize('foo', 'bar', {
maxAge: '1000'
}));

assert.equal('foo=bar; Max-Age=0', cookie.serialize('foo', 'bar', {
maxAge: 0
}));

assert.equal('foo=bar; Max-Age=0', cookie.serialize('foo', 'bar', {
maxAge: '0'
}));

assert.equal('foo=bar', cookie.serialize('foo', 'bar', {
maxAge: null
}));

assert.equal('foo=bar', cookie.serialize('foo', 'bar', {
maxAge: undefined
}));

assert.equal('foo=bar; Max-Age=3', cookie.serialize('foo', 'bar', {
maxAge: 3.14
}));
assert.equal(cookie.serialize('foo', 'bar', { maxAge: 1000 }), 'foo=bar; Max-Age=1000')
assert.equal(cookie.serialize('foo', 'bar', { maxAge: '1000' }), 'foo=bar; Max-Age=1000')
assert.equal(cookie.serialize('foo', 'bar', { maxAge: 0 }), 'foo=bar; Max-Age=0')
assert.equal(cookie.serialize('foo', 'bar', { maxAge: '0' }), 'foo=bar; Max-Age=0')
assert.equal(cookie.serialize('foo', 'bar', { maxAge: null }), 'foo=bar')
assert.equal(cookie.serialize('foo', 'bar', { maxAge: undefined }), 'foo=bar')
assert.equal(cookie.serialize('foo', 'bar', { maxAge: 3.14 }), 'foo=bar; Max-Age=3')
});

test('expires', function() {
assert.equal('foo=bar; Expires=Sun, 24 Dec 2000 10:30:59 GMT', cookie.serialize('foo', 'bar', {
assert.equal(cookie.serialize('foo', 'bar', {
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900))
}));
}), 'foo=bar; Expires=Sun, 24 Dec 2000 10:30:59 GMT')

assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
expires: Date.now()
}), /option expires is invalid/);
});

test('sameSite', function() {
assert.equal('foo=bar; SameSite=Strict', cookie.serialize('foo', 'bar', {
sameSite: true
}));

assert.equal('foo=bar; SameSite=Strict', cookie.serialize('foo', 'bar', {
sameSite: 'Strict'
}));

assert.equal('foo=bar; SameSite=Strict', cookie.serialize('foo', 'bar', {
sameSite: 'strict'
}));

assert.equal('foo=bar; SameSite=Lax', cookie.serialize('foo', 'bar', {
sameSite: 'Lax'
}));

assert.equal('foo=bar; SameSite=Lax', cookie.serialize('foo', 'bar', {
sameSite: 'lax'
}));

assert.equal('foo=bar; SameSite=None', cookie.serialize('foo', 'bar', {
sameSite: 'None'
}));

assert.equal('foo=bar; SameSite=None', cookie.serialize('foo', 'bar', {
sameSite: 'none'
}));

assert.equal('foo=bar', cookie.serialize('foo', 'bar', {
sameSite: false
}));
assert.equal(cookie.serialize('foo', 'bar', { sameSite: true }), 'foo=bar; SameSite=Strict')
assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'Strict' }), 'foo=bar; SameSite=Strict')
assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'strict' }), 'foo=bar; SameSite=Strict')
assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'Lax' }), 'foo=bar; SameSite=Lax')
assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'lax' }), 'foo=bar; SameSite=Lax')
assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'None' }), 'foo=bar; SameSite=None')
assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'none' }), 'foo=bar; SameSite=None')
assert.equal(cookie.serialize('foo', 'bar', { sameSite: false }), 'foo=bar')

assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
sameSite: 'foo'
}), /option sameSite is invalid/);
});

test('escaping', function() {
assert.deepEqual('cat=%2B%20', cookie.serialize('cat', '+ '));
assert.deepEqual(cookie.serialize('cat', '+ '), 'cat=%2B%20')
});

test('parse->serialize', function() {
assert.deepEqual(cookie.parse(cookie.serialize('cat', 'foo=123&name=baz five')),
{ cat: 'foo=123&name=baz five' })

assert.deepEqual({ cat: 'foo=123&name=baz five' }, cookie.parse(
cookie.serialize('cat', 'foo=123&name=baz five')));

assert.deepEqual({ cat: ' ";/' }, cookie.parse(
cookie.serialize('cat', ' ";/')));
assert.deepEqual(cookie.parse(cookie.serialize('cat', ' ";/')),
{ cat: ' ";/' })
});

test('unencoded', function() {
assert.deepEqual('cat=+ ', cookie.serialize('cat', '+ ', {
assert.deepEqual(cookie.serialize('cat', '+ ', {
encode: function(value) { return value; }
}));
}), 'cat=+ ')

assert.throws(cookie.serialize.bind(cookie, 'cat', '+ \n', {
encode: function(value) { return value; }
Expand Down

0 comments on commit 0b51953

Please sign in to comment.