Skip to content

Commit

Permalink
Return empty buffer upon empty response body and encoding is set to null
Browse files Browse the repository at this point in the history
Instead of returning an empty string, an empty buffer will now be
returned when the encoding is set to null.
  • Loading branch information
seanstrom committed Dec 29, 2014
1 parent 4455e53 commit f92ed13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion request.js
Expand Up @@ -1321,7 +1321,7 @@ Request.prototype.onRequestResponse = function (response) {
}
debug('emitting complete', self.uri.href)
if(typeof response.body === 'undefined' && !self._json) {
response.body = ''
response.body = self.encoding === null ? new Buffer(0) : ''
}
self.emit('complete', response, response.body)
})
Expand Down
14 changes: 13 additions & 1 deletion tests/test-emptyBody.js
Expand Up @@ -15,7 +15,7 @@ tape('setup', function(t) {
})
})

tape('empty body', function(t) {
tape('empty body with encoding', function(t) {
request('http://localhost:6767', function(err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
Expand All @@ -24,6 +24,18 @@ tape('empty body', function(t) {
})
})

tape('empty body without encoding', function(t) {
request({
url: 'http://localhost:6767',
encoding: null
}, function(err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.same(body, new Buffer(0))
t.end()
})
})

tape('empty JSON body', function(t) {
request({
url: 'http://localhost:6767',
Expand Down

0 comments on commit f92ed13

Please sign in to comment.