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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
'use strict';
const assert = require('assert');
const { format, parse } = require('../lib/extension');
describe('extension', () => {
describe('parse', () => {
it('parses a single extension', () => {
assert.deepStrictEqual(parse('foo'), {
foo: [{ __proto__: null }],
__proto__: null
});
});
it('parses params', () => {
assert.deepStrictEqual(parse('foo;bar;baz=1;bar=2'), {
foo: [{ bar: [true, '2'], baz: ['1'], __proto__: null }],
__proto__: null
});
});
it('parses multiple extensions', () => {
assert.deepStrictEqual(parse('foo,bar;baz,foo;baz'), {
foo: [{ __proto__: null }, { baz: [true], __proto__: null }],
bar: [{ baz: [true], __proto__: null }],
__proto__: null
});
});
it('parses quoted params', () => {
assert.deepStrictEqual(parse('foo;bar="hi"'), {
foo: [{ bar: ['hi'], __proto__: null }],
__proto__: null
});
assert.deepStrictEqual(parse('foo;bar="\\0"'), {
foo: [{ bar: ['0'], __proto__: null }],
__proto__: null
});
assert.deepStrictEqual(parse('foo;bar="b\\a\\z"'), {
foo: [{ bar: ['baz'], __proto__: null }],
__proto__: null
});
assert.deepStrictEqual(parse('foo;bar="b\\az";bar'), {
foo: [{ bar: ['baz', true], __proto__: null }],
__proto__: null
});
assert.throws(
() => parse('foo;bar="baz"qux'),
/^SyntaxError: Unexpected character at index 13$/
);
assert.throws(
() => parse('foo;bar="baz" qux'),
/^SyntaxError: Unexpected character at index 14$/
);
});
it('works with names that match `Object.prototype` property names', () => {
assert.deepStrictEqual(parse('hasOwnProperty, toString'), {
hasOwnProperty: [{ __proto__: null }],
toString: [{ __proto__: null }],
__proto__: null
});
assert.deepStrictEqual(parse('foo;constructor'), {
foo: [{ constructor: [true], __proto__: null }],
__proto__: null
});
});
it('ignores the optional white spaces', () => {
const header = 'foo; bar\t; \tbaz=1\t ; bar="1"\t\t, \tqux\t ;norf';
assert.deepStrictEqual(parse(header), {
foo: [{ bar: [true, '1'], baz: ['1'], __proto__: null }],
qux: [{ norf: [true], __proto__: null }],
__proto__: null
});
});
it('throws an error if a name is empty', () => {
[
[',', 0],
['foo,,', 4],
['foo, ,', 6],
['foo;=', 4],
['foo; =', 5],
['foo;;', 4],
['foo; ;', 5],
['foo;bar=,', 8],
['foo;bar=""', 9]
].forEach((element) => {
assert.throws(
() => parse(element[0]),
new RegExp(
`^SyntaxError: Unexpected character at index ${element[1]}$`
)
);
});
});
it('throws an error if a white space is misplaced', () => {
[
[' foo', 0],
['f oo', 2],
['foo;ba r', 7],
['foo;bar =', 8],
['foo;bar= ', 8],
['foo;bar=ba z', 11]
].forEach((element) => {
assert.throws(
() => parse(element[0]),
new RegExp(
`^SyntaxError: Unexpected character at index ${element[1]}$`
)
);
});
});
it('throws an error if a token contains invalid characters', () => {
[
['f@o', 1],
['f\\oo', 1],
['"foo"', 0],
['f"oo"', 1],
['foo;b@r', 5],
['foo;b\\ar', 5],
['foo;"bar"', 4],
['foo;b"ar"', 5],
['foo;bar=b@z', 9],
['foo;bar=b\\az ', 9],
['foo;bar="b@z"', 10],
['foo;bar="baz;"', 12],
['foo;bar=b"az"', 9],
['foo;bar="\\\\"', 10]
].forEach((element) => {
assert.throws(
() => parse(element[0]),
new RegExp(
`^SyntaxError: Unexpected character at index ${element[1]}$`
)
);
});
});
it('throws an error if the header value ends prematurely', () => {
[
'',
'foo ',
'foo\t',
'foo, ',
'foo;',
'foo;bar ',
'foo;bar,',
'foo;bar; ',
'foo;bar=',
'foo;bar="baz',
'foo;bar="1\\',
'foo;bar="baz" '
].forEach((header) => {
assert.throws(
() => parse(header),
/^SyntaxError: Unexpected end of input$/
);
});
});
});
describe('format', () => {
it('formats a single extension', () => {
const extensions = format({ foo: {} });
assert.strictEqual(extensions, 'foo');
});
it('formats params', () => {
const extensions = format({ foo: { bar: [true, 2], baz: 1 } });
assert.strictEqual(extensions, 'foo; bar; bar=2; baz=1');
});
it('formats multiple extensions', () => {
const extensions = format({
foo: [{}, { baz: true }],
bar: { baz: true }
});
assert.strictEqual(extensions, 'foo, foo; baz, bar; baz');
});
});
});
|