UNPKG

5.36 kBJavaScriptView Raw
1var Busboy = require('..');
2
3var path = require('path'),
4 inspect = require('util').inspect,
5 assert = require('assert');
6
7var EMPTY_FN = function() {};
8
9var t = 0,
10 group = path.basename(__filename, '.js') + '/';
11
12var tests = [
13 { source: ['foo'],
14 expected: [['foo', '', false, false]],
15 what: 'Unassigned value'
16 },
17 { source: ['foo=bar'],
18 expected: [['foo', 'bar', false, false]],
19 what: 'Assigned value'
20 },
21 { source: ['foo&bar=baz'],
22 expected: [['foo', '', false, false],
23 ['bar', 'baz', false, false]],
24 what: 'Unassigned and assigned value'
25 },
26 { source: ['foo=bar&baz'],
27 expected: [['foo', 'bar', false, false],
28 ['baz', '', false, false]],
29 what: 'Assigned and unassigned value'
30 },
31 { source: ['foo=bar&baz=bla'],
32 expected: [['foo', 'bar', false, false],
33 ['baz', 'bla', false, false]],
34 what: 'Two assigned values'
35 },
36 { source: ['foo&bar'],
37 expected: [['foo', '', false, false],
38 ['bar', '', false, false]],
39 what: 'Two unassigned values'
40 },
41 { source: ['foo&bar&'],
42 expected: [['foo', '', false, false],
43 ['bar', '', false, false]],
44 what: 'Two unassigned values and ampersand'
45 },
46 { source: ['foo=bar+baz%2Bquux'],
47 expected: [['foo', 'bar baz+quux', false, false]],
48 what: 'Assigned value with (plus) space'
49 },
50 { source: ['foo=bar%20baz%21'],
51 expected: [['foo', 'bar baz!', false, false]],
52 what: 'Assigned value with encoded bytes'
53 },
54 { source: ['foo%20bar=baz%20bla%21'],
55 expected: [['foo bar', 'baz bla!', false, false]],
56 what: 'Assigned value with encoded bytes #2'
57 },
58 { source: ['foo=bar%20baz%21&num=1000'],
59 expected: [['foo', 'bar baz!', false, false],
60 ['num', '1000', false, false]],
61 what: 'Two assigned values, one with encoded bytes'
62 },
63 { source: ['foo=bar&baz=bla'],
64 expected: [],
65 what: 'Limits: zero fields',
66 limits: { fields: 0 }
67 },
68 { source: ['foo=bar&baz=bla'],
69 expected: [['foo', 'bar', false, false]],
70 what: 'Limits: one field',
71 limits: { fields: 1 }
72 },
73 { source: ['foo=bar&baz=bla'],
74 expected: [['foo', 'bar', false, false],
75 ['baz', 'bla', false, false]],
76 what: 'Limits: field part lengths match limits',
77 limits: { fieldNameSize: 3, fieldSize: 3 }
78 },
79 { source: ['foo=bar&baz=bla'],
80 expected: [['fo', 'bar', true, false],
81 ['ba', 'bla', true, false]],
82 what: 'Limits: truncated field name',
83 limits: { fieldNameSize: 2 }
84 },
85 { source: ['foo=bar&baz=bla'],
86 expected: [['foo', 'ba', false, true],
87 ['baz', 'bl', false, true]],
88 what: 'Limits: truncated field value',
89 limits: { fieldSize: 2 }
90 },
91 { source: ['foo=bar&baz=bla'],
92 expected: [['fo', 'ba', true, true],
93 ['ba', 'bl', true, true]],
94 what: 'Limits: truncated field name and value',
95 limits: { fieldNameSize: 2, fieldSize: 2 }
96 },
97 { source: ['foo=bar&baz=bla'],
98 expected: [['fo', '', true, true],
99 ['ba', '', true, true]],
100 what: 'Limits: truncated field name and zero value limit',
101 limits: { fieldNameSize: 2, fieldSize: 0 }
102 },
103 { source: ['foo=bar&baz=bla'],
104 expected: [['', '', true, true],
105 ['', '', true, true]],
106 what: 'Limits: truncated zero field name and zero value limit',
107 limits: { fieldNameSize: 0, fieldSize: 0 }
108 },
109 { source: ['&'],
110 expected: [],
111 what: 'Ampersand'
112 },
113 { source: ['&&&&&'],
114 expected: [],
115 what: 'Many ampersands'
116 },
117 { source: ['='],
118 expected: [['', '', false, false]],
119 what: 'Assigned value, empty name and value'
120 },
121 { source: [''],
122 expected: [],
123 what: 'Nothing'
124 },
125];
126
127function next() {
128 if (t === tests.length)
129 return;
130
131 var v = tests[t];
132
133 var busboy = new Busboy({
134 limits: v.limits,
135 headers: {
136 'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
137 }
138 }),
139 finishes = 0,
140 results = [];
141
142 busboy.on('field', function(key, val, keyTrunc, valTrunc) {
143 results.push([key, val, keyTrunc, valTrunc]);
144 });
145 busboy.on('file', function() {
146 throw new Error(makeMsg(v.what, 'Unexpected file'));
147 });
148 busboy.on('finish', function() {
149 assert(finishes++ === 0, makeMsg(v.what, 'finish emitted multiple times'));
150 assert.deepEqual(results.length,
151 v.expected.length,
152 makeMsg(v.what, 'Parsed result count mismatch. Saw '
153 + results.length
154 + '. Expected: ' + v.expected.length));
155
156 var i = 0;
157 results.forEach(function(result) {
158 assert.deepEqual(result,
159 v.expected[i],
160 makeMsg(v.what,
161 'Result mismatch:\nParsed: ' + inspect(result)
162 + '\nExpected: ' + inspect(v.expected[i]))
163 );
164 ++i;
165 });
166 ++t;
167 next();
168 });
169
170 v.source.forEach(function(s) {
171 busboy.write(Buffer.from(s, 'utf8'), EMPTY_FN);
172 });
173 busboy.end();
174}
175next();
176
177function makeMsg(what, msg) {
178 return '[' + group + what + ']: ' + msg;
179}
180
181process.on('exit', function() {
182 assert(t === tests.length, makeMsg('_exit', 'Only finished ' + t + '/' + tests.length + ' tests'));
183});