From 320bfebb97dc106c954274a844864d8947df2e99 Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 31 May 2010 13:39:27 -0700 Subject: [PATCH] only send Content-Length if a body is available --- lib/index.js | 13 +++++++------ test/scoped_get_test.js | 3 ++- test/scoped_request_test.js | 4 ++-- test/scoped_url_test.js | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/index.js b/lib/index.js index 37efd28..9d8d5e8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,16 +25,17 @@ ScopedClient.prototype.request = function(method, reqBody, callback) { var err, port, client, req try { - this.options.headers.host = this.options.hostname - if(reqBody && reqBody.length > 0) - this.options.headers['Content-Length'] = reqBody.length + var headers = extend({}, this.options.headers) + var sendingData = method.match(/^P/) && reqBody && reqBody.length > 0 + headers.Host = this.options.hostname + if(sendingData) headers['Content-Length'] = reqBody.length port = this.options.port || ScopedClient.defaultPort[this.options.protocol] || 80 client = http.createClient(port, this.options.hostname) - req = client.request(method, this.fullPath(), this.options.headers) + req = client.request(method, this.fullPath(), headers) - if(reqBody && reqBody.length > 0) req.write(reqBody, 'utf-8') + if(sendingData) req.write(reqBody, 'utf-8') } catch(e) { err = e } if(callback) callback(err, req) @@ -51,7 +52,7 @@ ScopedClient.prototype.request = function(method, reqBody, callback) { body += chunk } catch(e) { err = e } }) - + resp.addListener('end', function() { callback(err, resp, body) }) diff --git a/test/scoped_get_test.js b/test/scoped_get_test.js index d1ea1d7..b4e71d7 100644 --- a/test/scoped_get_test.js +++ b/test/scoped_get_test.js @@ -12,7 +12,8 @@ var server = http.createServer(function(req, res) { server.listen(9999) server.addListener('listening', function() { - client = ScopedClient.create('http://localhost:9999', {headers: {accept: 'text/plain'}}) + client = ScopedClient.create('http://localhost:9999', + {headers: {accept: 'text/plain'}}) client.get()(function(err, resp, body) { called++ assert.equal(200, resp.statusCode) diff --git a/test/scoped_request_test.js b/test/scoped_request_test.js index c1483df..4b83b7e 100644 --- a/test/scoped_request_test.js +++ b/test/scoped_request_test.js @@ -24,6 +24,7 @@ var server = http.createServer(function(req, res) { }) }) server.listen(9999) + server.addListener('listening', function() { client = ScopedClient.create('http://localhost:9999') .headers({'user-agent':'bob'}) @@ -37,9 +38,8 @@ server.addListener('listening', function() { assert.equal('PUT', curr) assert.equal('fred', ua) assert.equal("PUT hello: yea fred", body) - client.userAgent('jim').head()(function(err, resp, body) { + client.head()(function(err, resp, body) { called++ - assert.equal('jim', ua) assert.equal('HEAD', curr) server.close() }) diff --git a/test/scoped_url_test.js b/test/scoped_url_test.js index e68edcc..b519b08 100644 --- a/test/scoped_url_test.js +++ b/test/scoped_url_test.js @@ -20,7 +20,7 @@ assert.equal('user:monkey', client.options.auth) assert.equal('https', client.options.protocol) assert.deepEqual({a:1}, client.options.query) -client.path('qux').auth('user:pw').port(82).hash() +client.path('qux').auth('user:pw').port(82) assert.equal('/bar/baz/qux', client.options.pathname) assert.equal('user:pw', client.options.auth) assert.equal(82, client.options.port)