# encoding: utf-8 require 'spec_helper' require 'spec_helper_features' describe 'Lookup proxy for url' do context '/v1/lookup' do let(:valid_pac_file) do <<-EOS.strip_heredoc.chomp function FindProxyForURL(url, host) { return "DIRECT"; } EOS end let(:translated_pac_file) do <<-EOS.strip_heredoc.chomp function FindProxyForURL(url, host) { return "PROXY localhost:3128"; } EOS end let(:client_valid_pac_file) do <<-EOS.strip_heredoc.chomp function FindProxyForURL(url, host) { if ( MyIpAddress() == '127.0.0.5' ) { return "PROXY localhost:3128"; } else { return "DIRECT"; } } EOS end let(:time_valid_pac_file) do <<-EOS.strip_heredoc.chomp function FindProxyForURL(url, host) { if ( weekdayRange('SUN', 'TUE') ) { return "PROXY localhost:3128"; } else { return "DIRECT"; } } EOS end let(:pac_file_return_nil) do <<-EOS.strip_heredoc.chomp function FindProxyForURL(url, host) { return ""; } EOS end let(:git_repo) { File.join(working_directory, 'git_repo.git') } before :each do config = Class.new do include FeduxOrg::Stdlib::Filesystem def root_directory ::File.expand_path('../../../', __FILE__) end def local_storage ::File.join(working_directory, 'git_repo.git') end def translation_file ::File.join(working_directory, 'translation.csv') end end.new LocalPac.config = config Capybara.app = LocalPac::App::LookupController.new end it 'looks up proxy for valid url' do repo = GitRepository.create(git_repo) repo.add_content('file.pac', valid_pac_file) search_for url: 'http://www.example.org', content: 'You asked me to look up' end it 'looks up proxy for valid url and changes client ip' do repo = GitRepository.create(git_repo) repo.add_content('client.pac', client_valid_pac_file) search_for url: 'http://www.example.org', content: 'PROXY localhost:3128', client_ip: '127.0.0.5', pac_file: '/client.pac' end it 'looks up proxy for valid url and changes time' do repo = GitRepository.create(git_repo) repo.add_content('time.pac', time_valid_pac_file) search_for url: 'http://www.example.org', content: 'PROXY localhost:3128', time: Time.parse('2014-03-10'), pac_file: '/time.pac' end it 'looks up proxy for translated proxy pac' do repo = GitRepository.create(git_repo) repo.add_content('translated.pac', translated_pac_file) create_file 'translation.csv', <<-EOS.strip_heredoc "network","requested_file","rewritten_file" "127.0.0.0/8","file.pac","translated.pac" EOS search_for url: 'http://www.example.org', content: 'PROXY localhost:3128' end it 'has a default ip' do repo = GitRepository.create(git_repo) repo.add_content('file.pac', valid_pac_file) visit('/file.pac') expect(page).to have_xpath("//input[@value='127.0.0.1']") end it 'has a default time' do repo = GitRepository.create(git_repo) repo.add_content('file.pac', valid_pac_file) visit('/file.pac') expect(page).to have_xpath("//input[@value='#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}']") end it 'returns a error message for a invalid url' do repo = GitRepository.create(git_repo) repo.add_content('file.pac', valid_pac_file) visit('/file.pac') within('#search') do fill_in 'url', :with => '§ASDF$$' end click_on('Search') expect(page).to have_content('Invalid URL...') end it 'returns a error message for a invalid time' do repo = GitRepository.create(git_repo) repo.add_content('file.pac', valid_pac_file) visit('/file.pac') within('#search') do fill_in 'url', :with => 'http://example.com' fill_in 'time', :with => 'GARBAGE' end click_on('Search') expect(page).to have_content('Invalid Time...') end it 'returns a error message for a invalid client ip' do repo = GitRepository.create(git_repo) repo.add_content('file.pac', valid_pac_file) visit('/file.pac') within('#search') do fill_in 'url', :with => 'http://example.com' fill_in 'client_ip', :with => 'GARBAGE' end click_on('Search') expect(page).to have_content('Invalid Client IP...') end it 'returns a error message for a invalid url on the second page' do repo = GitRepository.create(git_repo) repo.add_content('file.pac', valid_pac_file) visit('/file.pac') within('#search') do fill_in 'url', :with => 'http://www.example.org' end click_on('Search') within('#search') do fill_in 'url', :with => '§ASDF$$' end click_on('Search') expect(page).to have_content('Invalid URL...') end it 'returns a error message for a invalid url' do repo = GitRepository.create(git_repo) repo.add_content('file.pac', valid_pac_file) visit('/file.pac') within('#search') do fill_in 'url', :with => '' end click_on('Search') expect(page).to have_content('Invalid URL...') end end end