Skip to content

Commit

Permalink
test: add fetch tests with a proxy server (#9706)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Feb 20, 2023
1 parent 9b54365 commit c3d98e6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/browsers/src/httpUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function httpRequest(
hostname: url.hostname,
port: url.port,
path: url.pathname,
method,
headers: keepAlive ? {Connection: 'keep-alive'} : undefined,
};

Expand All @@ -59,6 +60,7 @@ export function httpRequest(
options.path = url.href;
options.hostname = proxy.hostname;
options.protocol = proxy.protocol;
options.port = proxy.port;
} else {
options.agent = createHttpsProxyAgent({
host: proxy.host,
Expand Down
96 changes: 96 additions & 0 deletions packages/browsers/test/src/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import assert from 'assert';
import fs from 'fs';
import http from 'http';
import https from 'https';
import os from 'os';
import path from 'path';

Expand Down Expand Up @@ -129,4 +131,98 @@ describe('fetch', () => {
assert.ok(fs.existsSync(expectedOutputPath));
}
);

describe('with proxy', () => {
const proxyUrl = new URL(`http://localhost:54321`);
let proxyServer: http.Server;
let proxiedRequestUrls: string[] = [];

beforeEach(() => {
proxiedRequestUrls = [];
proxyServer = http
.createServer(
(
originalRequest: http.IncomingMessage,
originalResponse: http.ServerResponse
) => {
const url = originalRequest.url as string;
const proxyRequest = (
url.startsWith('http:') ? http : https
).request(
url,
{
method: originalRequest.method,
rejectUnauthorized: false,
},
proxyResponse => {
originalResponse.writeHead(
proxyResponse.statusCode as number,
proxyResponse.headers
);
proxyResponse.pipe(originalResponse, {end: true});
}
);
originalRequest.pipe(proxyRequest, {end: true});
proxiedRequestUrls.push(url);
}
)
.listen({
port: proxyUrl.port,
hostname: proxyUrl.hostname,
});

process.env['HTTPS_PROXY'] = proxyUrl.toString();
process.env['HTTP_PROXY'] = proxyUrl.toString();
});

afterEach(async () => {
await new Promise((resolve, reject) => {
proxyServer.close(error => {
if (error) {
reject(error);
} else {
resolve(undefined);
}
});
});
delete process.env['HTTP_PROXY'];
delete process.env['HTTPS_PROXY'];
});

it('can send canFetch requests via a proxy', async () => {
assert.strictEqual(
await canFetch({
cacheDir: tmpDir,
browser: Browser.CHROME,
platform: BrowserPlatform.LINUX,
revision: testChromeRevision,
}),
true
);
assert.deepStrictEqual(proxiedRequestUrls, [
'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1083080/chrome-linux.zip',
]);
});

it('can fetch via a proxy', async function () {
this.timeout(60000);
const expectedOutputPath = path.join(
tmpDir,
'chrome',
`${BrowserPlatform.LINUX}-${testChromeRevision}`
);
assert.strictEqual(fs.existsSync(expectedOutputPath), false);
const browser = await fetch({
cacheDir: tmpDir,
browser: Browser.CHROME,
platform: BrowserPlatform.LINUX,
revision: testChromeRevision,
});
assert.strictEqual(browser.path, expectedOutputPath);
assert.ok(fs.existsSync(expectedOutputPath));
assert.deepStrictEqual(proxiedRequestUrls, [
'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1083080/chrome-linux.zip',
]);
});
});
});

0 comments on commit c3d98e6

Please sign in to comment.