Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
* All existing keepAlive tests now assert OK status
* Added http-proxy-agent keepAlive test
* Added pac-proxy-agent keepAlive tests
* Added proxy-agent keepAlive test and cache tests
  • Loading branch information
jportner committed May 15, 2023
1 parent 3b50fc1 commit 1467942
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/agent-base/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions packages/http-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
}
});
});
});
4 changes: 4 additions & 0 deletions packages/https-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand Down
80 changes: 80 additions & 0 deletions packages/pac-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
}
});
});
});
102 changes: 102 additions & 0 deletions packages/proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);

Expand Down Expand Up @@ -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 = '';
Expand Down

0 comments on commit 1467942

Please sign in to comment.