|
16 | 16 |
|
17 | 17 | import assert from 'assert';
|
18 | 18 | import fs from 'fs';
|
| 19 | +import http from 'http'; |
| 20 | +import https from 'https'; |
19 | 21 | import os from 'os';
|
20 | 22 | import path from 'path';
|
21 | 23 |
|
@@ -129,4 +131,98 @@ describe('fetch', () => {
|
129 | 131 | assert.ok(fs.existsSync(expectedOutputPath));
|
130 | 132 | }
|
131 | 133 | );
|
| 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 | + }); |
132 | 228 | });
|
0 commit comments