Last active
February 13, 2022 22:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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 <[email protected]>", | |
"license": "ISC" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for this!