2015-04-04 09:53:59 -07:00
|
|
|
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
|
// distributed with this work for additional information
|
|
|
|
|
// regarding copyright ownership. The SFC licenses this file
|
|
|
|
|
// to you 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
|
2014-11-12 19:33:37 +00:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-12 19:33:37 +00:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// 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.
|
2013-06-26 22:16:09 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
'use strict'
|
2013-06-26 22:16:09 -07:00
|
|
|
|
2024-04-16 01:50:58 +05:30
|
|
|
const assert = require('node:assert')
|
|
|
|
|
const { URL } = require('node:url')
|
2024-06-27 18:33:36 +01:00
|
|
|
const proxy = require('selenium-webdriver/proxy')
|
2020-08-03 17:56:31 +03:00
|
|
|
const test = require('../lib/test')
|
2024-06-27 18:33:36 +01:00
|
|
|
const { Browser } = require('selenium-webdriver')
|
2020-08-03 17:56:31 +03:00
|
|
|
const { Server } = require('../lib/test/httpserver')
|
2017-10-24 17:58:37 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
test.suite(function (env) {
|
2013-06-26 22:16:09 -07:00
|
|
|
function writeResponse(res, body, encoding, contentType) {
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
|
'Content-Length': Buffer.byteLength(body, encoding),
|
2020-08-03 17:56:31 +03:00
|
|
|
'Content-Type': contentType,
|
|
|
|
|
})
|
|
|
|
|
res.end(body)
|
2013-06-26 22:16:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function writePacFile(res) {
|
2020-08-03 17:56:31 +03:00
|
|
|
writeResponse(
|
|
|
|
|
res,
|
|
|
|
|
[
|
|
|
|
|
'function FindProxyForURL(url, host) {',
|
|
|
|
|
' if (shExpMatch(url, "' + goodbyeServer.url('*') + '")) {',
|
|
|
|
|
' return "DIRECT";',
|
|
|
|
|
' }',
|
|
|
|
|
' return "PROXY ' + proxyServer.host() + '";',
|
|
|
|
|
'}',
|
|
|
|
|
].join('\n'),
|
|
|
|
|
'ascii',
|
2024-02-07 16:07:24 +00:00
|
|
|
'application/x-javascript-config',
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
2013-06-26 22:16:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
const proxyServer = new Server(function (req, res) {
|
|
|
|
|
const pathname = new URL(req.url).pathname
|
2013-06-26 22:16:09 -07:00
|
|
|
if (pathname === '/proxy.pac') {
|
2020-08-03 17:56:31 +03:00
|
|
|
return writePacFile(res)
|
2013-06-26 22:16:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
writeResponse(
|
|
|
|
|
res,
|
2024-02-07 16:07:24 +00:00
|
|
|
['<!DOCTYPE html>', '<title>Proxy page</title>', '<h3>This is the proxy landing page</h3>'].join(''),
|
2020-08-03 17:56:31 +03:00
|
|
|
'utf8',
|
2024-02-07 16:07:24 +00:00
|
|
|
'text/html; charset=UTF-8',
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const helloServer = new Server(function (_req, res) {
|
|
|
|
|
writeResponse(
|
|
|
|
|
res,
|
2024-02-07 16:07:24 +00:00
|
|
|
['<!DOCTYPE html>', '<title>Hello</title>', '<h3>Hello, world!</h3>'].join(''),
|
2020-08-03 17:56:31 +03:00
|
|
|
'utf8',
|
2024-02-07 16:07:24 +00:00
|
|
|
'text/html; charset=UTF-8',
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const goodbyeServer = new Server(function (_req, res) {
|
|
|
|
|
writeResponse(
|
|
|
|
|
res,
|
2024-02-07 16:07:24 +00:00
|
|
|
['<!DOCTYPE html>', '<title>Goodbye</title>', '<h3>Goodbye, world!</h3>'].join(''),
|
2020-08-03 17:56:31 +03:00
|
|
|
'utf8',
|
2024-02-07 16:07:24 +00:00
|
|
|
'text/html; charset=UTF-8',
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
|
|
|
|
})
|
2013-06-26 22:16:09 -07:00
|
|
|
|
2015-02-18 13:09:24 -08:00
|
|
|
// Cannot pass start directly to mocha's before, as mocha will interpret the optional
|
|
|
|
|
// port parameter as an async callback parameter.
|
|
|
|
|
function mkStartFunc(server) {
|
2020-08-03 17:56:31 +03:00
|
|
|
return function () {
|
|
|
|
|
return server.start()
|
|
|
|
|
}
|
2015-02-18 13:09:24 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
before(mkStartFunc(proxyServer))
|
|
|
|
|
before(mkStartFunc(helloServer))
|
|
|
|
|
before(mkStartFunc(goodbyeServer))
|
2013-06-26 22:16:09 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
after(proxyServer.stop.bind(proxyServer))
|
|
|
|
|
after(helloServer.stop.bind(helloServer))
|
|
|
|
|
after(goodbyeServer.stop.bind(goodbyeServer))
|
2013-06-26 22:16:09 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let driver
|
|
|
|
|
beforeEach(function () {
|
|
|
|
|
driver = null
|
|
|
|
|
})
|
|
|
|
|
afterEach(function () {
|
|
|
|
|
return driver && driver.quit()
|
|
|
|
|
})
|
2013-06-26 22:16:09 -07:00
|
|
|
|
2016-06-30 11:04:08 -07:00
|
|
|
function createDriver(proxy) {
|
2020-08-03 17:56:31 +03:00
|
|
|
return (driver = env.builder().setProxy(proxy).build())
|
2016-06-30 11:04:08 -07:00
|
|
|
}
|
|
|
|
|
|
2015-02-21 09:59:24 -08:00
|
|
|
// Proxy support not implemented.
|
2020-08-03 17:56:31 +03:00
|
|
|
test
|
2024-02-07 16:07:24 +00:00
|
|
|
.ignore(env.browsers(Browser.CHROME, Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.FIREFOX))
|
2020-08-03 17:56:31 +03:00
|
|
|
.describe('manual proxy settings', function () {
|
|
|
|
|
it('can configure HTTP proxy host', async function () {
|
|
|
|
|
await createDriver(
|
|
|
|
|
proxy.manual({
|
|
|
|
|
http: proxyServer.host(),
|
|
|
|
|
bypass: [],
|
2024-02-07 16:07:24 +00:00
|
|
|
}),
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await driver.get(helloServer.url())
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await driver.getTitle(), 'Proxy page')
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can bypass proxy for specific hosts', async function () {
|
|
|
|
|
await createDriver(
|
|
|
|
|
proxy.manual({
|
|
|
|
|
http: proxyServer.host(),
|
|
|
|
|
bypass: [helloServer.host()],
|
2024-02-07 16:07:24 +00:00
|
|
|
}),
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await driver.get(helloServer.url())
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await driver.getTitle(), 'Hello')
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'Hello, world!')
|
2020-08-03 17:56:31 +03:00
|
|
|
|
|
|
|
|
// For firefox the no proxy settings appear to match on hostname only.
|
|
|
|
|
let url = goodbyeServer.url().replace(/127\.0\.0\.1/, 'localhost')
|
|
|
|
|
await driver.get(url)
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await driver.getTitle(), 'Proxy page')
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// TODO: test ftp and https proxies.
|
|
|
|
|
})
|
2013-06-26 22:16:09 -07:00
|
|
|
|
2013-07-04 19:46:17 -07:00
|
|
|
// PhantomJS does not support PAC file proxy configuration.
|
2013-11-29 21:16:00 -08:00
|
|
|
// Safari does not support proxies.
|
2020-08-03 17:56:31 +03:00
|
|
|
test
|
2024-02-07 16:07:24 +00:00
|
|
|
.ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.CHROME, Browser.FIREFOX))
|
2020-08-03 17:56:31 +03:00
|
|
|
.describe('pac proxy settings', function () {
|
|
|
|
|
it('can configure proxy through PAC file', async function () {
|
|
|
|
|
await createDriver(proxy.pac(proxyServer.url('/proxy.pac')))
|
|
|
|
|
|
|
|
|
|
await driver.get(helloServer.url())
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await driver.getTitle(), 'Proxy page')
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')
|
2020-08-03 17:56:31 +03:00
|
|
|
|
|
|
|
|
await driver.get(goodbyeServer.url())
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await driver.getTitle(), 'Goodbye')
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'Goodbye, world!')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|