Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/proxy_test.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const assert = require('node:assert')
21
const { URL } = require('node:url')
22
const proxy = require('selenium-webdriver/proxy')
23
const test = require('../lib/test')
24
const { Browser } = require('selenium-webdriver')
25
const { Server } = require('../lib/test/httpserver')
26
27
test.suite(function (env) {
28
function writeResponse(res, body, encoding, contentType) {
29
res.writeHead(200, {
30
'Content-Length': Buffer.byteLength(body, encoding),
31
'Content-Type': contentType,
32
})
33
res.end(body)
34
}
35
36
function writePacFile(res) {
37
writeResponse(
38
res,
39
[
40
'function FindProxyForURL(url, host) {',
41
' if (shExpMatch(url, "' + goodbyeServer.url('*') + '")) {',
42
' return "DIRECT";',
43
' }',
44
' return "PROXY ' + proxyServer.host() + '";',
45
'}',
46
].join('\n'),
47
'ascii',
48
'application/x-javascript-config',
49
)
50
}
51
52
const proxyServer = new Server(function (req, res) {
53
const pathname = new URL(req.url).pathname
54
if (pathname === '/proxy.pac') {
55
return writePacFile(res)
56
}
57
58
writeResponse(
59
res,
60
['<!DOCTYPE html>', '<title>Proxy page</title>', '<h3>This is the proxy landing page</h3>'].join(''),
61
'utf8',
62
'text/html; charset=UTF-8',
63
)
64
})
65
66
const helloServer = new Server(function (_req, res) {
67
writeResponse(
68
res,
69
['<!DOCTYPE html>', '<title>Hello</title>', '<h3>Hello, world!</h3>'].join(''),
70
'utf8',
71
'text/html; charset=UTF-8',
72
)
73
})
74
75
const goodbyeServer = new Server(function (_req, res) {
76
writeResponse(
77
res,
78
['<!DOCTYPE html>', '<title>Goodbye</title>', '<h3>Goodbye, world!</h3>'].join(''),
79
'utf8',
80
'text/html; charset=UTF-8',
81
)
82
})
83
84
// Cannot pass start directly to mocha's before, as mocha will interpret the optional
85
// port parameter as an async callback parameter.
86
function mkStartFunc(server) {
87
return function () {
88
return server.start()
89
}
90
}
91
92
before(mkStartFunc(proxyServer))
93
before(mkStartFunc(helloServer))
94
before(mkStartFunc(goodbyeServer))
95
96
after(proxyServer.stop.bind(proxyServer))
97
after(helloServer.stop.bind(helloServer))
98
after(goodbyeServer.stop.bind(goodbyeServer))
99
100
let driver
101
beforeEach(function () {
102
driver = null
103
})
104
afterEach(function () {
105
return driver && driver.quit()
106
})
107
108
function createDriver(proxy) {
109
return (driver = env.builder().setProxy(proxy).build())
110
}
111
112
// Proxy support not implemented.
113
test
114
.ignore(env.browsers(Browser.CHROME, Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.FIREFOX))
115
.describe('manual proxy settings', function () {
116
it('can configure HTTP proxy host', async function () {
117
await createDriver(
118
proxy.manual({
119
http: proxyServer.host(),
120
bypass: [],
121
}),
122
)
123
124
await driver.get(helloServer.url())
125
assert.strictEqual(await driver.getTitle(), 'Proxy page')
126
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')
127
})
128
129
it('can bypass proxy for specific hosts', async function () {
130
await createDriver(
131
proxy.manual({
132
http: proxyServer.host(),
133
bypass: [helloServer.host()],
134
}),
135
)
136
137
await driver.get(helloServer.url())
138
assert.strictEqual(await driver.getTitle(), 'Hello')
139
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'Hello, world!')
140
141
// For firefox the no proxy settings appear to match on hostname only.
142
let url = goodbyeServer.url().replace(/127\.0\.0\.1/, 'localhost')
143
await driver.get(url)
144
assert.strictEqual(await driver.getTitle(), 'Proxy page')
145
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')
146
})
147
148
// TODO: test ftp and https proxies.
149
})
150
151
// PhantomJS does not support PAC file proxy configuration.
152
// Safari does not support proxies.
153
test
154
.ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.CHROME, Browser.FIREFOX))
155
.describe('pac proxy settings', function () {
156
it('can configure proxy through PAC file', async function () {
157
await createDriver(proxy.pac(proxyServer.url('/proxy.pac')))
158
159
await driver.get(helloServer.url())
160
assert.strictEqual(await driver.getTitle(), 'Proxy page')
161
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')
162
163
await driver.get(goodbyeServer.url())
164
assert.strictEqual(await driver.getTitle(), 'Goodbye')
165
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'Goodbye, world!')
166
})
167
})
168
})
169
170