# encoding: utf-8 require 'spec_helper' describe 'Spec helper' do let(:valid_pac_file_url) do <<-EOS.strip_heredoc.chomp function FindProxyForURL(url, host) { if (url == 'www.heise.de') { return "DIRECT"; } else { return "PROXY localhost2:3128"; } } EOS end before :each do @__proxy = CapybaraProxy.new end context '#use_proxy' do it 'defaults to host' do @__proxy = double('proxy') expect(proxy).to receive(:host=).with('localhost1') expect(proxy).to receive(:port=).with('3128') use_proxy 'localhost1:3128' end it 'fails on invalid symbol' do expect { use_proxy :asdf, 'localhost1:3128' }.to raise_error Exceptions::SyntaxInvalid end it 'works with :host and connection string' do @__proxy = double('proxy') expect(proxy).to receive(:host=).with('localhost1') expect(proxy).to receive(:port=).with('3128') use_proxy :host, 'localhost1:3128' end it 'works with :pac and proxy pac' do @__proxy_pac = double('proxy_pac') expect(proxy_pac).to receive(:pac_file=).with('http://localhost1:4567') use_proxy :pac, 'http://localhost1:4567' end it 'finds proxy for proxy pac' do @__proxy = double('proxy') expect(proxy).to receive(:host=).with('localhost2') expect(proxy).to receive(:port=).with('3128') allow(proxy).to receive(:as_phantomjs_arguments).and_return(%w{--proxy localhost2:3128}) allow(proxy).to receive(:to_sym).and_return(:localhost2_3128) file = create_file 'proxy.pac', valid_pac_file_url use_proxy :pac, file begin use_timeout(0) do visit 'http://www.example.com' end rescue Exceptions::FetchUrlTimeout end end it 'accepts an type as well (long form)' do @__proxy = double('proxy') expect(proxy).to receive(:host=).with('localhost1') expect(proxy).to receive(:port=).with('3128') expect(proxy).to receive(:type=).with(:none) use_proxy :host, 'localhost1:3128', type: :none end it 'accepts an type as well (short form)' do @__proxy = double('proxy') expect(proxy).to receive(:host=).with('localhost1') expect(proxy).to receive(:port=).with('3128') expect(proxy).to receive(:type=).with(:none) use_proxy 'localhost1:3128', type: :none end end context '#use_time' do it 'sets time for use in proxy pac' do time = Time.now @__proxy_pac = double('proxy_pac') expect(proxy_pac).to receive(:time=).with(time) use_time time end end context '#use_client_ip' do it 'sets client ip for use in proxy pac' do client_ip = IPAddr.new('127.0.0.1').to_s @__proxy_pac = double('proxy_pac') expect(proxy_pac).to receive(:client_ip=).with(client_ip) use_client_ip client_ip end end context '#use_user' do it 'works with a valid user' do user = create :user @__proxy = double('proxy') expect(proxy).to receive(:user=).with(user) allow(proxy).to receive(:as_phantomjs_arguments).and_return([]) use_user 'user' end it 'works with a valid user via proxy' do create :user use_proxy 'localhost:3128' use_user 'user' visit('http://www.example.org') end it 'fails with an invalid user' do expect { silence :stderr do use_user 'user' end }.to raise_error Exceptions::ProxyUserInvalid end end end