Skip to content

Commit

Permalink
Allow falsy values as payload for HTTP post, put and patch
Browse files Browse the repository at this point in the history
Fixes #95
  • Loading branch information
basti1302 committed Apr 21, 2017
1 parent 22d1d9e commit 969ac75
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,13 @@ Builder.prototype.linkHeader = function() {

function createInitialTraversalState(self, body) {

var normalizedBody =
(body !== null && typeof body !== undefined) ? body : null;

var traversalState = {
aborted: false,
adapter: self.adapter || null,
body: body || null,
body: normalizedBody,
callbackHasBeenCalledAfterAbort: false,
autoHeaders: self.setsAutoHeaders(),
contentNegotiation: self.doesContentNegotiation(),
Expand Down
2 changes: 1 addition & 1 deletion lib/http_requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function executeHttpGet(t, callback) {
// request, Traverson uses exectueHttpGet.
exports.executeHttpRequest = function(t, request, method, callback) {
var requestOptions = getOptionsForStep(t);
if (t.body) {
if (t.body !== null && typeof t.body !== 'undefined') {
requestOptions.body = requestOptions.jsonReplacer ?
t.body : JSON.stringify(t.body);
}
Expand Down
1 change: 0 additions & 1 deletion test/localhost.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ describe('Traverson (when tested against a local server)', function() {
);
});


it('should yield the last URL', function(done) {
api
.newRequest()
Expand Down
23 changes: 23 additions & 0 deletions test/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ describe('post method', function() {
);
});

it('should post falsy values as payload',
function(done) {
post
.withArgs(postUri, sinon.match.object, sinon.match.func)
.callsArgWithAsync(2, null, result);

api
.newRequest()
.follow('post_link')
.post(0, callback);

waitFor(
function() { return callback.called; },
function() {
expect(callback).to.have.been.calledWith(null, result,
sinon.match.object);
expect(post.firstCall.args[1].body).to.exist;
expect(post.firstCall.args[1].body).to.equal('0');
done();
}
);
});

it('should convert the response to an object', function(done) {
post
.withArgs(postUri, sinon.match.object, sinon.match.func)
Expand Down
23 changes: 23 additions & 0 deletions test/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ describe('put method', function() {
);
});

it('should put falsy values as payload',
function(done) {
put
.withArgs(putUri, sinon.match.object, sinon.match.func)
.callsArgWithAsync(2, null, result);

api
.newRequest()
.follow('put_link')
.put(false, callback);

waitFor(
function() { return callback.called; },
function() {
expect(callback).to.have.been.calledWith(null, result,
sinon.match.object);
expect(put.firstCall.args[1].body).to.exist;
expect(put.firstCall.args[1].body).to.equal('false');
done();
}
);
});

it('should convert the response to an object', function(done) {
put
.withArgs(putUri, sinon.match.object, sinon.match.func)
Expand Down

0 comments on commit 969ac75

Please sign in to comment.