Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing of content in request with file type body #1326

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/collection/form-param.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 0 additions & 14 deletions lib/collection/request-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});

Expand Down
14 changes: 13 additions & 1 deletion test/unit/form-param.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
22 changes: 18 additions & 4 deletions test/unit/request-body.test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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');
dev-sharma-08 marked this conversation as resolved.
Show resolved Hide resolved
});

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);
dev-sharma-08 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would the result of serialisation of a ReadStream be? Wouldn't it be unusable? Do we want to keep this? @codenirvana

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toJSON() will have no effect on the readStream. It will remain usable, I have checked this.
Regarding wether we want to keep it @codenirvana can share his opinion.

});
});
});
Loading