Skip to content

Commit

Permalink
only send Content-Length if a body is available
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed May 31, 2010
1 parent 98fe302 commit 320bfeb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
13 changes: 7 additions & 6 deletions lib/index.js
Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down
3 changes: 2 additions & 1 deletion test/scoped_get_test.js
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/scoped_request_test.js
Expand Up @@ -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'})
Expand All @@ -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()
})
Expand Down
2 changes: 1 addition & 1 deletion test/scoped_url_test.js
Expand Up @@ -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)
Expand Down

0 comments on commit 320bfeb

Please sign in to comment.