Skip to content

Commit

Permalink
Merge pull request #98 from openhoat/master
Browse files Browse the repository at this point in the history
write method invocation into json method
  • Loading branch information
howardabrams committed Sep 14, 2016
2 parents b398d5a + 4d350e4 commit 1c0bdb4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mockResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@ function createResponse(options) {
if (typeof a === 'number') {
mockResponse.statusCode = a;
} else {
_data += JSON.stringify(a);
mockResponse.write(JSON.stringify(a), 'utf8');
}
}
if (b) {
if (typeof b === 'number') {
mockResponse.statusCode = b;
} else {
_data += JSON.stringify(b);
mockResponse.write(JSON.stringify(b), 'utf8');
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/lib/mockResponse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,18 @@ describe('mockResponse', function() {
expect(response.emit).to.have.been.calledWith('end');
});

// reference : https://github.com/howardabrams/node-mocks-http/pull/98
it('should call .write()', function() {
var originalWrite = response.write.bind(response);
var hackedContent = JSON.stringify({foo: 'bar'});
response.write = function(data, encoding) {
console.log('data :', data);
return originalWrite(hackedContent, encoding);
};
response.json({hello: 'world'});
expect(response._getData()).to.eql(hackedContent);
});

});

// TODO: fix in 2.0; method should mimic Express Response.jsonp()
Expand Down Expand Up @@ -1092,4 +1104,5 @@ describe('mockResponse', function() {

});


});

0 comments on commit 1c0bdb4

Please sign in to comment.