-
-
Save smahi/a838c7ae1c171672d183d2fe46839e64 to your computer and use it in GitHub Desktop.
Revisions
-
couto revised this gist
Dec 3, 2014 . 2 changed files with 50 additions and 36 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,15 +2,21 @@ 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: { @@ -31,6 +37,7 @@ server.route({ } }); if (!module.parent) { server.start(function() { console.log('Server started', server.info.uri); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,16 +7,27 @@ var Code = require('code'), 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) { @@ -26,48 +37,44 @@ lab.experiment('Upload a photo', function () { 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(); }); }); }); -
couto revised this gist
Dec 3, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ server.route({ config: { payload: { maxBytes: 1048576, // 1MB output: 'stream', // we need to pipe the image to another server parse: true }, validate: { -
couto revised this gist
Dec 3, 2014 . 2 changed files with 28 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,9 +2,7 @@ var Hapi = require('hapi'), Joi = require('joi'), server = module.exports = new Hapi.Server(); server.connection({ port: 3000 }); server.route({ path: '/photos', @@ -18,7 +16,12 @@ server.route({ 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) { This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,11 @@ lab.experiment('Upload a photo', function () { form.append('name', 'Ren'); form.append('description', 'Ren Höek is a hot-tempered, "asthma-hound" Chihuahua.'); form.append('filename', 'ren.jpg'); form.append('checksum', '5965ae98ecab44a2a29b87f90c681229'); form.append('width', 256); form.append('height', 256); form.append('filedata', new Buffer('lets imagine that this is an image')); streamToPromise(form).then(function (payload) { @@ -24,8 +29,11 @@ lab.experiment('Upload a photo', function () { Code.expect(response.statusCode).to.equal(200); Code.expect(response.result.name).to.equal('Ren'); Code.expect(response.result.filename).to.equal('ren.jpg'); Code.expect(response.result.checksum).to.equal('5965ae98ecab44a2a29b87f90c681229'); Code.expect(response.result.width).to.equal(256); Code.expect(response.result.height).to.equal(256); done(); }); @@ -39,15 +47,24 @@ lab.experiment('Upload a photo', function () { method: 'POST', payload: { name: 'Stimpy', description: 'Stimpson "Stimpy" J. Cat is a three-year-old dim-witted and happy-go-lucky cat.', filename: 'stimpy.jpg', checksum: '68b834fb9bbaf6b49d4a84cc85b44395', width: 256, height: 256, filedata: new Buffer('lets pretend this is an image').toString('base64') } }, function (response) { console.log(response); Code.expect(response.statusCode).to.equal(200); Code.expect(response.result.name).to.equal('Stimpy'); Code.expect(response.result.description).to.equal('Stimpson "Stimpy" J. Cat is a three-year-old dim-witted and happy-go-lucky cat.'); Code.expect(response.result.filename).to.equal('stimpy.jpg'); Code.expect(response.result.checksum).to.equal('68b834fb9bbaf6b49d4a84cc85b44395'); Code.expect(response.result.width).to.equal(256); Code.expect(response.result.height).to.equal(256); done(); }); -
couto created this gist
Dec 3, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ { "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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ var Hapi = require('hapi'), Joi = require('joi'), server = module.exports = new Hapi.Server(); server.connection({ port: 3000 }); server.route({ path: '/photos', method: 'POST', config: { payload: { maxBytes: 1048576, // 1MB output: 'stream', parse: true }, validate: { payload: Joi.object({ name: Joi.string(), description: Joi.string() }) }, 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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ 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 () { lab.test('Should accept a multipart/form-data request', function (done) { var form = new FormData(); form.append('name', 'Ren'); form.append('description', 'Ren Höek is a hot-tempered, "asthma-hound" Chihuahua.'); streamToPromise(form).then(function (payload) { server.inject({ url: '/photos', method: 'POST', headers: form.getHeaders(), payload: payload }, function (response) { Code.expect(response.statusCode).to.equal(200); Code.expect(response.result.name).to.equal('Ren'); Code.expect(response.result.description).to.equal('Ren Höek is a hot-tempered, "asthma-hound" Chihuahua.'); done(); }); }); }); lab.test('Should accept a text/json request', function (done) { server.inject({ url: '/photos', method: 'POST', payload: { name: 'Stimpy', description: 'Stimpson "Stimpy" J. Cat is a three-year-old dim-witted and happy-go-lucky cat.' } }, function (response) { console.log(response); Code.expect(response.statusCode).to.equal(200); Code.expect(response.result.name).to.equal('Stimpy'); Code.expect(response.result.description).to.equal('Stimpson "Stimpy" J. Cat is a three-year-old[7] dim-witted and happy-go-lucky cat.'); done(); }); }); });