agent-base
Advanced tools
Comparing version 4.0.1 to 4.1.0
4.1.0 / 2017-06-26 | ||
================== | ||
* mix in Agent options into Request options | ||
* throw when nothing is returned from agent-base callback | ||
* do not modify the options object for https requests | ||
4.0.1 / 2017-06-13 | ||
@@ -3,0 +10,0 @@ ================== |
28
index.js
@@ -42,2 +42,4 @@ 'use strict'; | ||
this.timeout = (opts && opts.timeout) || null; | ||
this.options = opts; | ||
} | ||
@@ -66,4 +68,16 @@ inherits(Agent, EventEmitter); | ||
) { | ||
const opts = Object.assign({}, _opts); | ||
const ownOpts = Object.assign({}, _opts); | ||
// set default `host` for HTTP to localhost | ||
if (null == ownOpts.host) { | ||
ownOpts.host = 'localhost'; | ||
} | ||
// set default `port` for HTTP if none was explicitly specified | ||
if (null == ownOpts.port) { | ||
ownOpts.port = ownOpts.secureEndpoint ? 443 : 80; | ||
} | ||
const opts = Object.assign({}, this.options, ownOpts); | ||
if (opts.host && opts.path) { | ||
@@ -76,7 +90,2 @@ // if both a `host` and `path` are specified then it's most likely the | ||
// set default `port` for HTTP if none was explicitly specified | ||
if (null == opts.port) { | ||
opts.port = opts.secureEndpoint ? 443 : 80; | ||
} | ||
delete opts.agent; | ||
@@ -128,3 +137,8 @@ delete opts.hostname; | ||
} | ||
req.onSocket(socket); | ||
if (socket) { | ||
req.onSocket(socket); | ||
} else { | ||
const err = new Error(`no Duplex stream was returned to agent-base for \`${req.method} ${req.path}\``); | ||
onerror(err); | ||
} | ||
} | ||
@@ -131,0 +145,0 @@ |
{ | ||
"name": "agent-base", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Turn a function into an `http.Agent` instance", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
'use strict'; | ||
const url = require('url'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
@@ -14,5 +13,8 @@ | ||
https.request = (function(request) { | ||
return function(options, cb) { | ||
return function(_options, cb) { | ||
let options | ||
if (typeof options === 'string') { | ||
options = url.parse(options); | ||
options = url.parse(_options); | ||
} else { | ||
options = Object.assign({}, _options); | ||
} | ||
@@ -19,0 +21,0 @@ if (null == options.port) { |
@@ -46,23 +46,56 @@ /** | ||
}); | ||
it('should be mixed in with HTTP request options', function(done) { | ||
var agent = new Agent({ | ||
host: 'my-proxy.com', | ||
port: 3128, | ||
foo: 'bar' | ||
}); | ||
agent.callback = function(req, opts, fn) { | ||
assert.equal('bar', opts.foo); | ||
assert.equal('a', opts.b); | ||
// `host` and `port` are special-cases, and should always be | ||
// overwritten in the request `opts` inside the agent-base callback | ||
assert.equal('localhost', opts.host); | ||
assert.equal(80, opts.port); | ||
done(); | ||
}; | ||
var opts = { | ||
b: 'a', | ||
agent: agent | ||
}; | ||
http.get(opts); | ||
}); | ||
}); | ||
describe('`this` context', function() { | ||
it('should be the Agent instance', function(done) { | ||
var called = false; | ||
var agent = new Agent(); | ||
agent.callback = function () { | ||
called = true; | ||
assert.equal(this, agent); | ||
done(); | ||
} | ||
var info = url.parse('http://127.0.0.1/foo'); | ||
info.agent = agent; | ||
http.get(info); | ||
var req = http.get(info); | ||
req.on('error', function(err) { | ||
assert(/no Duplex stream was returned/.test(err.message)); | ||
done(); | ||
}); | ||
}) | ||
it('should be the Agent instance with callback signature', function(done) { | ||
var called = false; | ||
var agent = new Agent(); | ||
agent.callback = function (req, opts, fn) { | ||
called = true; | ||
assert.equal(this, agent); | ||
done(); | ||
fn(); | ||
} | ||
var info = url.parse('http://127.0.0.1/foo'); | ||
info.agent = agent; | ||
http.get(info); | ||
var req = http.get(info); | ||
req.on('error', function(err) { | ||
assert(/no Duplex stream was returned/.test(err.message)); | ||
done(); | ||
}); | ||
}) | ||
@@ -397,2 +430,19 @@ }) | ||
it('should not modify the passed in Options object', function(done) { | ||
var called = false; | ||
var agent = new Agent(function(req, opts, fn) { | ||
called = true; | ||
assert.equal(true, opts.secureEndpoint); | ||
assert.equal(443, opts.port); | ||
assert.equal('localhost', opts.host); | ||
}); | ||
var opts = { agent: agent }; | ||
var req = https.request(opts); | ||
assert.equal(true, called); | ||
assert.equal(false, 'secureEndpoint' in opts); | ||
assert.equal(false, 'port' in opts); | ||
done(); | ||
}); | ||
it('should work for basic HTTPS requests', function(done) { | ||
@@ -399,0 +449,0 @@ var called = false; |
Sorry, the diff of this file is not supported yet
31036
713
5