Skip to content

Commit c3d98e6

Browse files
authored
test: add fetch tests with a proxy server (#9706)
1 parent 9b54365 commit c3d98e6

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

packages/browsers/src/httpUtil.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export function httpRequest(
4949
hostname: url.hostname,
5050
port: url.port,
5151
path: url.pathname,
52+
method,
5253
headers: keepAlive ? {Connection: 'keep-alive'} : undefined,
5354
};
5455

@@ -59,6 +60,7 @@ export function httpRequest(
5960
options.path = url.href;
6061
options.hostname = proxy.hostname;
6162
options.protocol = proxy.protocol;
63+
options.port = proxy.port;
6264
} else {
6365
options.agent = createHttpsProxyAgent({
6466
host: proxy.host,

packages/browsers/test/src/fetch.spec.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import assert from 'assert';
1818
import fs from 'fs';
19+
import http from 'http';
20+
import https from 'https';
1921
import os from 'os';
2022
import path from 'path';
2123

@@ -129,4 +131,98 @@ describe('fetch', () => {
129131
assert.ok(fs.existsSync(expectedOutputPath));
130132
}
131133
);
134+
135+
describe('with proxy', () => {
136+
const proxyUrl = new URL(`http://localhost:54321`);
137+
let proxyServer: http.Server;
138+
let proxiedRequestUrls: string[] = [];
139+
140+
beforeEach(() => {
141+
proxiedRequestUrls = [];
142+
proxyServer = http
143+
.createServer(
144+
(
145+
originalRequest: http.IncomingMessage,
146+
originalResponse: http.ServerResponse
147+
) => {
148+
const url = originalRequest.url as string;
149+
const proxyRequest = (
150+
url.startsWith('http:') ? http : https
151+
).request(
152+
url,
153+
{
154+
method: originalRequest.method,
155+
rejectUnauthorized: false,
156+
},
157+
proxyResponse => {
158+
originalResponse.writeHead(
159+
proxyResponse.statusCode as number,
160+
proxyResponse.headers
161+
);
162+
proxyResponse.pipe(originalResponse, {end: true});
163+
}
164+
);
165+
originalRequest.pipe(proxyRequest, {end: true});
166+
proxiedRequestUrls.push(url);
167+
}
168+
)
169+
.listen({
170+
port: proxyUrl.port,
171+
hostname: proxyUrl.hostname,
172+
});
173+
174+
process.env['HTTPS_PROXY'] = proxyUrl.toString();
175+
process.env['HTTP_PROXY'] = proxyUrl.toString();
176+
});
177+
178+
afterEach(async () => {
179+
await new Promise((resolve, reject) => {
180+
proxyServer.close(error => {
181+
if (error) {
182+
reject(error);
183+
} else {
184+
resolve(undefined);
185+
}
186+
});
187+
});
188+
delete process.env['HTTP_PROXY'];
189+
delete process.env['HTTPS_PROXY'];
190+
});
191+
192+
it('can send canFetch requests via a proxy', async () => {
193+
assert.strictEqual(
194+
await canFetch({
195+
cacheDir: tmpDir,
196+
browser: Browser.CHROME,
197+
platform: BrowserPlatform.LINUX,
198+
revision: testChromeRevision,
199+
}),
200+
true
201+
);
202+
assert.deepStrictEqual(proxiedRequestUrls, [
203+
'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1083080/chrome-linux.zip',
204+
]);
205+
});
206+
207+
it('can fetch via a proxy', async function () {
208+
this.timeout(60000);
209+
const expectedOutputPath = path.join(
210+
tmpDir,
211+
'chrome',
212+
`${BrowserPlatform.LINUX}-${testChromeRevision}`
213+
);
214+
assert.strictEqual(fs.existsSync(expectedOutputPath), false);
215+
const browser = await fetch({
216+
cacheDir: tmpDir,
217+
browser: Browser.CHROME,
218+
platform: BrowserPlatform.LINUX,
219+
revision: testChromeRevision,
220+
});
221+
assert.strictEqual(browser.path, expectedOutputPath);
222+
assert.ok(fs.existsSync(expectedOutputPath));
223+
assert.deepStrictEqual(proxiedRequestUrls, [
224+
'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1083080/chrome-linux.zip',
225+
]);
226+
});
227+
});
132228
});

0 commit comments

Comments
 (0)