UNPKG

6.32 kBJavaScriptView Raw
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
20var http = require('http'),
21 url = require('url');
22
23var Browser = require('..').Browser,
24 promise = require('..').promise,
25 firefox = require('../firefox'),
26 proxy = require('../proxy'),
27 assert = require('../testing/assert'),
28 test = require('../lib/test'),
29 Server = require('../lib/test/httpserver').Server,
30 Pages = test.Pages;
31
32test.suite(function(env) {
33 function writeResponse(res, body, encoding, contentType) {
34 res.writeHead(200, {
35 'Content-Length': Buffer.byteLength(body, encoding),
36 'Content-Type': contentType
37 });
38 res.end(body);
39 }
40
41 function writePacFile(res) {
42 writeResponse(res, [
43 'function FindProxyForURL(url, host) {',
44 ' if (shExpMatch(url, "' + goodbyeServer.url('*') + '")) {',
45 ' return "DIRECT";',
46 ' }',
47 ' return "PROXY ' + proxyServer.host() + '";',
48 '}'
49 ].join('\n'), 'ascii', 'application/x-javascript-config');
50 }
51
52 var proxyServer = new Server(function(req, res) {
53 var pathname = url.parse(req.url).pathname;
54 if (pathname === '/proxy.pac') {
55 return writePacFile(res);
56 }
57
58 writeResponse(res, [
59 '<!DOCTYPE html>',
60 '<title>Proxy page</title>',
61 '<h3>This is the proxy landing page</h3>'
62 ].join(''), 'utf8', 'text/html; charset=UTF-8');
63 });
64
65 var helloServer = new Server(function(req, res) {
66 writeResponse(res, [
67 '<!DOCTYPE html>',
68 '<title>Hello</title>',
69 '<h3>Hello, world!</h3>'
70 ].join(''), 'utf8', 'text/html; charset=UTF-8');
71 });
72
73 var goodbyeServer = new Server(function(req, res) {
74 writeResponse(res, [
75 '<!DOCTYPE html>',
76 '<title>Goodbye</title>',
77 '<h3>Goodbye, world!</h3>'
78 ].join(''), 'utf8', 'text/html; charset=UTF-8');
79 });
80
81 // Cannot pass start directly to mocha's before, as mocha will interpret the optional
82 // port parameter as an async callback parameter.
83 function mkStartFunc(server) {
84 return function() {
85 return server.start();
86 };
87 }
88
89 test.before(mkStartFunc(proxyServer));
90 test.before(mkStartFunc(helloServer));
91 test.before(mkStartFunc(goodbyeServer));
92
93 test.after(proxyServer.stop.bind(proxyServer));
94 test.after(helloServer.stop.bind(helloServer));
95 test.after(goodbyeServer.stop.bind(goodbyeServer));
96
97 var driver;
98 test.beforeEach(function() { driver = null; });
99 test.afterEach(function() { return driver && driver.quit(); });
100
101 function createDriver(proxy) {
102 // For Firefox we need to explicitly enable proxies for localhost by
103 // clearing the network.proxy.no_proxies_on preference.
104 let profile = new firefox.Profile();
105 profile.setPreference('network.proxy.no_proxies_on', '');
106
107 return driver = env.builder()
108 .setFirefoxOptions(new firefox.Options().setProfile(profile))
109 .setProxy(proxy)
110 .build();
111 }
112
113 // Proxy support not implemented.
114 test.ignore(env.browsers(Browser.IE, Browser.OPERA, Browser.SAFARI)).
115 describe('manual proxy settings', function() {
116 // phantomjs 1.9.1 in webdriver mode does not appear to respect proxy
117 // settings.
118 test.ignore(env.browsers(Browser.PHANTOM_JS)).
119 it('can configure HTTP proxy host', function*() {
120 yield createDriver(proxy.manual({
121 http: proxyServer.host()
122 }));
123
124 yield driver.get(helloServer.url());
125 yield assert(driver.getTitle()).equalTo('Proxy page');
126 yield assert(driver.findElement({tagName: 'h3'}).getText()).
127 equalTo('This is the proxy landing page');
128 });
129
130 // PhantomJS does not support bypassing the proxy for individual hosts.
131 // geckodriver does not support the bypass option, this must be configured
132 // through profile preferences.
133 test.ignore(env.browsers(
134 Browser.FIREFOX,
135 'legacy-' + Browser.FIREFOX,
136 Browser.PHANTOM_JS)).
137 it('can bypass proxy for specific hosts', function*() {
138 yield createDriver(proxy.manual({
139 http: proxyServer.host(),
140 bypass: helloServer.host()
141 }));
142
143 yield driver.get(helloServer.url());
144 yield assert(driver.getTitle()).equalTo('Hello');
145 yield assert(driver.findElement({tagName: 'h3'}).getText()).
146 equalTo('Hello, world!');
147
148 yield driver.get(goodbyeServer.url());
149 yield assert(driver.getTitle()).equalTo('Proxy page');
150 yield assert(driver.findElement({tagName: 'h3'}).getText()).
151 equalTo('This is the proxy landing page');
152 });
153
154 // TODO: test ftp and https proxies.
155 });
156
157 // PhantomJS does not support PAC file proxy configuration.
158 // Safari does not support proxies.
159 test.ignore(env.browsers(
160 Browser.IE, Browser.OPERA, Browser.PHANTOM_JS, Browser.SAFARI)).
161 describe('pac proxy settings', function() {
162 test.it('can configure proxy through PAC file', function*() {
163 yield createDriver(proxy.pac(proxyServer.url('/proxy.pac')));
164
165 yield driver.get(helloServer.url());
166 yield assert(driver.getTitle()).equalTo('Proxy page');
167 yield assert(driver.findElement({tagName: 'h3'}).getText()).
168 equalTo('This is the proxy landing page');
169
170 yield driver.get(goodbyeServer.url());
171 yield assert(driver.getTitle()).equalTo('Goodbye');
172 yield assert(driver.findElement({tagName: 'h3'}).getText()).
173 equalTo('Goodbye, world!');
174 });
175 });
176
177 // TODO: figure out how to test direct and system proxy settings.
178 describe.skip('direct proxy settings', function() {});
179 describe.skip('system proxy settings', function() {});
180});