Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add ScopedClient methods for header/headers/userAgent

  • Loading branch information...
commit 89794ac66244bcab1bfa739dcf0da5cf513b0717 1 parent cec6539
@technoweenie technoweenie authored
Showing with 25 additions and 5 deletions.
  1. +14 −0 lib/index.js
  2. +11 −5 test/scoped_request_test.js
View
14 lib/index.js
@@ -138,6 +138,20 @@ ScopedClient.prototype.hash = function(h) {
return this
}
+ScopedClient.prototype.header = function (name, value) {
+ this.options.headers[name] = value
+ return this
+}
+
+ScopedClient.prototype.headers = function(h) {
+ extend(this.options.headers, h)
+ return this
+}
+
+ScopedClient.prototype.userAgent = function(ua) {
+ return this.header('user-agent', ua)
+}
+
ScopedClient.prototype.buildOptions = function() {
var options = {}
, i = 0
View
16 test/scoped_request_test.js
@@ -4,6 +4,7 @@ var ScopedClient = require('../lib')
, assert = require('assert')
, called = 0
, curr = null
+ , ua = null
, client
var server = http.createServer(function(req, res) {
@@ -13,7 +14,8 @@ var server = http.createServer(function(req, res) {
})
req.addListener('end', function() {
curr = req.method
- var respBody = curr + ' hello: ' + body
+ ua = req.headers['user-agent']
+ var respBody = curr + ' hello: ' + body + ' ' + ua
res.writeHead(200, {'content-type': 'text/plain',
'content-length': respBody.length})
if(curr != 'HEAD')
@@ -24,16 +26,20 @@ var server = http.createServer(function(req, res) {
server.listen(9999)
server.addListener('listening', function() {
client = ScopedClient.create('http://localhost:9999')
+ .headers({'user-agent':'bob'})
client.del()(function(err, resp, body) {
called++
assert.equal('DELETE', curr)
- assert.equal("DELETE hello: ", body)
- client.put('yea')(function(err, resp, body) {
+ assert.equal('bob', ua)
+ assert.equal("DELETE hello: bob", body)
+ client.header('user-agent', 'fred').put('yea')(function(err, resp, body) {
called++
assert.equal('PUT', curr)
- assert.equal("PUT hello: yea", body)
- client.head()(function(err, resp, body) {
+ assert.equal('fred', ua)
+ assert.equal("PUT hello: yea fred", body)
+ client.userAgent('jim').head()(function(err, resp, body) {
called++
+ assert.equal('jim', ua)
assert.equal('HEAD', curr)
server.close()
})
Please sign in to comment.
Something went wrong with that request. Please try again.