diff --git a/packages/agent-base/test/test.ts b/packages/agent-base/test/test.ts index 9004ee59..1eaf4970 100644 --- a/packages/agent-base/test/test.ts +++ b/packages/agent-base/test/test.ts @@ -462,6 +462,7 @@ describe('Agent (TypeScript)', () => { assert(gotReq); expect(connectCount).toEqual(1); expect(res.headers.connection).toEqual('keep-alive'); + expect(res.statusCode).toEqual(200); res.resume(); const s1 = res.socket; @@ -473,6 +474,7 @@ describe('Agent (TypeScript)', () => { }); expect(connectCount).toEqual(1); expect(res2.headers.connection).toEqual('keep-alive'); + expect(res2.statusCode).toEqual(200); assert(res2.socket === s1); res2.resume(); diff --git a/packages/http-proxy-agent/test/test.ts b/packages/http-proxy-agent/test/test.ts index 3eb5eb68..393e8c94 100644 --- a/packages/http-proxy-agent/test/test.ts +++ b/packages/http-proxy-agent/test/test.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as http from 'http'; import * as https from 'https'; import assert from 'assert'; +import { once } from 'events'; import { createProxy, ProxyServer } from 'proxy'; import { listen } from 'async-listen'; import { json, req } from 'agent-base'; @@ -187,5 +188,32 @@ describe('HttpProxyAgent', () => { const body = await json(res); expect(body.host).toEqual(httpServerUrl.hostname); }); + + it('should work with `keepAlive: true`', async () => { + httpServer.on('request', (req, res) => { + res.end(JSON.stringify(req.headers)); + }); + + const agent = new HttpProxyAgent(proxyUrl, { keepAlive: true }); + + try { + const res = await req(httpServerUrl, { agent }); + expect(res.headers.connection).toEqual('keep-alive'); + expect(res.statusCode).toEqual(200); + res.resume(); + const s1 = res.socket; + await once(s1, 'free'); + + const res2 = await req(httpServerUrl, { agent }); + expect(res2.headers.connection).toEqual('keep-alive'); + expect(res2.statusCode).toEqual(200); // FAIL, received 400 + res2.resume(); + const s2 = res2.socket; + assert(s1 === s2); + await once(s2, 'free'); + } finally { + agent.destroy(); + } + }); }); }); diff --git a/packages/https-proxy-agent/test/test.ts b/packages/https-proxy-agent/test/test.ts index 93bd43e9..4acc4077 100644 --- a/packages/https-proxy-agent/test/test.ts +++ b/packages/https-proxy-agent/test/test.ts @@ -215,12 +215,14 @@ describe('HttpsProxyAgent', () => { try { const res = await req(serverUrl, { agent }); expect(res.headers.connection).toEqual('keep-alive'); + expect(res.statusCode).toEqual(200); res.resume(); const s1 = res.socket; await once(s1, 'free'); const res2 = await req(serverUrl, { agent }); expect(res2.headers.connection).toEqual('keep-alive'); + expect(res2.statusCode).toEqual(200); res2.resume(); const s2 = res2.socket; assert(s1 === s2); @@ -332,6 +334,7 @@ describe('HttpsProxyAgent', () => { rejectUnauthorized: false, }); expect(res.headers.connection).toEqual('keep-alive'); + expect(res.statusCode).toEqual(200); res.resume(); const s1 = res.socket; await once(s1, 'free'); @@ -341,6 +344,7 @@ describe('HttpsProxyAgent', () => { rejectUnauthorized: false, }); expect(res2.headers.connection).toEqual('keep-alive'); + expect(res2.statusCode).toEqual(200); res2.resume(); const s2 = res2.socket; assert(s1 === s2); diff --git a/packages/pac-proxy-agent/test/test.ts b/packages/pac-proxy-agent/test/test.ts index 10121944..0cff6117 100644 --- a/packages/pac-proxy-agent/test/test.ts +++ b/packages/pac-proxy-agent/test/test.ts @@ -8,6 +8,7 @@ import { listen } from 'async-listen'; import { ProxyServer, createProxy } from 'proxy'; import { req, json } from 'agent-base'; import { PacProxyAgent } from '../src'; +import { once } from 'events'; const sslOptions = { key: fs.readFileSync(`${__dirname}/ssl-cert-snakeoil.key`), @@ -244,6 +245,44 @@ describe('PacProxyAgent', () => { assert.equal(httpServerUrl.host, data.host); assert(gotReq); }, 10000); // This test is slow on Windows :/ + + it('should work with `keepAlive: true`', async () => { + httpServer.on('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + }); + + function FindProxyForURL() { + return 'PROXY localhost:PORT;'; + } + + const uri = `data:,${encodeURIComponent( + FindProxyForURL.toString().replace('PORT', proxyServerUrl.port) + )}`; + const agent = new PacProxyAgent(uri, { keepAlive: true }); + + try { + const res = await req(new URL('/test', httpServerUrl), { + agent, + }); + expect(res.headers.connection).toEqual('keep-alive'); + expect(res.statusCode).toEqual(200); + res.resume(); + const s1 = res.socket; + await once(s1, 'free'); + + const res2 = await req(new URL('/test', httpServerUrl), { + agent, + }); + expect(res2.headers.connection).toEqual('keep-alive'); + expect(res2.statusCode).toEqual(200); // FAIL, received 400 + res2.resume(); + const s2 = res2.socket; + assert(s1 === s2); + await once(s2, 'free'); + } finally { + agent.destroy(); + } + }); }); describe('"https" module', () => { @@ -358,5 +397,46 @@ describe('PacProxyAgent', () => { assert.equal(proxyCount, 4); assert(gotReq); }, 10000); // This test is slow on Windows :/ + + it('should work with `keepAlive: true`', async () => { + httpsServer.on('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + }); + + function FindProxyForURL() { + return 'PROXY localhost:PORT;'; + } + + const uri = `data:,${encodeURIComponent( + FindProxyForURL.toString().replace('PORT', proxyServerUrl.port) + )}`; + const agent = new PacProxyAgent(uri, { + keepAlive: true, + rejectUnauthorized: false, + }); + + try { + const res = await req(new URL('/test', httpsServerUrl), { + agent, + }); + expect(res.headers.connection).toEqual('keep-alive'); + expect(res.statusCode).toEqual(200); + res.resume(); + const s1 = res.socket; + await once(s1, 'free'); + + const res2 = await req(new URL('/test', httpsServerUrl), { + agent, + }); + expect(res2.headers.connection).toEqual('keep-alive'); + expect(res2.statusCode).toEqual(200); + res2.resume(); + const s2 = res2.socket; + assert(s1 === s2); + await once(s2, 'free'); + } finally { + agent.destroy(); + } + }); }); }); diff --git a/packages/proxy-agent/test/test.ts b/packages/proxy-agent/test/test.ts index 5887ea18..599857cb 100644 --- a/packages/proxy-agent/test/test.ts +++ b/packages/proxy-agent/test/test.ts @@ -158,6 +158,7 @@ describe('ProxyAgent', () => { }); res.resume(); expect(res.headers.connection).toEqual('keep-alive'); + expect(res.statusCode).toEqual(200); const s1 = res.socket; await once(s1, 'free'); @@ -166,6 +167,7 @@ describe('ProxyAgent', () => { }); res2.resume(); expect(res2.headers.connection).toEqual('keep-alive'); + expect(res2.statusCode).toEqual(200); // FAIL, received 400 const s2 = res2.socket; assert(s1 === s2); @@ -252,6 +254,106 @@ describe('ProxyAgent', () => { ); }); + it('should work with `keepAlive: true`', async () => { + httpsServer.on('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + }); + + process.env.HTTP_PROXY = httpsProxyServerUrl.href; + const agent = new ProxyAgent({ + keepAlive: true, + rejectUnauthorized: false, + }); + + try { + const res = await req(new URL('/test', httpsServerUrl), { + agent, + }); + res.resume(); + expect(res.headers.connection).toEqual('keep-alive'); + expect(res.statusCode).toEqual(200); + const s1 = res.socket; + await once(s1, 'free'); + + const res2 = await req(new URL('/test', httpsServerUrl), { + agent, + }); + res2.resume(); + expect(res2.headers.connection).toEqual('keep-alive'); + expect(res2.statusCode).toEqual(200); + const s2 = res2.socket; + assert(s1 === s2); + + await once(s2, 'free'); + } finally { + agent.destroy(); + } + }); + + it('should cache agent for multiple http requests', async () => { + let gotHttpReq = false; + httpServer.on('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + gotHttpReq = true; + }); + + process.env.ALL_PROXY = httpsProxyServerUrl.href; + const agent = new ProxyAgent({ rejectUnauthorized: false }); + + const res = await req(httpServerUrl, { + agent, + }); + const body = await json(res); + assert(gotHttpReq); + assert.equal(httpServerUrl.host, body.host); + expect(agent.cache.size).toEqual(1); + expect([...agent.cache.values()][0]).toBeInstanceOf(HttpProxyAgent); + + gotHttpReq = false; + const res2 = await req(httpServerUrl, { + agent, + }); + const body2 = await json(res2); + assert(gotHttpReq); + assert.equal(httpServerUrl.host, body2.host); + expect(agent.cache.size).toEqual(1); + expect([...agent.cache.values()][0]).toBeInstanceOf(HttpProxyAgent); + }); + + it('should cache agent for multiple https requests', async () => { + let gotHttpsReq = false; + httpsServer.on('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + gotHttpsReq = true; + }); + + process.env.ALL_PROXY = httpsProxyServerUrl.href; + const agent = new ProxyAgent({ rejectUnauthorized: false }); + + const res = await req(httpsServerUrl, { + agent, + }); + const body = await json(res); + assert(gotHttpsReq); + assert.equal(httpsServerUrl.host, body.host); + expect(agent.cache.size).toEqual(1); + expect([...agent.cache.values()][0]).toBeInstanceOf( + HttpsProxyAgent + ); + + gotHttpsReq = false; + const res2 = await req(httpsServerUrl, { + agent, + }); + const body2 = await json(res2); + assert(gotHttpsReq); + assert.equal(httpsServerUrl.host, body2.host); + expect(agent.cache.size).toEqual(1); + expect([...agent.cache.values()][0]).toBeInstanceOf( + HttpsProxyAgent + ); + }); + it('should call provided function with getProxyForUrl option', async () => { let gotCall = false; let urlParameter = '';