Skip to content

Instantly share code, notes, and snippets.

@Couto
Last active February 13, 2022 22:16
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Couto/127ca8a6bd28ecc4a084 to your computer and use it in GitHub Desktop.
Save Couto/127ca8a6bd28ecc4a084 to your computer and use it in GitHub Desktop.
Test case for file uploads with Hapi.js. Currently failing when same request is made with a simple JSON.
{
"name": "file-upload",
"version": "1.0.0",
"description": "File Upload test case",
"main": "server.js",
"dependencies": {
"hapi": "^8.0.0-rc8",
"joi": "^5.0.2"
},
"devDependencies": {
"code": "^1.2.1",
"form-data": "^0.1.4",
"lab": "^5.1.0",
"stream-to-promise": "^1.0.4"
},
"scripts": {
"test": "lab test.js",
"start": "node server.js"
},
"author": "Luis Couto <couto@15minuteslate.net>",
"license": "ISC"
}
var Hapi = require('hapi'),
Joi = require('joi'),
server = module.exports = new Hapi.Server();
server.connection({
port: 3000,
router: {
isCaseSensitive: true,
stripTrailingSlash: true
}
});
server.route({
path: '/photos',
method: 'POST',
config: {
payload: {
maxBytes: 1048576, // 1MB
output: 'stream', // We need to pipe the filedata to another server
parse: true
},
validate: {
payload: Joi.object({
name: Joi.string(),
description: Joi.string(),
filename: Joi.string(),
checksum: Joi.string(),
width: Joi.number(),
height: Joi.number(),
filedata: Joi.binary().encoding('base64')
})
},
handler: function (request, reply) {
// just return whatever payload might have
reply(request.payload);
}
}
});
if (!module.parent) {
server.start(function() {
console.log('Server started', server.info.uri);
});
}
var Code = require('code'),
FormData = require('form-data'),
Lab = require('lab'),
lab = exports.lab = Lab.script(),
server = require('./server.js'),
streamToPromise = require('stream-to-promise');
lab.experiment('Upload a photo', function () {
var image = {
name: 'Ren & Stimpy',
description: [
'Ren Höek is a hot-tempered, "asthma-hound" Chihuahua.',
'Stimpson "Stimpy" J. Cat is a three-year-old dim-witted and happy-go-lucky cat.'
].join('\n'),
filename: 'ren.jpg',
checksum: '5965ae98ecab44a2a29b87f90c681229',
width: 256,
height: 256,
filedata: new Buffer('lets imagine that this is an image')
};
lab.test('Should accept a multipart/form-data request', function (done) {
var form = new FormData();
// Fill the form object
Object.keys(image).forEach(function (key) {
form.append(key, image[key]);
});
streamToPromise(form).then(function (payload) {
server.inject({
url: '/photos',
method: 'POST',
headers: form.getHeaders(),
payload: payload
}, function (response) {
var result = response.result;
Code.expect(response.statusCode).to.equal(200);
Code.expect(result.name).to.equal(image.name);
Code.expect(result.description).to.equal(image.description);
Code.expect(result.filename).to.equal(image.filename);
Code.expect(result.checksum).to.equal(image.checksum);
Code.expect(result.width).to.equal(image.width);
Code.expect(result.height).to.equal(image.height);
done();
});
});
});
lab.test('Should accept a text/json request', function (done) {
// Convert the image buffer to a base64 string
image.filedata = image.filedata.toString('base64');
server.inject({
url: '/photos',
method: 'POST',
payload: image
}, function (response) {
var result = response.result;
Code.expect(response.statusCode).to.equal(200);
Code.expect(result.name).to.equal(image.name);
Code.expect(result.description).to.equal(image.description);
Code.expect(result.filename).to.equal(image.filename);
Code.expect(result.checksum).to.equal(image.checksum);
Code.expect(result.width).to.equal(image.width);
Code.expect(result.height).to.equal(image.height);
done();
});
});
});
@defra91
Copy link

defra91 commented Feb 24, 2017

Thank you very much, that's what I was looking for. One small question: my test does not pass the validation:

{ ...
filedata: Joi.binary().encoding('base64')
... }

I have a simple route with just a file in the payload.

@JustinMcConnell
Copy link

Thanks for this.

@Memogcia
Copy link

thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment