forked from microsoft/playwright
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproxy.spec.ts
254 lines (219 loc) · 8.81 KB
/
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
/**
* 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 { playwrightTest as it, expect } from './config/browserTest';
import socks from 'socksv5';
import net from 'net';
it.skip(({ mode }) => mode === 'service');
it('should throw for bad server value', async ({ browserType }) => {
const error = await browserType.launch({
// @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 ({ browserType, server }) => {
server.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>');
});
const browser = await browserType.launch({
proxy: { server: `localhost:${server.PORT}` }
});
const page = await browser.newPage();
await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
await browser.close();
});
it('should use proxy for second page', async ({ browserType, server }) => {
server.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>');
});
const browser = await browserType.launch({
proxy: { server: `localhost:${server.PORT}` }
});
const page = await browser.newPage();
await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
const page2 = await browser.newPage();
await page2.goto('http://non-existent.com/target.html');
expect(await page2.title()).toBe('Served by the proxy');
await browser.close();
});
it('should work with IP:PORT notion', async ({ browserType, server }) => {
server.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>');
});
const browser = await browserType.launch({
proxy: { server: `127.0.0.1:${server.PORT}` }
});
const page = await browser.newPage();
await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
await browser.close();
});
it('should authenticate', async ({ browserType, server }) => {
server.setRoute('/target.html', async (req, res) => {
const auth = req.headers['proxy-authorization'];
if (!auth) {
res.writeHead(407, 'Proxy Authentication Required', {
'Proxy-Authenticate': 'Basic realm="Access to internal site"'
});
res.end();
} else {
res.end(`<html><title>${auth}</title></html>`);
}
});
const browser = await browserType.launch({
proxy: { server: `localhost:${server.PORT}`, username: 'user', password: 'secret' }
});
const page = await browser.newPage();
await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Basic ' + Buffer.from('user:secret').toString('base64'));
await browser.close();
});
it('should exclude patterns', async ({ browserType, server, browserName, headless }) => {
it.fixme(browserName === 'chromium' && !headless, 'Chromium headed crashes with CHECK(!in_frame_tree_) in RenderFrameImpl::OnDeleteFrame.');
server.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>');
});
// 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 browser = await browserType.launch({
proxy: { server: `localhost:${server.PORT}`, bypass: '1.non.existent.domain.for.the.test, 2.non.existent.domain.for.the.test, .another.test' }
});
const page = await browser.newPage();
await page.goto('http://0.non.existent.domain.for.the.test/target.html');
expect(await page.title()).toBe('Served by the proxy');
{
const error = await page.goto('http://1.non.existent.domain.for.the.test/target.html').catch(e => e);
expect(error.message).toBeTruthy();
}
{
const error = await page.goto('http://2.non.existent.domain.for.the.test/target.html').catch(e => e);
expect(error.message).toBeTruthy();
}
{
const error = await page.goto('http://foo.is.the.another.test/target.html').catch(e => e);
expect(error.message).toBeTruthy();
}
{
await page.goto('http://3.non.existent.domain.for.the.test/target.html');
expect(await page.title()).toBe('Served by the proxy');
}
await browser.close();
});
it('should use socks proxy', async ({ browserType, socksPort }) => {
const browser = await browserType.launch({
proxy: { server: `socks5://localhost:${socksPort}` }
});
const page = await browser.newPage();
await page.goto('http://non-existent.com');
expect(await page.title()).toBe('Served by the SOCKS proxy');
await browser.close();
});
it('should use socks proxy in second page', async ({ browserType, socksPort }) => {
const browser = await browserType.launch({
proxy: { server: `socks5://localhost:${socksPort}` }
});
const page = await browser.newPage();
await page.goto('http://non-existent.com');
expect(await page.title()).toBe('Served by the SOCKS proxy');
const page2 = await browser.newPage();
await page2.goto('http://non-existent.com');
expect(await page2.title()).toBe('Served by the SOCKS proxy');
await browser.close();
});
it('does launch without a port', async ({ browserType }) => {
const browser = await browserType.launch({
proxy: { server: 'http://localhost' }
});
await browser.close();
});
it('should use proxy with emulated user agent', async ({ browserType }) => {
it.fixme(true, 'Non-emulated user agent is used in proxy CONNECT');
let requestText = '';
// This is our proxy server
const server = net.createServer(socket => {
socket.on('data', data => {
requestText = data.toString();
socket.end();
});
});
await new Promise<void>(f => server.listen(0, f));
const browser = await browserType.launch({
proxy: { server: `http://127.0.0.1:${(server.address() as any).port}` }
});
const page = await browser.newPage({
userAgent: 'MyUserAgent'
});
// HTTPS over HTTP proxy will start with CONNECT request.
await page.goto('https://bing.com/').catch(() => {});
await browser.close();
server.close();
// This connect request should have emulated user agent.
expect(requestText).toContain('MyUserAgent');
});
async function setupSocksForwardingServer(port: number, forwardPort: number){
const socksServer = socks.createServer((info, accept, deny) => {
if (!['127.0.0.1', 'fake-localhost-127-0-0-1.nip.io'].includes(info.dstAddr) || info.dstPort !== 1337) {
deny();
return;
}
const socket = accept(true);
if (socket) {
const dstSock = new net.Socket();
socket.pipe(dstSock).pipe(socket);
socket.on('close', () => dstSock.end());
socket.on('end', () => dstSock.end());
dstSock.setKeepAlive(false);
dstSock.connect(forwardPort, '127.0.0.1');
}
});
await new Promise<void>(resolve => socksServer.listen(port, 'localhost', resolve));
socksServer.useAuth(socks.auth.None());
return {
closeProxyServer: () => socksServer.close(),
proxyServerAddr: `socks5://localhost:${port}`,
};
}
it('should use SOCKS proxy for websocket requests', async ({ browserName, platform, browserType, server }, testInfo) => {
it.fixme(browserName === 'webkit' && platform !== 'linux');
const { proxyServerAddr, closeProxyServer } = await setupSocksForwardingServer(testInfo.workerIndex + 2048 + 2, server.PORT);
const browser = await browserType.launch({
proxy: {
server: proxyServerAddr,
}
});
server.sendOnWebSocketConnection('incoming');
server.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>');
});
const page = await browser.newPage();
// Hosts get resolved by the client
await page.goto('http://fake-localhost-127-0-0-1.nip.io:1337/target.html');
expect(await page.title()).toBe('Served by the proxy');
const value = await page.evaluate(() => {
let cb;
const result = new Promise(f => cb = f);
const ws = new WebSocket('ws://fake-localhost-127-0-0-1.nip.io:1337/ws');
ws.addEventListener('message', data => { ws.close(); cb(data.data); });
return result;
});
expect(value).toBe('incoming');
await browser.close();
closeProxyServer();
});