proxy_test.js 6.2 KB

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