Skip to content

Commit

Permalink
Move more tests over to Mocha (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Nov 27, 2021
1 parent ffdf2ba commit 82169a9
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 242 deletions.
81 changes: 81 additions & 0 deletions test/decoder.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const {assert} = require('chai');
const {Decoder} = require('../lib/utils');

describe('Decoder', () => {
[
{
source: ['Hello world'],
expected: 'Hello world',
what: 'No encoded bytes'
},
{
source: ['Hello%20world'],
expected: 'Hello world',
what: 'One full encoded byte'
},
{
source: ['Hello%20world%21'],
expected: 'Hello world!',
what: 'Two full encoded bytes'
},
{
source: ['Hello%', '20world'],
expected: 'Hello world',
what: 'One full encoded byte split #1'
},
{
source: ['Hello%2', '0world'],
expected: 'Hello world',
what: 'One full encoded byte split #2'
},
{
source: ['Hello%20', 'world'],
expected: 'Hello world',
what: 'One full encoded byte (concat)'
},
{
source: ['Hello%2Qworld'],
expected: 'Hello%2Qworld',
what: 'Malformed encoded byte #1'
},
{
source: ['Hello%world'],
expected: 'Hello%world',
what: 'Malformed encoded byte #2'
},
{
source: ['Hello+world'],
expected: 'Hello world',
what: 'Plus to space'
},
{
source: ['Hello+world%21'],
expected: 'Hello world!',
what: 'Plus and encoded byte'
},
{
source: ['5%2B5%3D10'],
expected: '5+5=10',
what: 'Encoded plus'
},
{
source: ['5+%2B+5+%3D+10'],
expected: '5 + 5 = 10',
what: 'Spaces and encoded plus'
},
].forEach((v) => {
it(v.what, () => {

const dec = new Decoder();
let result = '';
v.source.forEach(function (s) {
result += dec.write(s);
});
const msg = 'Decoded string mismatch.\n'
+ 'Saw: ' + result + '\n'
+ 'Expected: ' + v.expected;
assert.deepEqual(result, v.expected, msg);
});
})
})

84 changes: 84 additions & 0 deletions test/multipart-stream-pause.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const { inspect } = require('util');
const { assert } = require('chai');
const Busboy = require('..');

const BOUNDARY = 'u2KxIV5yF1y+xUspOQCCZopaVgeV6Jxihv35XQJmuTx8X3sh';

function formDataSection(key, value) {
return Buffer.from('\r\n--' + BOUNDARY
+ '\r\nContent-Disposition: form-data; name="'
+ key + '"\r\n\r\n' + value);
}
function formDataFile(key, filename, contentType) {
return Buffer.concat([
Buffer.from('\r\n--' + BOUNDARY + '\r\n'),
Buffer.from('Content-Disposition: form-data; name="'
+ key + '"; filename="' + filename + '"\r\n'),
Buffer.from('Content-Type: ' + contentType + '\r\n\r\n'),
Buffer.allocUnsafe(100000)
]);
}

describe('multipart-stream-pause', () => {
it('processes stream correctly', () => {
const reqChunks = [
Buffer.concat([
formDataFile('file', 'file.bin', 'application/octet-stream'),
formDataSection('foo', 'foo value')
]),
formDataSection('bar', 'bar value'),
Buffer.from('\r\n--' + BOUNDARY + '--\r\n')
];
const busboy = new Busboy({
headers: {
'content-type': 'multipart/form-data; boundary=' + BOUNDARY
}
});
let finishes = 0;
const results = [];
const expected = [
['file', 'file', 'file.bin', '7bit', 'application/octet-stream'],
['field', 'foo', 'foo value', false, false, '7bit', 'text/plain'],
['field', 'bar', 'bar value', false, false, '7bit', 'text/plain'],
];

busboy.on('field', function(key, val, keyTrunc, valTrunc, encoding, contype) {
results.push(['field', key, val, keyTrunc, valTrunc, encoding, contype]);
});
busboy.on('file', function(fieldname, stream, filename, encoding, mimeType) {
results.push(['file', fieldname, filename, encoding, mimeType]);
// Simulate a pipe where the destination is pausing (perhaps due to waiting
// for file system write to finish)
setTimeout(function() {
stream.resume();
}, 10);
});
busboy.on('finish', function() {
assert(finishes++ === 0, 'finish emitted multiple times');
assert.deepEqual(results.length,
expected.length,
'Parsed result count mismatch. Saw '
+ results.length
+ '. Expected: ' + expected.length);

results.forEach(function(result, i) {
assert.deepEqual(result,
expected[i],
'Result mismatch:\nParsed: ' + inspect(result)
+ '\nExpected: ' + inspect(expected[i]));
});
}).on('error', function(err) {
assert(false, 'Unexpected error: ' + err.stack);
});

reqChunks.forEach(function(buf) {
busboy.write(buf);
});
busboy.end();

process.on('exit', function() {
assert(finishes === 1, 'busboy did not finish');
});
})
})

97 changes: 97 additions & 0 deletions test/parse-params.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const { inspect } = require('util');
const { assert } = require('chai');
const { parseParams } = require('../lib/utils');

describe('parse-params', () => {
[
{ source: 'video/ogg',
expected: ['video/ogg'],
what: 'No parameters'
},
{ source: 'video/ogg;',
expected: ['video/ogg'],
what: 'No parameters (with separator)'
},
{ source: 'video/ogg; ',
expected: ['video/ogg'],
what: 'No parameters (with separator followed by whitespace)'
},
{ source: ';video/ogg',
expected: ['', 'video/ogg'],
what: 'Empty parameter'
},
{ source: 'video/*',
expected: ['video/*'],
what: 'Subtype with asterisk'
},
{ source: 'text/plain; encoding=utf8',
expected: ['text/plain', ['encoding', 'utf8']],
what: 'Unquoted'
},
{ source: 'text/plain; encoding=',
expected: ['text/plain', ['encoding', '']],
what: 'Unquoted empty string'
},
{ source: 'text/plain; encoding="utf8"',
expected: ['text/plain', ['encoding', 'utf8']],
what: 'Quoted'
},
{ source: 'text/plain; greeting="hello \\"world\\""',
expected: ['text/plain', ['greeting', 'hello "world"']],
what: 'Quotes within quoted'
},
{ source: 'text/plain; encoding=""',
expected: ['text/plain', ['encoding', '']],
what: 'Quoted empty string'
},
{ source: 'text/plain; encoding="utf8";\t foo=bar;test',
expected: ['text/plain', ['encoding', 'utf8'], ['foo', 'bar'], 'test'],
what: 'Multiple params with various spacing'
},
{ source: "text/plain; filename*=iso-8859-1'en'%A3%20rates",
expected: ['text/plain', ['filename', '£ rates']],
what: 'Extended parameter (RFC 5987) with language'
},
{ source: "text/plain; filename*=utf-8''%c2%a3%20and%20%e2%82%ac%20rates",
expected: ['text/plain', ['filename', '£ and € rates']],
what: 'Extended parameter (RFC 5987) without language'
},
{ source: "text/plain; filename*=utf-8''%E6%B5%8B%E8%AF%95%E6%96%87%E6%A1%A3",
expected: ['text/plain', ['filename', '测试文档']],
what: 'Extended parameter (RFC 5987) without language #2'
},
{ source: "text/plain; filename*=iso-8859-1'en'%A3%20rates; altfilename*=utf-8''%c2%a3%20and%20%e2%82%ac%20rates",
expected: ['text/plain', ['filename', '£ rates'], ['altfilename', '£ and € rates']],
what: 'Multiple extended parameters (RFC 5987) with mixed charsets'
},
{ source: "text/plain; filename*=iso-8859-1'en'%A3%20rates; altfilename=\"foobarbaz\"",
expected: ['text/plain', ['filename', '£ rates'], ['altfilename', 'foobarbaz']],
what: 'Mixed regular and extended parameters (RFC 5987)'
},
{ source: "text/plain; filename=\"foobarbaz\"; altfilename*=iso-8859-1'en'%A3%20rates",
expected: ['text/plain', ['filename', 'foobarbaz'], ['altfilename', '£ rates']],
what: 'Mixed regular and extended parameters (RFC 5987) #2'
},
{ source: 'text/plain; filename="C:\\folder\\test.png"',
expected: ['text/plain', ['filename', 'C:\\folder\\test.png']],
what: 'Unescaped backslashes should be considered backslashes'
},
{ source: 'text/plain; filename="John \\"Magic\\" Smith.png"',
expected: ['text/plain', ['filename', 'John "Magic" Smith.png']],
what: 'Escaped double-quotes should be considered double-quotes'
},
{ source: 'multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY',
expected: ['multipart/form-data', ['charset', 'utf-8'], ['boundary', '0xKhTmLbOuNdArY']],
what: 'Multiple non-quoted parameters'
},
].forEach((v) => {
it(v.what, () => {

const result = parseParams(v.source),
msg = 'parsed parameters mismatch.\n'
+ 'Saw: ' + inspect(result) + '\n'
+ 'Expected: ' + inspect(v.expected);
assert.deepEqual(result, v.expected, msg);
});
})
})
80 changes: 0 additions & 80 deletions test/test-types-multipart-stream-pause.js

This file was deleted.

Loading

0 comments on commit 82169a9

Please sign in to comment.