-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: refactor to standard mocha api
- Loading branch information
1 parent
042073f
commit f364585
Showing
3 changed files
with
246 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.