diff --git a/lib/payload.js b/lib/payload.js index 2aed46b2e..3474b4e15 100755 --- a/lib/payload.js +++ b/lib/payload.js @@ -139,7 +139,7 @@ exports.read = function (request, next) { // Set parser - internals.parse(payload.toString('utf8'), req.headers, function (err, result) { + internals.parse(payload, req.headers, function (err, result) { if (err) { return finish(err); @@ -167,7 +167,7 @@ internals.isGzip = function (compareBuf) { }; -internals.parse = function (result, headers, callback) { +internals.parse = function (payload, headers, callback) { callback = Utils.nextTick(callback); @@ -179,7 +179,7 @@ internals.parse = function (result, headers, callback) { // Text if (mime.match(/^text\/.+$/)) { - return callback(result); + return callback(payload.toString('utf8')); } // JSON @@ -189,7 +189,7 @@ internals.parse = function (result, headers, callback) { var error = null; try { - obj = JSON.parse(result); + obj = JSON.parse(payload.toString('utf8')); } catch (exp) { error = Boom.badRequest('Invalid request payload format'); @@ -201,7 +201,7 @@ internals.parse = function (result, headers, callback) { // Form-encoded if (mime === 'application/x-www-form-urlencoded') { - return callback(null, Querystring.parse(result)); + return callback(null, Querystring.parse(payload.toString('utf8'))); } // Multipart @@ -237,7 +237,7 @@ internals.parse = function (result, headers, callback) { }); form.writeHeaders(headers); - form.write(new Buffer(result)); + form.write(new Buffer(payload)); return; } diff --git a/test/integration/payload.js b/test/integration/payload.js index 656785c24..ff16f81cf 100755 --- a/test/integration/payload.js +++ b/test/integration/payload.js @@ -7,6 +7,7 @@ var Http = require('http'); var Path = require('path'); var Stream = require('stream'); var Zlib = require('zlib'); +var Assert = require('assert'); var Hapi = require('../..'); @@ -542,11 +543,19 @@ describe('Payload', function () { it('parses a file correctly', function (done) { - var file = Fs.readFileSync(Path.join(__dirname, '../../images/hapi.png')); + var path = Path.join(__dirname, '../../images/hapi.png'); + var stats = Fs.statSync(path); + var fileStream = Fs.createReadStream(path); + var fileContents = Fs.readFileSync(path, { encoding: 'binary' }); + var fileHandler = function (request) { expect(request.raw.req.headers['content-type']).to.contain('multipart/form-data'); - expect(request.payload['my_file']).to.contain('Photoshop'); + expect(request.payload['my_file'].size).to.equal(stats.size); + + var tmpContents = Fs.readFileSync(request.payload['my_file'].path, { encoding: 'binary' }); + Assert.deepEqual(fileContents, tmpContents); + done(); }; @@ -556,7 +565,7 @@ describe('Payload', function () { var r = Request.post(server.settings.uri + '/file'); var form = r.form(); - form.append('my_file', file); + form.append('my_file', fileStream); }); }); });