Skip to content

Commit

Permalink
Merge pull request #8 from ruiquelhas/enhancement/use-supervizor
Browse files Browse the repository at this point in the history
Simplify the implementation using supervizor
  • Loading branch information
ruiquelhas committed Dec 22, 2015
2 parents 4523f21 + 93c02e1 commit 0204699
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ $ npm install copperfield

Register the package as a server plugin to enable validation for each route that parses — `parse: true` — and reads the request payload into memory — `output: 'data'`. For every other route with a different configuration, the validation is skipped.

If the validation fails, a [joi](https://github.com/hapijs/joi)-like `400 Bad Request` error is returned alongside an additional `magic-pattern: invalid` response header. If everything is ok, the response will ultimately contain a `magic-pattern: valid` header.
If the validation fails, a [joi](https://github.com/hapijs/joi)-like `400 Bad Request` error is returned alongside an additional `content-validation: failure` response header. If everything is ok, the response will ultimately contain a `content-validation: success` header.

### Example

```js
const Hapi = require('hapi');
const Copperfield, = require('copperfield');
const Copperfield = require('copperfield');

server = new Hapi.Server();
server.connection({
Expand Down
56 changes: 8 additions & 48 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,19 @@
'use strict';

const Boom = require('boom');
const Houdin = require('houdin');
const pluck = require('lodash.pluck');
const Supervizor = require('supervizor');

const internals = {};

internals.onPreHandler = function (options) {

return function (request, reply) {

if (request.method !== 'post' || request.mime !== 'multipart/form-data') {
return reply.continue();
}

Houdin.validate(request.payload, options, (err, valid) => {

if (err) {
const error = Boom.badRequest(err.message);
error.output.headers['magic-pattern'] = 'invalid';
error.output.payload.validation = {
source: 'payload',
keys: pluck(err.details, 'path')
};

return reply(error);
}

if (valid) {
request.app.magic = true;
}

return reply.continue();
});
};
};

internals.onPreResponse = function (options) {

return function (request, reply) {
exports.register = function (server, options, next) {

if (!request.response.isBoom && request.app.magic === true) {
request.response.header('magic-pattern', 'valid');
const plugin = {
register: Supervizor,
options: {
validator: Houdin.validate,
whitelist: options.whitelist
}

reply.continue();
};
};

exports.register = function (server, options, next) {

server.ext('onPreHandler', internals.onPreHandler(options));
server.ext('onPreResponse', internals.onPreResponse(options));

next();
server.register(plugin, next);
};

exports.register.attributes = {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
"stream-to-promise": "1.x.x"
},
"dependencies": {
"boom": "3.x.x",
"houdin": "1.x.x",
"lodash.pluck": "3.x.x"
"supervizor": "1.x.x"
},
"engines": {
"node": ">= 4.0.0"
Expand Down
30 changes: 2 additions & 28 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,6 @@ lab.experiment('copperfield', () => {
server.register(setup, done);
});

lab.test('should return control to the server if the request method is not POST', (done) => {

server.inject('/', (response) => {

Code.expect(response.statusCode).to.equal(200);
Code.expect(response.headers['magic-pattern']).to.not.exist();
done();
});
});

lab.test('should return control to the server if the payload does not contain any file', (done) => {

const form = new FormData();
form.append('foo', 'bar');

streamToPromise(form).then((payload) => {

server.inject({ headers: form.getHeaders(), method: 'POST', payload: payload, url: '/' }, (response) => {

Code.expect(response.statusCode).to.equal(200);
Code.expect(response.headers['magic-pattern']).to.not.exist();
done();
});
});
});

lab.test('should return error if some file in the payload is not allowed', (done) => {

const png = path.join(os.tmpdir(), 'foo.png');
Expand All @@ -84,7 +58,7 @@ lab.experiment('copperfield', () => {
server.inject({ headers: form.getHeaders(), method: 'POST', payload: payload, url: '/' }, (response) => {

Code.expect(response.statusCode).to.equal(400);
Code.expect(response.headers['magic-pattern']).to.equal('invalid');
Code.expect(response.headers['content-validation']).to.equal('failure');
Code.expect(response.result).to.include(['message', 'validation']);
Code.expect(response.result.message).to.equal('child \"file1\" fails because [\"file1\" type is not allowed]');
Code.expect(response.result.validation).to.include(['source', 'keys']);
Expand All @@ -110,7 +84,7 @@ lab.experiment('copperfield', () => {
server.inject({ headers: form.getHeaders(), method: 'POST', payload: payload, url: '/' }, (response) => {

Code.expect(response.statusCode).to.equal(200);
Code.expect(response.headers['magic-pattern']).to.equal('valid');
Code.expect(response.headers['content-validation']).to.equal('success');
done();
});
});
Expand Down

0 comments on commit 0204699

Please sign in to comment.