Skip to content

Commit bf1d68f

Browse files
shresthaoshanlewis-wow
authored andcommitted
test & example added
1 parent ff66a0a commit bf1d68f

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

examples/request_finished.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { Server } = require('proxy-chain');
2+
const http = require('http');
3+
const request = require('request');
4+
5+
(async () => {
6+
// Create a target server
7+
const targetServer = http.createServer((req, res) => {
8+
res.writeHead(200, { 'Content-Type': 'text/plain' });
9+
res.end('Hello World!');
10+
});
11+
await new Promise((resolve) => targetServer.listen(0, resolve));
12+
const targetPort = targetServer.address().port;
13+
14+
// Create a proxy server
15+
const server = new Server({
16+
port: 0,
17+
verbose: true,
18+
});
19+
20+
server.on('requestFinished', ({ id, connectionId, request }) => {
21+
console.log(`Request finished: { id: ${id}, connectionId: ${connectionId}, method: ${request.method}, url: ${request.url} }`);
22+
});
23+
24+
await server.listen();
25+
const proxyPort = server.port;
26+
27+
console.log(`Proxy server listening on port ${proxyPort}`);
28+
console.log(`Target server listening on port ${targetPort}`);
29+
30+
// Make a request through the proxy
31+
await new Promise((resolve, reject) => {
32+
request({
33+
url: `http://127.0.0.1:${targetPort}`,
34+
proxy: `http://127.0.0.1:${proxyPort}`,
35+
}, (error, response, body) => {
36+
if (error) return reject(error);
37+
console.log(`Response body: ${body}`);
38+
resolve();
39+
});
40+
});
41+
42+
// Close servers
43+
await server.close(true);
44+
await new Promise((resolve) => targetServer.close(resolve));
45+
console.log('Servers closed.');
46+
})();

test/server.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ const createTestSuite = ({
180180
const mainProxyServerConnectionIds = [];
181181
const mainProxyServerConnectionsClosed = [];
182182
const mainProxyServerConnectionId2Stats = {};
183+
const mainProxyServerRequestsFinished = [];
183184

184185
let upstreamProxyHostname = '127.0.0.1';
185186

@@ -462,6 +463,10 @@ const createTestSuite = ({
462463
mainProxyServerConnectionId2Stats[connectionId] = stats;
463464
});
464465

466+
mainProxyServer.on('requestFinished', ({ id, connectionId }) => {
467+
mainProxyServerRequestsFinished.push({ id, connectionId });
468+
});
469+
465470
return mainProxyServer.listen();
466471
}
467472
})
@@ -894,6 +899,19 @@ const createTestSuite = ({
894899
});
895900
}
896901

902+
if (useMainProxy) {
903+
_it('should emit requestFinished event', () => {
904+
const opts = getRequestOpts('/hello-world');
905+
opts.method = 'GET';
906+
return requestPromised(opts)
907+
.then((response) => {
908+
expect(response.body).to.eql('Hello world!');
909+
expect(response.statusCode).to.eql(200);
910+
expect(mainProxyServerRequestsFinished.length).to.be.above(0);
911+
});
912+
});
913+
}
914+
897915
if (!useSsl && mainProxyAuth && mainProxyAuth.username && mainProxyAuth.password) {
898916
it('handles GET request using puppeteer with invalid credentials', async () => {
899917
const phantomUrl = `${useSsl ? 'https' : 'http'}://${LOCALHOST_TEST}:${targetServerPort}/hello-world`;
@@ -1259,6 +1277,7 @@ const createTestSuite = ({
12591277
expect(mainProxyServer.getConnectionIds()).to.be.deep.eql([]);
12601278
}
12611279
expect(mainProxyServerConnectionIds).to.be.deep.eql([]);
1280+
mainProxyServerRequestsFinished.splice(0, mainProxyServerRequestsFinished.length);
12621281

12631282
const closedSomeConnectionsTwice = mainProxyServerConnectionsClosed
12641283
.reduce((duplicateConnections, id, index) => {

0 commit comments

Comments
 (0)