Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 3, 2023
1 parent 73ced72 commit f2f6393
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 220 deletions.
4 changes: 2 additions & 2 deletions packages/agent-base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export abstract class Agent extends http.Agent {
.then(() => this.connect(req, o))
.then((socket) => {
if (socket instanceof http.Agent) {
// @ts-expect-error `createSocket()` isn't defined in `@types/node`
return socket.createSocket(req, o, cb);
// @ts-expect-error `addRequest()` isn't defined in `@types/node`
return socket.addRequest(req, o);
}
this._currentSocket = socket;
// @ts-expect-error `createSocket()` isn't defined in `@types/node`
Expand Down
2 changes: 1 addition & 1 deletion packages/https-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
return tls.connect({
...omit(opts, 'host', 'path', 'port'),
socket,
servername,
servername: net.isIP(servername) ? undefined : servername,
});
}

Expand Down
58 changes: 58 additions & 0 deletions packages/https-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ describe('HttpsProxyAgent', () => {
sslProxyUrl = (await listen(sslProxy)) as URL;
});

beforeEach(() => {
server.removeAllListeners('request');
sslServer.removeAllListeners('request');
});

// shut down the test HTTP servers
afterAll(() => {
server.close();
Expand Down Expand Up @@ -189,6 +194,31 @@ describe('HttpsProxyAgent', () => {
assert.equal('bar', req.headers.foo);
socket.destroy();
});

it('should work with `keepAlive: true`', async () => {
server.on('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});

const agent = new HttpsProxyAgent(proxyUrl, { keepAlive: true });

try {
const res = await req(serverUrl, { agent });
expect(res.headers.connection).toEqual('keep-alive');
res.resume();
const s1 = res.socket;
await once(s1, 'free');

const res2 = await req(serverUrl, { agent });
expect(res2.headers.connection).toEqual('keep-alive');
res2.resume();
const s2 = res2.socket;
assert(s1 === s2);
await once(s2, 'free');
} finally {
agent.destroy();
}
});
});

describe('"https" module', () => {
Expand Down Expand Up @@ -275,5 +305,33 @@ describe('HttpsProxyAgent', () => {
assert.equal(sslServerUrl.hostname, body.host);
}
);

it('should work with `keepAlive: true`', async () => {
sslServer.on('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});

const agent = new HttpsProxyAgent(sslProxyUrl, {
keepAlive: true,
rejectUnauthorized: false,
});

try {
const res = await req(sslServerUrl, { agent, rejectUnauthorized: false });
expect(res.headers.connection).toEqual('keep-alive');
res.resume();
const s1 = res.socket;
await once(s1, 'free');

const res2 = await req(sslServerUrl, { agent, rejectUnauthorized: false });
expect(res2.headers.connection).toEqual('keep-alive');
res2.resume();
const s2 = res2.socket;
assert(s1 === s2);
await once(s2, 'free');
} finally {
agent.destroy();
}
});
});
});
9 changes: 8 additions & 1 deletion packages/proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ProxyAgent extends Agent {

constructor(opts?: ProxyAgentOptions) {
super(opts);
debug('Creating new ProxyAgent instance');
debug('Creating new ProxyAgent instance: %o', opts);
this.connectOpts = opts;
}

Expand Down Expand Up @@ -104,4 +104,11 @@ export class ProxyAgent extends Agent {

return agent;
}

destroy(): void {
for (const agent of this.cache.values()) {
agent.destroy();
}
super.destroy();
}
}
37 changes: 37 additions & 0 deletions packages/proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ProxyServer, createProxy } from 'proxy';
import socks from 'socksv5';
import { listen } from 'async-listen';
import { ProxyAgent } from '../src';
import { once } from 'events';

const sslOptions = {
key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
Expand Down Expand Up @@ -77,6 +78,8 @@ describe('ProxyAgent', () => {
delete process.env.HTTP_PROXY;
delete process.env.HTTPS_PROXY;
delete process.env.NO_PROXY;
httpServer.removeAllListeners('request');
httpsServer.removeAllListeners('request');
});

describe('"http" module', () => {
Expand Down Expand Up @@ -135,6 +138,40 @@ describe('ProxyAgent', () => {
const body = await json(res);
assert.equal(httpServerUrl.host, body.host);
});

it('should work with `keepAlive: true`', async () => {
httpServer.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', httpServerUrl), {
agent,
});
res.resume();
expect(res.headers.connection).toEqual('keep-alive');
const s1 = res.socket;
await once(s1, 'free');

const res2 = await req(new URL('/test', httpServerUrl), {
agent,
});
res2.resume();
expect(res2.headers.connection).toEqual('keep-alive');
const s2 = res2.socket;
assert(s1 === s2);

await once(s2, 'free');
} finally {
agent.destroy();
}
});
});

describe('"https" module', () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"scripts": {
"build": "tsc",
"test": "mocha --reporter spec",
"test": "jest --env node --verbose --bail",
"lint": "eslint . --ext .ts",
"pack": "node ../../scripts/pack.mjs",
"prepublishOnly": "npm run build"
Expand Down Expand Up @@ -42,9 +42,11 @@
"devDependencies": {
"@types/args": "^5.0.0",
"@types/debug": "^4.1.7",
"@types/mocha": "^5.2.7",
"@types/jest": "^29.5.1",
"@types/node": "^14.18.43",
"mocha": "6",
"async-listen": "^2.1.0",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
"tsconfig": "workspace:*",
"typescript": "^5.0.4"
},
Expand Down
196 changes: 0 additions & 196 deletions packages/proxy/test/test.js

This file was deleted.

Loading

0 comments on commit f2f6393

Please sign in to comment.