Skip to content

Commit

Permalink
tests: refactor to standard mocha api
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Mar 30, 2022
1 parent 042073f commit f364585
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 182 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"eslint-plugin-markdown": "2.2.1",
"mocha": "9.2.2",
"nyc": "15.1.0",
"safe-buffer": "5.2.1",
"top-sites": "1.1.89"
},
"files": [
Expand All @@ -33,7 +34,7 @@
"scripts": {
"bench": "node benchmark/index.js",
"lint": "eslint .",
"test": "mocha --reporter spec --bail --check-leaks --ui qunit test/",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
"test-cov": "nyc --reporter=html --reporter=text npm test",
"update-bench": "node scripts/update-benchmark.js",
Expand Down
115 changes: 55 additions & 60 deletions test/parse.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,60 @@

var assert = require('assert');
var Buffer = require('safe-buffer').Buffer

var cookie = require('..');

suite('parse');

test('argument validation', function() {
assert.throws(cookie.parse.bind(), /argument str must be a string/);
assert.throws(cookie.parse.bind(null, 42), /argument str must be a string/);
});

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

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

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

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

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

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

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

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(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(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(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' })
});
describe('cookie.parse(str)', function () {
it('should throw with no arguments', function () {
assert.throws(cookie.parse.bind(), /argument str must be a string/)
})

it('should throw when not a string', function () {
assert.throws(cookie.parse.bind(null, 42), /argument str must be a string/)
})

it('should parse cookie string to object', function () {
assert.deepEqual(cookie.parse('foo=bar'), { foo: 'bar' })
assert.deepEqual(cookie.parse('foo=123'), { foo: '123' })
})

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

it('should parse cookie with empty value', function () {
assert.deepEqual(cookie.parse('foo= ; bar='), { foo: '', bar: '' })
})

it('should URL-decode values', function () {
assert.deepEqual(cookie.parse('foo="bar=123456789&name=Magic+Mouse"'),
{ foo: 'bar=123456789&name=Magic+Mouse' })

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

it('should return original value on escape error', function () {
assert.deepEqual(cookie.parse('foo=%1;bar=bar'), { foo: '%1', bar: 'bar' })
})

it('should ignore cookies without value', function () {
assert.deepEqual(cookie.parse('foo=bar;fizz ; buzz'), { foo: 'bar' })
})

it('should ignore duplicate cookies', function () {
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' })
})
})

describe('cookie.parse(str, options)', function () {
describe('with "decode" option', function () {
it('should specify alternative value decoder', function () {
assert.deepEqual(cookie.parse('foo="YmFy"', {
decode: function (v) { return Buffer.from(v, 'base64').toString() }
}), { foo: 'bar' })
})
})
})

0 comments on commit f364585

Please sign in to comment.