Skip to content

Commit

Permalink
Remove static dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Rui Quelhas committed Nov 23, 2015
1 parent 3b8219b commit ae23d4b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"form-data": "0.x.x",
"hapi": "11.x.x",
"lab": "7.x.x",
"sinon": "1.x.x",
"mock-fs": "3.x.x",
"stream-to-promise": "1.x.x"
},
"dependencies": {
Expand Down
44 changes: 15 additions & 29 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict';

const fs = require('fs');
const path = require('path');

const Code = require('code');
const FormData = require('form-data');
const Hapi = require('hapi');
const Lab = require('lab');
const Magik = require('magik');
const sinon = require('sinon');
const mock = require('mock-fs');
const streamToPromise = require('stream-to-promise');

const lab = exports.lab = Lab.script();
Expand Down Expand Up @@ -62,22 +58,16 @@ lab.experiment('houdin', () => {

lab.test('should return error if the file type cannot be guessed', (done) => {

sinon.stub(Magik, 'guess');
Magik.guess.yieldsAsync(new Error());

// Use payload with a file of a known type to ensure Magik is used.
const file = path.join(__dirname, 'static', 'file.gif');
const fs = mock.fs({ invalid: new Buffer([0x00]) });

const form = new FormData();
form.append('file', fs.createReadStream(file));
form.append('file', fs.createReadStream('invalid'));
form.append('foo', 'bar');

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

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

Magik.guess.restore();

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\" could not be validated]');
Expand All @@ -91,22 +81,16 @@ lab.experiment('houdin', () => {

lab.test('should return error if the file type is not known', (done) => {

sinon.stub(Magik, 'guess');
Magik.guess.yieldsAsync(null, []);

// Use payload with a file of a known type to ensure Magik is used.
const file = path.join(__dirname, 'static', 'file.gif');
const fs = mock.fs({ unknown: new Buffer([0x00, 0x00]) });

const form = new FormData();
form.append('file', fs.createReadStream(file));
form.append('file', fs.createReadStream('unknown'));
form.append('foo', 'bar');

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

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

Magik.guess.restore();

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 unknown]');
Expand All @@ -120,13 +104,15 @@ lab.experiment('houdin', () => {

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

const file1 = path.join(__dirname, 'static', 'file.gif');
const file2 = path.join(__dirname, 'static', 'file.png');
const fs = mock.fs({
png: new Buffer([0x89, 0x50]),
gif: new Buffer([0x47, 0x49])
});

const form = new FormData();
form.append('file1', fs.createReadStream(file1));
form.append('file2', fs.createReadStream(file2));
form.append('file3', fs.createReadStream(file1));
form.append('file1', fs.createReadStream('gif'));
form.append('file2', fs.createReadStream('png'));
form.append('file3', fs.createReadStream('gif'));
form.append('foo', 'bar');

streamToPromise(form).then((payload) => {
Expand All @@ -146,11 +132,11 @@ lab.experiment('houdin', () => {

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

const file = path.join(__dirname, 'static', 'file.png');
const fs = mock.fs({ png: new Buffer([0x89, 0x50]) });

const form = new FormData();
form.append('file1', fs.createReadStream(file));
form.append('file2', fs.createReadStream(file));
form.append('file1', fs.createReadStream('png'));
form.append('file2', fs.createReadStream('png'));
form.append('foo', 'bar');

streamToPromise(form).then((payload) => {
Expand Down
Binary file removed test/static/file.gif
Binary file not shown.
Binary file removed test/static/file.png
Binary file not shown.

0 comments on commit ae23d4b

Please sign in to comment.