Skip to content

Commit 52ae82c

Browse files
committed
test & example added
1 parent 942d22b commit 52ae82c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

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
@@ -154,6 +154,7 @@ const createTestSuite = ({
154154
const mainProxyServerConnectionIds = [];
155155
const mainProxyServerConnectionsClosed = [];
156156
const mainProxyServerConnectionId2Stats = {};
157+
const mainProxyServerRequestsFinished = [];
157158

158159
let upstreamProxyHostname = '127.0.0.1';
159160

@@ -419,6 +420,10 @@ const createTestSuite = ({
419420
mainProxyServerConnectionId2Stats[connectionId] = stats;
420421
});
421422

423+
mainProxyServer.on('requestFinished', ({ id, connectionId }) => {
424+
mainProxyServerRequestsFinished.push({ id, connectionId });
425+
});
426+
422427
return mainProxyServer.listen();
423428
}
424429
})
@@ -832,6 +837,19 @@ const createTestSuite = ({
832837
});
833838
}
834839

840+
if (useMainProxy) {
841+
_it('should emit requestFinished event', () => {
842+
const opts = getRequestOpts('/hello-world');
843+
opts.method = 'GET';
844+
return requestPromised(opts)
845+
.then((response) => {
846+
expect(response.body).to.eql('Hello world!');
847+
expect(response.statusCode).to.eql(200);
848+
expect(mainProxyServerRequestsFinished.length).to.be.above(0);
849+
});
850+
});
851+
}
852+
835853
if (!useSsl && mainProxyAuth && mainProxyAuth.username && mainProxyAuth.password) {
836854
it('handles GET request using puppeteer with invalid credentials', async () => {
837855
const phantomUrl = `${useSsl ? 'https' : 'http'}://${LOCALHOST_TEST}:${targetServerPort}/hello-world`;
@@ -1178,6 +1196,7 @@ const createTestSuite = ({
11781196
expect(mainProxyServer.getConnectionIds()).to.be.deep.eql([]);
11791197
}
11801198
expect(mainProxyServerConnectionIds).to.be.deep.eql([]);
1199+
mainProxyServerRequestsFinished.splice(0, mainProxyServerRequestsFinished.length);
11811200

11821201
const closedSomeConnectionsTwice = mainProxyServerConnectionsClosed
11831202
.reduce((duplicateConnections, id, index) => {

0 commit comments

Comments
 (0)