# encoding: utf-8 require 'spec_helper' describe ProxyPac::PACEngine do let(:valid_pac_file) do <<-EOS.strip_heredoc function FindProxyForURL(url, host) { return "DIRECT" } EOS end let(:client_ip_pac) do <<-EOS.strip_heredoc function FindProxyForURL(url, host) { if ( MyIpAddress() == '127.0.0.2' ) { return "DIRECT"; } else { return "PROXY localhost:8080"; } } EOS end context '#find' do it 'returns a result' do file = double('PacFile') expect(file).to receive(:content).and_return(valid_pac_file) parser = ProxyPac::PACEngine.new(file: file) result = parser.find(Addressable::URI.parse('http://example.org')) expect(result).to eq('DIRECT') end it 'makes use of an environment' do file = double('PacFile') expect(file).to receive(:content).and_return(client_ip_pac) parser = ProxyPac::PACEngine.new(file: file, env: { client_ip: '127.0.0.1'}) result = parser.find(Addressable::URI.parse('http://example.org')) expect(result).to eq('PROXY localhost:8080') end it 'raises an error if pac file is invalid' do file = double('PacFile') allow(file).to receive(:content).and_return('asdfasdf()') parser = ProxyPac::PACEngine.new(file: file) expect do silence(:stderr) do parser.find(Addressable::URI.parse('http://example.org')) end end.to raise_error Exceptions::PacFileInvalid end end end