Skip to content

Commit

Permalink
Merge pull request #18 from ruiquelhas/update-tests-and-dependencies
Browse files Browse the repository at this point in the history
Update tests and dependencies
  • Loading branch information
ruiquelhas committed Jun 6, 2017
2 parents ac2bc75 + 54b28b1 commit ccfb4bb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 25 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# thurston
Route-level file type validation for [hapi](https://github.com/hapijs/hapi) parsed `multipart/form-data` stream request payloads. Also works as a standalone module, and most importantly, works in tandem with [houdin](https://github.com/ruiquelhas/houdin) for a truly magical experience.

[![NPM Version][fury-img]][fury-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Dependencies][david-img]][david-url]
[![NPM Version][version-img]][version-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Dependencies][david-img]][david-url] [![Dev Dependencies][david-dev-img]][david-dev-url]

## Table of Contents
- [Installation](#installation)
Expand Down Expand Up @@ -90,11 +90,13 @@ Thurston.validate({ file: gif }, options, (err, value) => {
## Supported File Types
The same as [file-type](https://github.com/sindresorhus/file-type#supported-file-types).

[coveralls-img]: https://coveralls.io/repos/ruiquelhas/thurston/badge.svg
[coveralls-img]: https://img.shields.io/coveralls/ruiquelhas/thurston.svg?style=flat-square
[coveralls-url]: https://coveralls.io/github/ruiquelhas/thurston
[david-img]: https://david-dm.org/ruiquelhas/thurston.svg
[david-img]: https://img.shields.io/david/ruiquelhas/thurston.svg?style=flat-square
[david-url]: https://david-dm.org/ruiquelhas/thurston
[fury-img]: https://badge.fury.io/js/thurston.svg
[fury-url]: https://badge.fury.io/js/thurston
[travis-img]: https://travis-ci.org/ruiquelhas/thurston.svg
[david-dev-img]: https://img.shields.io/david/dev/ruiquelhas/thurston.svg?style=flat-square
[david-dev-url]: https://david-dm.org/ruiquelhas/thurston?type=dev
[version-img]: https://img.shields.io/npm/v/thurston.svg?style=flat-square
[version-url]: https://www.npmjs.com/package/thurston
[travis-img]: https://img.shields.io/travis/ruiquelhas/thurston.svg?style=flat-square
[travis-url]: https://travis-ci.org/ruiquelhas/thurston
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"multi-part": "2.x.x"
},
"dependencies": {
"houdin": "2.x.x",
"houdin": "3.x.x",
"items": "2.x.x"
},
"engines": {
Expand Down
85 changes: 67 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ const lab = exports.lab = Lab.script();

lab.experiment('Thurston', () => {

let server;
let goodServer;
let badServer;
let invalid;
let png;

lab.before((done) => {

server = new Hapi.Server();
server.connection({
goodServer = new Hapi.Server();
goodServer.connection({
routes: {
validate: {
options: {
Expand All @@ -32,7 +33,16 @@ lab.experiment('Thurston', () => {
}
});

server.route([{
badServer = new Hapi.Server();
badServer.connection({
routes: {
validate: {
options: {}
}
}
});

const baseRoute = {
config: {
handler: (request, reply) => reply(),
payload: {
Expand All @@ -45,16 +55,29 @@ lab.experiment('Thurston', () => {
},
method: 'POST',
path: '/stream'
}, {
config: {
handler: (request, reply) => reply(),
validate: {
payload: Thurston.validate
}
},
method: 'POST',
path: '/data'
}]);
};

goodServer.route(baseRoute);

badServer.route([
baseRoute,
Object.assign({}, baseRoute, {
config: Object.assign({}, baseRoute.config, {
payload: {
output: 'data'
}
}),
path: '/data'
}),
Object.assign({}, baseRoute, {
config: Object.assign({}, baseRoute.config, {
payload: {
output: 'file'
}
}),
path: '/file'
})
]);

done();
});
Expand All @@ -77,7 +100,7 @@ lab.experiment('Thurston', () => {
form.append('file', Fs.createReadStream(invalid));
form.append('foo', 'bar');

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

Code.expect(response.statusCode).to.equal(400);
Code.expect(response.result).to.include(['message', 'validation']);
Expand All @@ -89,23 +112,49 @@ lab.experiment('Thurston', () => {
});
});

lab.test('should return error if no whitelist is specified', (done) => {

const form = new Form();
form.append('file', Fs.createReadStream(png));

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

Code.expect(response.statusCode).to.equal(400);
Code.expect(response.result).to.include(['message', 'validation']);
Code.expect(response.result.message).to.equal('child \"file\" fails because [\"file\" type is not allowed]');
Code.expect(response.result.validation).to.include(['source', 'keys']);
Code.expect(response.result.validation.source).to.equal('payload');
Code.expect(response.result.validation.keys).to.include('file');
done();
});
});

lab.test('should return control to the server if all files the payload are allowed', (done) => {

const form = new Form();
form.append('file1', Fs.createReadStream(png));
form.append('file2', Fs.createReadStream(png));
form.append('foo', 'bar');

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

Code.expect(response.statusCode).to.equal(200);
done();
});
});

lab.test('should return control to the server if the payload is parsed as a temporary file', (done) => {

badServer.inject({ method: 'POST', payload: undefined, url: '/file' }, (response) => {

Code.expect(response.statusCode).to.equal(200);
done();
});
});

lab.test('should return control to the server if the parsed payload is broken', (done) => {
lab.test('should return control to the server if the payload is parsed as a buffer', (done) => {

server.inject({ method: 'POST', payload: undefined, url: '/data' }, (response) => {
badServer.inject({ method: 'POST', payload: undefined, url: '/data' }, (response) => {

Code.expect(response.statusCode).to.equal(200);
done();
Expand Down

0 comments on commit ccfb4bb

Please sign in to comment.