diff --git a/lib/collection/form-param.js b/lib/collection/form-param.js index 5c825d403..3fe3577d7 100644 --- a/lib/collection/form-param.js +++ b/lib/collection/form-param.js @@ -24,6 +24,7 @@ _.inherit(( this.value = options.value || ''; this.type = options.type; this.src = options.src; + this.content = options.content; this.contentType = options.contentType; }), Property); diff --git a/lib/collection/request-body.js b/lib/collection/request-body.js index 149b24500..c4c7a0117 100644 --- a/lib/collection/request-body.js +++ b/lib/collection/request-body.js @@ -204,20 +204,6 @@ _.assign(RequestBody.prototype, /** @lends RequestBody.prototype */ { } return _.isEmpty(data); // catch all for remaining data modes - }, - - /** - * Convert the request body to JSON compatible plain object - * - * @returns {Object} - */ - toJSON () { - var obj = PropertyBase.toJSON(this); - - // make sure that file content is removed because it is non-serializable ReadStream - _.unset(obj, 'file.content'); - - return obj; } }); diff --git a/test/unit/form-param.test.js b/test/unit/form-param.test.js index 3e17ffd0c..d8bc6ec72 100644 --- a/test/unit/form-param.test.js +++ b/test/unit/form-param.test.js @@ -68,7 +68,7 @@ describe('FormParam', function () { expect(fp.toJSON()).to.eql(definition); }); - it('should correctly handle param with type `file`', function () { + it('should correctly handle param with type `file` and src', function () { var definition = { key: 'foo', src: 'fileSrc', @@ -80,6 +80,18 @@ describe('FormParam', function () { expect(fp.toJSON()).to.eql(definition); }); + it('should correctly handle param with type `file` and content', function () { + var definition = { + key: 'foo', + content: 'Base64 representation of buffer', + type: 'file', + contentType: 'application/json' + }, + fp = new FormParam(definition); + + expect(fp.toJSON()).to.eql(definition); + }); + it('should not have value for param with type `file`', function () { var fp = new FormParam({ key: 'foo', diff --git a/test/unit/request-body.test.js b/test/unit/request-body.test.js index 5fe647707..400b9bd59 100644 --- a/test/unit/request-body.test.js +++ b/test/unit/request-body.test.js @@ -1,4 +1,5 @@ -var expect = require('chai').expect, +var Stream = require('stream'), + expect = require('chai').expect, rawRequestBody = require('../fixtures').requestData, RequestBody = require('../../lib/index.js').RequestBody, QueryParam = require('../../lib/index.js').QueryParam, @@ -525,14 +526,27 @@ describe('RequestBody', function () { expect(rBody.toJSON()).to.eql(definition); }); - it('should not have content in file body', function () { + it('should have content in file body', function () { var definition = { mode: 'file', - file: { src: 'fileSrc', content: 'this should be removed' } + file: { src: 'fileSrc', content: 'this should not be removed' } }, rBody = new RequestBody(definition); - expect(rBody.toJSON().file).to.not.have.property('content'); + expect(rBody.toJSON().file).to.have.property('content'); + expect(rBody.toJSON().file.content).to.eql('this should not be removed'); + }); + + it('should correctly handle request body with non serializable data', function () { + var stream = new Stream.Readable(), + definition = { + mode: 'file', + file: { content: stream } + }, + rBody = new RequestBody(definition); + + expect(rBody.toJSON()).to.eql(definition); + expect(rBody.toJSON().file.content).to.eql(stream); }); }); });