-
-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): include multipart and qs parser unit tests, part of #415
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
- Loading branch information
1 parent
1a33d0c
commit 50fbddb
Showing
7 changed files
with
129 additions
and
119 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
const { MultipartParser } = require('../../src/index'); | ||
|
||
let parser; | ||
|
||
function test(testFn) { | ||
parser = new MultipartParser(); | ||
testFn(); | ||
} | ||
|
||
test(function constructor() { | ||
assert.strictEqual(parser.boundary, null); | ||
assert.strictEqual(parser.state, 0); | ||
assert.strictEqual(parser.flags, 0); | ||
assert.strictEqual(parser.boundaryChars, null); | ||
assert.strictEqual(parser.index, null); | ||
assert.strictEqual(parser.lookbehind, null); | ||
assert.strictEqual(parser.constructor.name, 'MultipartParser'); | ||
}); | ||
|
||
test(function initWithBoundary() { | ||
const boundary = 'abc'; | ||
parser.initWithBoundary(boundary); | ||
assert.deepEqual(Array.prototype.slice.call(parser.boundary), [ | ||
13, | ||
10, | ||
45, | ||
45, | ||
97, | ||
98, | ||
99, | ||
]); | ||
assert.strictEqual(parser.state, MultipartParser.STATES.START); | ||
|
||
assert.deepEqual(parser.boundaryChars, { | ||
10: true, | ||
13: true, | ||
45: true, | ||
97: true, | ||
98: true, | ||
99: true, | ||
}); | ||
}); | ||
|
||
test(function parserError() { | ||
const boundary = 'abc'; | ||
const buffer = Buffer.alloc(5); | ||
|
||
parser.initWithBoundary(boundary); | ||
buffer.write('--ad', 0); | ||
// assert.strictEqual(parser.bufferLength, 0); | ||
parser.write(buffer); | ||
assert.strictEqual(parser.bufferLength, 5); | ||
}); | ||
|
||
// ! skip | ||
// test(function end() { | ||
// (function testError() { | ||
// assert.strictEqual( | ||
// parser.end().message, | ||
// `MultipartParser.end(): stream ended unexpectedly: ${parser.explain()}`, | ||
// ); | ||
// })(); | ||
|
||
// (function testRegular() { | ||
// parser.state = MultipartParser.STATES.END; | ||
// assert.strictEqual(parser.end(), undefined); | ||
// })(); | ||
// }); |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const { QuerystringParser } = require('../../src/index'); | ||
|
||
let parser; | ||
|
||
function test(testFn) { | ||
parser = new QuerystringParser(); | ||
testFn(); | ||
} | ||
|
||
test(function ctor() { | ||
assert.equal(parser.buffer, ''); | ||
assert.equal(parser.constructor.name, 'QuerystringParser'); | ||
}); | ||
|
||
test(function write() { | ||
const a = Buffer.from('a=1'); | ||
parser.write(a); | ||
assert.equal(parser.bufferLength, a.length); | ||
|
||
const b = Buffer.from('&b=2'); | ||
parser.write(b); | ||
assert.equal(parser.buffer, a + b); | ||
assert.equal(parser.bufferLength, a.length + b.length); | ||
}); | ||
|
||
// test(function end() { | ||
// const FIELDS = { a: ['b', { c: 'd' }], e: 'f' }; | ||
|
||
// gently.expect(GENTLY.hijacked.querystring, 'parse', (str) => { | ||
// assert.equal(str, parser.buffer); | ||
// return FIELDS; | ||
// }); | ||
|
||
// gently.expect(parser, 'onField', Object.keys(FIELDS).length, (key, val) => { | ||
// assert.deepEqual(FIELDS[key], val); | ||
// }); | ||
|
||
// gently.expect(parser, 'onEnd'); | ||
|
||
// parser.buffer = 'my buffer'; | ||
// parser.end(); | ||
// assert.equal(parser.buffer, ''); | ||
// }); |