From f92ed133049f6a9d2f61a5d342ffa24d3d507966 Mon Sep 17 00:00:00 2001 From: seanstrom Date: Mon, 29 Dec 2014 14:52:06 -0800 Subject: [PATCH] Return empty buffer upon empty response body and encoding is set to null Instead of returning an empty string, an empty buffer will now be returned when the encoding is set to null. --- request.js | 2 +- tests/test-emptyBody.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/request.js b/request.js index 97e07e667..6bdd51436 100644 --- a/request.js +++ b/request.js @@ -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) }) diff --git a/tests/test-emptyBody.js b/tests/test-emptyBody.js index 68b2f6cdb..86bc6c0e9 100644 --- a/tests/test-emptyBody.js +++ b/tests/test-emptyBody.js @@ -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) @@ -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',