Skip to content

Commit 50fbddb

Browse files
committed
fix(tests): include multipart and qs parser unit tests, part of #415
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
1 parent 1a33d0c commit 50fbddb

7 files changed

+129
-119
lines changed

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ module.exports = Object.assign(formidable, {
2727
MultipartParser,
2828
OctetStreamParser,
2929
QuerystringParser,
30+
31+
// typo aliases
32+
OctetstreamParser: OctetStreamParser,
33+
QueryStringParser: QuerystringParser,
3034
});

src/parsers/Multipart.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class MultipartParser extends Transform {
4848
this.boundary = null;
4949
this.boundaryChars = null;
5050
this.lookbehind = null;
51+
this.bufferLength = 0;
5152
this.state = STATE.PARSER_UNINITIALIZED;
5253

5354
this.index = null;
@@ -98,7 +99,7 @@ class MultipartParser extends Transform {
9899
const { lookbehind, boundary, boundaryChars } = this;
99100
const boundaryLength = boundary.length;
100101
const boundaryEnd = boundaryLength - 1;
101-
const bufferLength = buffer.length;
102+
this.bufferLength = buffer.length;
102103
let c = null;
103104
let cl = null;
104105

@@ -125,7 +126,7 @@ class MultipartParser extends Transform {
125126
}
126127
};
127128

128-
for (i = 0; i < bufferLength; i++) {
129+
for (i = 0; i < this.bufferLength; i++) {
129130
c = buffer[i];
130131
switch (state) {
131132
case STATE.PARSER_UNINITIALIZED:
@@ -232,7 +233,7 @@ class MultipartParser extends Transform {
232233
if (index === 0) {
233234
// boyer-moore derrived algorithm to safely skip non-boundary data
234235
i += boundaryEnd;
235-
while (i < bufferLength && !(buffer[i] in boundaryChars)) {
236+
while (i < this.bufferLength && !(buffer[i] in boundaryChars)) {
236237
i += boundaryLength;
237238
}
238239
i -= boundaryEnd;
@@ -317,7 +318,7 @@ class MultipartParser extends Transform {
317318
this.flags = flags;
318319

319320
done();
320-
return bufferLength;
321+
return this.bufferLength;
321322
}
322323

323324
explain() {

src/parsers/Querystring.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ class QuerystringParser extends Transform {
1212
super({ readableObjectMode: true });
1313
this.maxKeys = maxKeys;
1414
this.buffer = '';
15+
this.bufferLength = 0;
1516
}
1617

1718
_transform(buffer, encoding, callback) {
1819
this.buffer += buffer.toString('ascii');
20+
this.bufferLength = this.buffer.length;
1921
callback();
2022
}
2123

test-legacy/simple/test-multipart-parser.js

-67
This file was deleted.

test-legacy/simple/test-querystring-parser.js

-48
This file was deleted.

test/unit/test-multipart-parser.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
5+
const { MultipartParser } = require('../../src/index');
6+
7+
let parser;
8+
9+
function test(testFn) {
10+
parser = new MultipartParser();
11+
testFn();
12+
}
13+
14+
test(function constructor() {
15+
assert.strictEqual(parser.boundary, null);
16+
assert.strictEqual(parser.state, 0);
17+
assert.strictEqual(parser.flags, 0);
18+
assert.strictEqual(parser.boundaryChars, null);
19+
assert.strictEqual(parser.index, null);
20+
assert.strictEqual(parser.lookbehind, null);
21+
assert.strictEqual(parser.constructor.name, 'MultipartParser');
22+
});
23+
24+
test(function initWithBoundary() {
25+
const boundary = 'abc';
26+
parser.initWithBoundary(boundary);
27+
assert.deepEqual(Array.prototype.slice.call(parser.boundary), [
28+
13,
29+
10,
30+
45,
31+
45,
32+
97,
33+
98,
34+
99,
35+
]);
36+
assert.strictEqual(parser.state, MultipartParser.STATES.START);
37+
38+
assert.deepEqual(parser.boundaryChars, {
39+
10: true,
40+
13: true,
41+
45: true,
42+
97: true,
43+
98: true,
44+
99: true,
45+
});
46+
});
47+
48+
test(function parserError() {
49+
const boundary = 'abc';
50+
const buffer = Buffer.alloc(5);
51+
52+
parser.initWithBoundary(boundary);
53+
buffer.write('--ad', 0);
54+
// assert.strictEqual(parser.bufferLength, 0);
55+
parser.write(buffer);
56+
assert.strictEqual(parser.bufferLength, 5);
57+
});
58+
59+
// ! skip
60+
// test(function end() {
61+
// (function testError() {
62+
// assert.strictEqual(
63+
// parser.end().message,
64+
// `MultipartParser.end(): stream ended unexpectedly: ${parser.explain()}`,
65+
// );
66+
// })();
67+
68+
// (function testRegular() {
69+
// parser.state = MultipartParser.STATES.END;
70+
// assert.strictEqual(parser.end(), undefined);
71+
// })();
72+
// });

test/unit/test-querystring-parser.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const { QuerystringParser } = require('../../src/index');
5+
6+
let parser;
7+
8+
function test(testFn) {
9+
parser = new QuerystringParser();
10+
testFn();
11+
}
12+
13+
test(function ctor() {
14+
assert.equal(parser.buffer, '');
15+
assert.equal(parser.constructor.name, 'QuerystringParser');
16+
});
17+
18+
test(function write() {
19+
const a = Buffer.from('a=1');
20+
parser.write(a);
21+
assert.equal(parser.bufferLength, a.length);
22+
23+
const b = Buffer.from('&b=2');
24+
parser.write(b);
25+
assert.equal(parser.buffer, a + b);
26+
assert.equal(parser.bufferLength, a.length + b.length);
27+
});
28+
29+
// test(function end() {
30+
// const FIELDS = { a: ['b', { c: 'd' }], e: 'f' };
31+
32+
// gently.expect(GENTLY.hijacked.querystring, 'parse', (str) => {
33+
// assert.equal(str, parser.buffer);
34+
// return FIELDS;
35+
// });
36+
37+
// gently.expect(parser, 'onField', Object.keys(FIELDS).length, (key, val) => {
38+
// assert.deepEqual(FIELDS[key], val);
39+
// });
40+
41+
// gently.expect(parser, 'onEnd');
42+
43+
// parser.buffer = 'my buffer';
44+
// parser.end();
45+
// assert.equal(parser.buffer, '');
46+
// });

0 commit comments

Comments
 (0)