Skip to content

Commit b9bf2e6

Browse files
pietermeessindresorhus
authored andcommitted
Support an object in the agent option (#386)
1 parent 01c2636 commit b9bf2e6

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function requestAsEventEmitter(opts) {
6868
const ee = new EventEmitter();
6969
const requestUrl = opts.href || urlLib.resolve(urlLib.format(opts), opts.path);
7070
const redirects = [];
71+
const agents = typeof opts.agent === 'object' ? opts.agent : null;
7172
let retryCount = 0;
7273
let redirectUrl;
7374
let uploadBodySize;
@@ -81,6 +82,11 @@ function requestAsEventEmitter(opts) {
8182

8283
let fn = opts.protocol === 'https:' ? https : http;
8384

85+
if (agents) {
86+
const protocolName = opts.protocol === 'https:' ? 'https' : 'http';
87+
opts.agent = agents[protocolName] || opts.agent;
88+
}
89+
8490
if (opts.useElectronNet && process.versions.electron) {
8591
const electron = require('electron');
8692
fn = electron.net || electron.remote.net;
@@ -338,6 +344,7 @@ function asPromise(opts) {
338344
ee.on('error', reject);
339345
ee.on('uploadProgress', proxy.emit.bind(proxy, 'uploadProgress'));
340346
ee.on('downloadProgress', proxy.emit.bind(proxy, 'downloadProgress'));
347+
ee.on('redirect', proxy.emit.bind(proxy, 'redirect'));
341348
});
342349

343350
const promise = timeoutFn(cancelable);

readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,21 @@ got('todomvc.com', {
403403
});
404404
```
405405

406+
If you require different agents for different protocols, you can pass a map of agents to the `agent` option. This is necessary because a request to one protocol might redirect to another. In such a scenario, `got` will switch over to the right protocol agent for you.
407+
408+
```js
409+
const got = require('got');
410+
const HttpAgent = require('agentkeepalive');
411+
const HttpsAgent = HttpAgent.HttpsAgent;
412+
413+
got('sindresorhus.com', {
414+
agent: {
415+
http: new HttpAgent(),
416+
https: new HttpsAgent()
417+
}
418+
});
419+
```
420+
406421

407422
## Cookies
408423

test/redirects.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import {Agent as HttpAgent} from 'http';
2+
import {Agent as HttpsAgent} from 'https';
13
import test from 'ava';
24
import pem from 'pem';
35
import pify from 'pify';
6+
import sinon from 'sinon';
47
import got from '..';
58
import {createServer, createSSLServer} from './helpers/server';
69

@@ -35,12 +38,22 @@ test.before('setup', async () => {
3538
const cert = keys.certificate;
3639

3740
https = await createSSLServer({key, cert}); // eslint-disable-line object-property-newline
41+
http = await createServer();
42+
43+
// HTTPS Handlers
3844

3945
https.on('/', (req, res) => {
4046
res.end('https');
4147
});
4248

43-
http = await createServer();
49+
https.on('/httpsToHttp', (req, res) => {
50+
res.writeHead(302, {
51+
location: http.url
52+
});
53+
res.end();
54+
});
55+
56+
// HTTP Handlers
4457

4558
http.on('/', (req, res) => {
4659
res.end('reached');
@@ -173,6 +186,29 @@ test('redirects from http to https works', async t => {
173186
t.truthy((await got(`${http.url}/httpToHttps`, {rejectUnauthorized: false})).body);
174187
});
175188

189+
test('redirects from https to http works', async t => {
190+
t.truthy((await got(`${https.url}/httpsToHttp`, {rejectUnauthorized: false})).body);
191+
});
192+
193+
test('redirects from https to http works with an agent object', async t => {
194+
const httpAgent = new HttpAgent({keepAlive: true});
195+
const httpsAgent = new HttpsAgent({keepAlive: true});
196+
const httpSpy = sinon.spy(httpAgent, 'addRequest');
197+
const httpsSpy = sinon.spy(httpsAgent, 'addRequest');
198+
t.truthy((await got(`${https.url}/httpsToHttp`, {
199+
rejectUnauthorized: false,
200+
agent: {
201+
http: httpAgent,
202+
https: httpsAgent
203+
}
204+
})).body);
205+
t.true(httpSpy.calledOnce);
206+
t.true(httpsSpy.calledOnce);
207+
// Make sure to close all open sockets
208+
httpAgent.destroy();
209+
httpsAgent.destroy();
210+
});
211+
176212
test('redirects works with lowercase method', async t => {
177213
const body = (await got(`${http.url}/relative`, {method: 'head'})).body;
178214
t.is(body, '');

0 commit comments

Comments
 (0)