forked from petkaantonov/cookieparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
98 lines (82 loc) · 2.96 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var assert = require('assert');
var cookie = require('../js/cookieparser.js');
/*
Tests based on node-cookie
// MIT License
Copyright (C) Roman Shtylman <[email protected]>
https://github.com/defunctzombie/node-cookie/
*/
describe("parse", function() {
var foobar = { FOO: 'bar', baz: 'raz' };
specify('basic', function() {
assert.deepEqual({ foo: 'bar' }, cookie.parse('foo=bar'));
assert.deepEqual({ foo: '123' }, cookie.parse('foo=123'));
});
specify("basic multiple values", function() {
assert.deepEqual({ foo: 'bar', abc: "4" }, cookie.parse('abc=4; foo=bar'));
assert.deepEqual({ foo: 'bar', abc: "4" }, cookie.parse('foo=bar; abc=4'));
});
specify('ignore spaces', function() {
assert.deepEqual(
foobar,
cookie.parse('FOO = bar ; baz = raz')
);
assert.deepEqual(
foobar,
cookie.parse(" f ; FOO = bar; ; f ; baz = raz")
);
assert.deepEqual(
foobar,
cookie.parse(" f ; FOO = \"bar\" ; ; f ; baz =\"raz\" ")
);
assert.deepEqual(
foobar,
cookie.parse(" f ; FOO = \"bar\" ; ; f ; baz = \"raz\" ")
);
assert.deepEqual(
foobar,
cookie.parse(" f ; FOO = \"bar\"; ; f ; baz = \"raz\" ")
);
});
specify('escaping', function() {
assert.deepEqual({ foo: 'bar=123456789&name=Magic+Mouse' },
cookie.parse('foo="bar=123456789&name=Magic+Mouse"'));
assert.deepEqual({ email: ' ",;/' },
cookie.parse('email=%20%22%2c%3b%2f'));
});
specify('ignore escaping error and return original value', function() {
assert.deepEqual({ foo: '%1', bar: 'bar' }, cookie.parse('foo=%1;bar=bar'));
});
specify('ignore non values', function() {
assert.deepEqual({ foo: '%1', bar: 'bar' }, cookie.parse('foo=%1;bar=bar;HttpOnly;Secure'));
});
specify('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({ email: '%20%22%2c%3b%2f' },
cookie.parse('email=%20%22%2c%3b%2f',{
decode: function(value) { return value; }
}));
});
specify("Doesn't accept large input", function() {
var str = new Array(13500).join("n");
try {
cookie.parse(str);
assert.fail();
}
catch(e) {
assert(e instanceof RangeError);
}
});
specify("Doesn't accept wrong input", function() {
try {
cookie.parse({});
assert.fail();
}
catch(e) {
assert(e instanceof TypeError);
}
});
});