From 731faa15a15de5514d50f32e2b77d6d7168912ed Mon Sep 17 00:00:00 2001 From: Ben Marvell Date: Mon, 9 Dec 2013 14:05:20 +0000 Subject: [PATCH] Set Content-Length for empty data response. --- lib/restler.js | 5 ++++- test/restler.js | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/restler.js b/lib/restler.js index 466107a..f8e9649 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -70,11 +70,14 @@ function Request(uri, options) { this.headers['Content-Type'] = 'application/x-www-form-urlencoded'; this.headers['Content-Length'] = this.options.data.length; } - if(typeof this.options.data == 'string') { + if (typeof this.options.data == 'string') { var buffer = new Buffer(this.options.data, this.options.encoding || 'utf8'); this.options.data = buffer; this.headers['Content-Length'] = buffer.length; } + if (!this.options.data) { + this.headers['Content-Length'] = 0; + } } var proto = (this.url.protocol == 'https:') ? https : http; diff --git a/test/restler.js b/test/restler.js index c56759d..60de000 100644 --- a/test/restler.js +++ b/test/restler.js @@ -666,6 +666,13 @@ module.exports['Content-Length'] = { test.equal(36, data, 'should byte-size content-length'); test.done(); }); + }, + + 'None data request content length': function (test) { + rest.post(host).on('complete', function(data) { + test.equal(0, data, 'should set content-length') + test.done(); + }) } };