-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
browsercontext-proxy.spec.ts
377 lines (331 loc) · 14.4 KB
/
browsercontext-proxy.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { browserTest as it, expect } from './config/browserTest';
it.use({
launchOptions: async ({ launchOptions }, use) => {
await use({
...launchOptions,
proxy: { server: 'per-context' }
});
}
});
it.beforeEach(({ server }) => {
server.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>');
});
});
it('should throw for missing global proxy on Chromium Windows', async ({ browserName, platform, browserType, server }) => {
it.skip(browserName !== 'chromium' || platform !== 'win32');
let browser;
try {
browser = await browserType.launch({
proxy: undefined,
});
const error = await browser.newContext({ proxy: { server: `localhost:${server.PORT}` } }).catch(e => e);
expect(error.toString()).toContain('Browser needs to be launched with the global proxy');
} finally {
await browser.close();
}
});
it('should work when passing the proxy only on the context level', async ({ browserName, platform, browserType, server, proxyServer }) => {
// Currently an upstream bug in the network stack of Chromium which leads that
// the wrong proxy gets used in the BrowserContext.
it.fixme(browserName === 'chromium' && platform === 'win32');
proxyServer.forwardTo(server.PORT);
let browser;
try {
browser = await browserType.launch({
proxy: undefined,
});
const context = await browser.newContext({
proxy: { server: `localhost:${proxyServer.PORT}` }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
} finally {
await browser.close();
}
});
it('should throw for bad server value', async ({ contextFactory }) => {
const error = await contextFactory({
// @ts-expect-error server must be a string
proxy: { server: 123 }
}).catch(e => e);
expect(error.message).toContain('proxy.server: expected string, got number');
});
it('should use proxy', async ({ contextFactory, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
proxy: { server: `localhost:${proxyServer.PORT}` }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
await context.close();
});
it.describe('should proxy local network requests', () => {
for (const additionalBypass of [false, true]) {
it.describe(additionalBypass ? 'with other bypasses' : 'by default', () => {
for (const params of [
{
target: 'localhost',
description: 'localhost',
},
{
target: '127.0.0.1',
description: 'loopback address',
},
{
target: '169.254.3.4',
description: 'link-local'
}
]) {
it(`${params.description}`, async ({ platform, browserName, contextFactory, server, proxyServer }) => {
it.fail(browserName === 'webkit' && platform === 'darwin' && additionalBypass && ['localhost', '127.0.0.1'].includes(params.target), 'WK fails to proxy 127.0.0.1 and localhost if additional bypasses are present');
const path = `/target-${additionalBypass}-${params.target}.html`;
server.setRoute(path, async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>');
});
const url = `http://${params.target}:${server.PORT}${path}`;
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
proxy: { server: `localhost:${proxyServer.PORT}`, bypass: additionalBypass ? '1.non.existent.domain.for.the.test' : undefined }
});
const page = await context.newPage();
await page.goto(url);
expect(proxyServer.requestUrls).toContain(url);
expect(await page.title()).toBe('Served by the proxy');
await page.goto('http://1.non.existent.domain.for.the.test/foo.html').catch(() => {});
if (additionalBypass)
expect(proxyServer.requestUrls).not.toContain('http://1.non.existent.domain.for.the.test/foo.html');
else
expect(proxyServer.requestUrls).toContain('http://1.non.existent.domain.for.the.test/foo.html');
await context.close();
});
}
});
}
});
it('should use ipv6 proxy', async ({ contextFactory, server, proxyServer, browserName }) => {
it.fail(browserName === 'firefox', 'page.goto: NS_ERROR_UNKNOWN_HOST');
it.fail(!!process.env.INSIDE_DOCKER, 'docker does not support IPv6 by default');
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
proxy: { server: `[0:0:0:0:0:0:0:1]:${proxyServer.PORT}` }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
await context.close();
});
it('should use proxy twice', async ({ contextFactory, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
proxy: { server: `localhost:${proxyServer.PORT}` }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
await page.goto('http://non-existent-2.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent-2.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
await context.close();
});
it('should use proxy for second page', async ({ contextFactory, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
proxy: { server: `localhost:${proxyServer.PORT}` }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
const page2 = await context.newPage();
proxyServer.requestUrls = [];
await page2.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page2.title()).toBe('Served by the proxy');
await context.close();
});
it('should use proxy for https urls', async ({ contextFactory, server, httpsServer, proxyServer }) => {
httpsServer.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by https server via proxy</title></html>');
});
proxyServer.forwardTo(httpsServer.PORT);
const context = await contextFactory({
ignoreHTTPSErrors: true,
proxy: { server: `localhost:${proxyServer.PORT}` }
});
const page = await context.newPage();
await page.goto('https://non-existent.com/target.html');
expect(proxyServer.connectHosts).toContain('non-existent.com:443');
expect(await page.title()).toBe('Served by https server via proxy');
await context.close();
});
it('should work with IP:PORT notion', async ({ contextFactory, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
proxy: { server: `127.0.0.1:${proxyServer.PORT}` }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
await context.close();
});
it('should throw for socks5 authentication', async ({ contextFactory }) => {
const error = await contextFactory({
proxy: { server: `socks5://localhost:1234`, username: 'user', password: 'secret' }
}).catch(e => e);
expect(error.message).toContain('Browser does not support socks5 proxy authentication');
});
it('should throw for socks4 authentication', async ({ contextFactory }) => {
const error = await contextFactory({
proxy: { server: `socks4://localhost:1234`, username: 'user', password: 'secret' }
}).catch(e => e);
expect(error.message).toContain('Socks4 proxy protocol does not support authentication');
});
it('should authenticate', async ({ contextFactory, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
let auth;
proxyServer.setAuthHandler(req => {
auth = req.headers['proxy-authorization'];
return !!auth;
});
const context = await contextFactory({
proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: 'secret' }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(auth).toBe('Basic ' + Buffer.from('user:secret').toString('base64'));
expect(await page.title()).toBe('Served by the proxy');
await context.close();
});
it('should authenticate with empty password', async ({ contextFactory, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
let auth;
proxyServer.setAuthHandler(req => {
auth = req.headers['proxy-authorization'];
return !!auth;
});
const context = await contextFactory({
proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: '' }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(auth).toBe('Basic ' + Buffer.from('user:').toString('base64'));
expect(await page.title()).toBe('Served by the proxy');
await context.close();
});
it('should isolate proxy credentials between contexts', async ({ contextFactory, server, browserName, proxyServer }) => {
it.fixme(browserName === 'firefox', 'Credentials from the first context stick around');
proxyServer.forwardTo(server.PORT);
let auth;
proxyServer.setAuthHandler(req => {
auth = req.headers['proxy-authorization'];
return !!auth;
});
{
const context = await contextFactory({
proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user1', password: 'secret1' }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(auth).toBe('Basic ' + Buffer.from('user1:secret1').toString('base64'));
expect(await page.title()).toBe('Served by the proxy');
await context.close();
}
auth = undefined;
{
const context = await contextFactory({
proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user2', password: 'secret2' }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
expect(auth).toBe('Basic ' + Buffer.from('user2:secret2').toString('base64'));
await context.close();
}
});
it('should exclude patterns', async ({ contextFactory, server, browserName, headless, proxyServer }) => {
it.fixme(browserName === 'chromium' && !headless, 'Chromium headed crashes with CHECK(!in_frame_tree_) in RenderFrameImpl::OnDeleteFrame.');
proxyServer.forwardTo(server.PORT);
// FYI: using long and weird domain names to avoid ATT DNS hijacking
// that resolves everything to some weird search results page.
//
// @see https://gist.github.com/CollinChaffin/24f6c9652efb3d6d5ef2f5502720ef00
const context = await contextFactory({
proxy: { server: `localhost:${proxyServer.PORT}`, bypass: '1.non.existent.domain.for.the.test, 2.non.existent.domain.for.the.test, .another.test' }
});
const page = await context.newPage();
await page.goto('http://0.non.existent.domain.for.the.test/target.html');
expect(proxyServer.requestUrls).toContain('http://0.non.existent.domain.for.the.test/target.html');
expect(await page.title()).toBe('Served by the proxy');
proxyServer.requestUrls = [];
{
const error = await page.goto('http://1.non.existent.domain.for.the.test/target.html').catch(e => e);
expect(proxyServer.requestUrls).toEqual([]);
expect(error.message).toBeTruthy();
}
{
const error = await page.goto('http://2.non.existent.domain.for.the.test/target.html').catch(e => e);
expect(proxyServer.requestUrls).toEqual([]);
expect(error.message).toBeTruthy();
}
{
const error = await page.goto('http://foo.is.the.another.test/target.html').catch(e => e);
expect(proxyServer.requestUrls).toEqual([]);
expect(error.message).toBeTruthy();
}
{
await page.goto('http://3.non.existent.domain.for.the.test/target.html');
expect(proxyServer.requestUrls).toContain('http://3.non.existent.domain.for.the.test/target.html');
expect(await page.title()).toBe('Served by the proxy');
}
await context.close();
});
it('should use socks proxy', async ({ contextFactory, socksPort }) => {
const context = await contextFactory({
proxy: { server: `socks5://localhost:${socksPort}` }
});
const page = await context.newPage();
await page.goto('http://non-existent.com');
expect(await page.title()).toBe('Served by the SOCKS proxy');
await context.close();
});
it('should use socks proxy in second page', async ({ contextFactory, socksPort }) => {
const context = await contextFactory({
proxy: { server: `socks5://localhost:${socksPort}` }
});
const page = await context.newPage();
await page.goto('http://non-existent.com');
expect(await page.title()).toBe('Served by the SOCKS proxy');
const page2 = await context.newPage();
await page2.goto('http://non-existent.com');
expect(await page2.title()).toBe('Served by the SOCKS proxy');
await context.close();
});
it('does launch without a port', async ({ contextFactory }) => {
const context = await contextFactory({
proxy: { server: 'http://localhost' }
});
await context.close();
});