Skip to content

Commit

Permalink
start plugins tests; update exports
Browse files Browse the repository at this point in the history
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
  • Loading branch information
tunnckoCore committed Jan 29, 2020
1 parent 7568d08 commit 7fed544
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 15 deletions.
17 changes: 16 additions & 1 deletion src/Formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,24 @@ class IncomingForm extends EventEmitter {
for (let idx = 0; idx < this._plugins.length; idx++) {
const plugin = this._plugins[idx].bind(this);

const res = plugin.call(this, this, this.options);
let res = null;

try {
res = plugin.call(this, this, this.options);
} catch (err) {
this._error(
new Error(`plugin on index ${idx} failed with ${err.message}`),
);
break;
}

// todo: use Set/Map and pass plugin name instead of the `idx` index
this.emit('pluginReturn', idx, res);
results.push(res);
}

this.emit('pluginReturns', results);

if (results.length === 0 && results.length !== this._plugins.length) {
this._error(
new Error(
Expand Down Expand Up @@ -410,4 +424,5 @@ class IncomingForm extends EventEmitter {
}
}

IncomingForm.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
module.exports = IncomingForm;
27 changes: 13 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
const File = require('./File');
const Formidable = require('./Formidable');

const JSONParser = require('./parsers/JSON');
const DummyParser = require('./parsers/Dummy');
const MultipartParser = require('./parsers/Multipart');
const OctetStreamParser = require('./parsers/OctetStream');
const QuerystringParser = require('./parsers/Querystring');
const plugins = require('./plugins/index');
const parsers = require('./parsers/index');

// make it available without requiring the `new` keyword
// if you want it access `const formidable.IncomingForm` as v1
Expand All @@ -22,13 +19,15 @@ module.exports = Object.assign(formidable, {
IncomingForm: Formidable,

// parsers
JSONParser,
DummyParser,
MultipartParser,
OctetStreamParser,
QuerystringParser,

// typo aliases
OctetstreamParser: OctetStreamParser,
QueryStringParser: QuerystringParser,
...parsers,
parsers,

// misc
defaultOptions: Formidable.DEFAULT_OPTIONS,
enabledPlugins: Formidable.DEFAULT_OPTIONS.enabledPlugins,

// plugins
plugins: {
...plugins,
},
});
17 changes: 17 additions & 0 deletions src/parsers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const JSONParser = require('./JSON');
const DummyParser = require('./Dummy');
const MultipartParser = require('./Multipart');
const OctetStreamParser = require('./OctetStream');
const QueryStringParser = require('./Querystring');

Object.assign(exports, {
JSONParser,
DummyParser,
MultipartParser,
OctetStreamParser,
OctetstreamParser: OctetStreamParser,
QueryStringParser,
QuerystringParser: QueryStringParser,
});
13 changes: 13 additions & 0 deletions src/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const octetstream = require('./octetstream');
const querystring = require('./querystring');
const multipart = require('./multipart');
const json = require('./json');

Object.assign(exports, {
octetstream,
querystring,
multipart,
json,
});
97 changes: 97 additions & 0 deletions test/unit/test-custom-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* eslint-disable no-underscore-dangle */

'use strict';

// ! TODO: doesn't actually pass, should switch the test runner first,
// ! before finishing the plugins tests

const http = require('http');
const assert = require('assert');
const test = require('utest');

const { formidable } = require('../../src/index');

function makeRequest(server) {
server.listen(0, () => {
const choosenPort = server.address().port;

const request = http.request({
port: choosenPort,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
});

request.write(JSON.stringify({ qux: 'zaz' }));
request.end();
});
}

function onDone({ server, form, req, res }) {
form.parse(req, (err, fields) => {
assert.strictEqual(fields.qux, 'zaz');

setTimeout(() => {
res.end();
server.close();
}, 200);
});
}

test('custom plugins', {
'should have 3 custom plugins, and all be called when .parse() is called': () => {
const server = http.createServer((req, res) => {
const form = formidable();

form.on('pluginReturns', (results) => {
assert.strictEqual(results.length, 3, 'three plugins should be called');
});
form.on('end', () => {
assert.strictEqual(form.__customPlugin1, 111);
assert.strictEqual(form.__customPlugin2, 222);
assert.strictEqual(form.__customPlugin3, 333);
});

form.use((inst) => {
const self = inst;
self.__customPlugin1 = 111;
});
form.use((inst) => {
const self = inst;
self.__customPlugin2 = 222;
});
form.use((inst) => {
const self = inst;
self.__customPlugin3 = 333;
});

onDone({ server, form, req, res });
});

makeRequest(server);
},

'should emit `error` event when some plugin fail': () => {
const server = http.createServer((req, res) => {
const form = formidable();
// console.log(form);

form.once('error', (err) => {
assert.strictEqual(
err.message.includes('custom plugin err'),
true,
'should have error',
);
console.log('should print here!');
});

form.use(() => {
console.log('should be called');
throw new Error('custom plugin err');
});

onDone({ server, form, req, res });
});

makeRequest(server);
},
});

0 comments on commit 7fed544

Please sign in to comment.