Path: blob/trunk/javascript/selenium-webdriver/test/proxy_test.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const { URL } = require('node:url')21const proxy = require('selenium-webdriver/proxy')22const test = require('../lib/test')23const { Browser } = require('selenium-webdriver')24const { Server } = require('../lib/test/httpserver')2526test.suite(function (env) {27function writeResponse(res, body, encoding, contentType) {28res.writeHead(200, {29'Content-Length': Buffer.byteLength(body, encoding),30'Content-Type': contentType,31})32res.end(body)33}3435function writePacFile(res) {36writeResponse(37res,38[39'function FindProxyForURL(url, host) {',40' if (shExpMatch(url, "' + goodbyeServer.url('*') + '")) {',41' return "DIRECT";',42' }',43' return "PROXY ' + proxyServer.host() + '";',44'}',45].join('\n'),46'ascii',47'application/x-javascript-config',48)49}5051const proxyServer = new Server(function (req, res) {52const pathname = new URL(req.url).pathname53if (pathname === '/proxy.pac') {54return writePacFile(res)55}5657writeResponse(58res,59['<!DOCTYPE html>', '<title>Proxy page</title>', '<h3>This is the proxy landing page</h3>'].join(''),60'utf8',61'text/html; charset=UTF-8',62)63})6465const helloServer = new Server(function (_req, res) {66writeResponse(67res,68['<!DOCTYPE html>', '<title>Hello</title>', '<h3>Hello, world!</h3>'].join(''),69'utf8',70'text/html; charset=UTF-8',71)72})7374const goodbyeServer = new Server(function (_req, res) {75writeResponse(76res,77['<!DOCTYPE html>', '<title>Goodbye</title>', '<h3>Goodbye, world!</h3>'].join(''),78'utf8',79'text/html; charset=UTF-8',80)81})8283// Cannot pass start directly to mocha's before, as mocha will interpret the optional84// port parameter as an async callback parameter.85function mkStartFunc(server) {86return function () {87return server.start()88}89}9091before(mkStartFunc(proxyServer))92before(mkStartFunc(helloServer))93before(mkStartFunc(goodbyeServer))9495after(proxyServer.stop.bind(proxyServer))96after(helloServer.stop.bind(helloServer))97after(goodbyeServer.stop.bind(goodbyeServer))9899let driver100beforeEach(function () {101driver = null102})103afterEach(function () {104return driver && driver.quit()105})106107function createDriver(proxy) {108return (driver = env.builder().setProxy(proxy).build())109}110111// Proxy support not implemented.112test113.ignore(env.browsers(Browser.CHROME, Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.FIREFOX))114.describe('manual proxy settings', function () {115it('can configure HTTP proxy host', async function () {116await createDriver(117proxy.manual({118http: proxyServer.host(),119bypass: [],120}),121)122123await driver.get(helloServer.url())124assert.strictEqual(await driver.getTitle(), 'Proxy page')125assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')126})127128it('can bypass proxy for specific hosts', async function () {129await createDriver(130proxy.manual({131http: proxyServer.host(),132bypass: [helloServer.host()],133}),134)135136await driver.get(helloServer.url())137assert.strictEqual(await driver.getTitle(), 'Hello')138assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'Hello, world!')139140// For firefox the no proxy settings appear to match on hostname only.141let url = goodbyeServer.url().replace(/127\.0\.0\.1/, 'localhost')142await driver.get(url)143assert.strictEqual(await driver.getTitle(), 'Proxy page')144assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')145})146147// TODO: test ftp and https proxies.148})149150// PhantomJS does not support PAC file proxy configuration.151// Safari does not support proxies.152test153.ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.CHROME, Browser.FIREFOX))154.describe('pac proxy settings', function () {155it('can configure proxy through PAC file', async function () {156await createDriver(proxy.pac(proxyServer.url('/proxy.pac')))157158await driver.get(helloServer.url())159assert.strictEqual(await driver.getTitle(), 'Proxy page')160assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')161162await driver.get(goodbyeServer.url())163assert.strictEqual(await driver.getTitle(), 'Goodbye')164assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'Goodbye, world!')165})166})167})168169170