This commit is contained in:
FireFart
2014-02-28 21:49:01 +01:00
parent 3f3f5fdaa0
commit 68e47d70fd
9 changed files with 19 additions and 292 deletions

View File

@@ -6,29 +6,15 @@ describe Browser do
it_behaves_like 'Browser::Actions'
it_behaves_like 'Browser::Options'
CONFIG_FILE_WITHOUT_PROXY = SPEC_FIXTURES_CONF_DIR + '/browser/browser.conf.json'
CONFIG_FILE_WITH_PROXY = SPEC_FIXTURES_CONF_DIR + '/browser/browser.conf_proxy.json'
#CONFIG_FILE_WITH_PROXY_AND_AUTH = SPEC_FIXTURES_CONF_DIR + '/browser/browser.conf_proxy_auth.json'
subject(:browser) {
Browser.reset
Browser.instance(options)
}
let(:options) { {} }
let(:instance_vars_to_check) {
['user_agent', 'user_agent_mode', 'available_user_agents', 'proxy',
['useragent', 'proxy',
'max_threads', 'cache_ttl', 'request_timeout', 'connect_timeout']
}
let(:json_config_without_proxy) { JSON.parse(File.read(CONFIG_FILE_WITHOUT_PROXY)) }
let(:json_config_with_proxy) { JSON.parse(File.read(CONFIG_FILE_WITH_PROXY)) }
def check_instance_variables(browser, json_expected_vars)
json_expected_vars['max_threads'] ||= 1 # max_thread can not be nil
instance_vars_to_check.each do |variable_name|
browser.send(:"#{variable_name}").should === json_expected_vars[variable_name]
end
end
describe 'Singleton' do
it 'should not allow #new' do
@@ -36,59 +22,6 @@ describe Browser do
end
end
describe '::instance' do
after { check_instance_variables(browser, @json_expected_vars) }
context "when default config_file = #{CONFIG_FILE_WITHOUT_PROXY}" do
it 'will check the instance vars' do
@json_expected_vars = json_config_without_proxy
end
end
context "when :config_file = #{CONFIG_FILE_WITH_PROXY}" do
let(:options) { { config_file: CONFIG_FILE_WITH_PROXY } }
it 'will check the instance vars' do
@json_expected_vars = json_config_with_proxy
end
end
context 'when options[:cache_dir]' do
let(:cache_dir) { CACHE_DIR + '/somewhere' }
let(:options) { { cache_dir: cache_dir } }
after { subject.cache_dir.should == cache_dir }
it 'sets @cache_dir' do
@json_expected_vars = json_config_without_proxy
end
end
end
describe '#load_config' do
context 'when config_file is a symlink' do
let(:config_file) { './rspec_symlink' }
it 'raises an error' do
File.symlink('./testfile', config_file)
expect { browser.load_config(config_file) }.to raise_error("[ERROR] Config file is a symlink.")
File.unlink(config_file)
end
end
context 'otherwise' do
after do
browser.load_config(@config_file)
check_instance_variables(browser, @expected)
end
it 'sets the correct variables' do
@config_file = CONFIG_FILE_WITH_PROXY
@expected = json_config_without_proxy.merge(json_config_with_proxy)
end
end
end
describe '::append_params_header_field' do
after :each do
Browser.append_params_header_field(
@@ -143,7 +76,7 @@ describe Browser do
}
after :each do
browser.stub(user_agent: 'SomeUA')
browser.useragent = 'SomeUA'
browser.cache_ttl = 250
browser.merge_request_params(params).should == @expected

View File

@@ -1,8 +0,0 @@
{
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0) Gecko/20100101 Firefox/9.0",
"user_agent_mode": "static",
"cache_ttl": 300,
"request_timeout": 2000,
"connect_timeout": 1000,
"max_threads": 5
}

View File

@@ -1,8 +0,0 @@
{
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0) Gecko/20100101 Firefox/11.0",
"user_agent_mode": "static",
"proxy": "127.0.0.1:3038",
"cache_ttl": 300,
"request_timeout": 2000,
"connect_timeout": 1000
}

View File

@@ -1,9 +0,0 @@
{
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0) Gecko/20100101 Firefox/11.0",
"user_agent_mode": "static",
"proxy": "127.0.0.1:3038",
"proxy_auth": "user:pass",
"cache_ttl": 300,
"request_timeout": 2000,
"connect_timeout": 1000
}

View File

@@ -71,69 +71,6 @@ shared_examples 'Browser::Options' do
end
end
describe '#user_agent_mode= & #user_agent_mode' do
# Testing all valid modes
Browser::USER_AGENT_MODES.each do |user_agent_mode|
it "sets & returns #{user_agent_mode}" do
browser.user_agent_mode = user_agent_mode
browser.user_agent_mode.should === user_agent_mode
end
end
it 'sets the mode to "static" if nil is given' do
browser.user_agent_mode = nil
browser.user_agent_mode.should === 'static'
end
it 'raises an error if the mode is not valid' do
expect { browser.user_agent_mode = 'invalid-mode' }.to raise_error
end
end
describe '#user_agent= & #user_agent' do
let(:available_user_agents) { %w{ ua-1 ua-2 ua-3 ua-4 ua-6 ua-7 ua-8 ua-9 ua-10 ua-11 ua-12 ua-13 ua-14 ua-15 ua-16 ua-17 } }
context 'when static mode' do
it 'returns the same user agent' do
browser.user_agent = 'fake UA'
browser.user_agent_mode = 'static'
(1..3).each do
browser.user_agent.should === 'fake UA'
end
end
end
context 'when semi-static mode' do
it 'chooses a random user_agent in the available_user_agents array and always return it' do
browser.available_user_agents = available_user_agents
browser.user_agent = 'Firefox 11.0'
browser.user_agent_mode = 'semi-static'
user_agent = browser.user_agent
user_agent.should_not === 'Firefox 11.0'
available_user_agents.include?(user_agent).should be_true
(1..3).each do
browser.user_agent.should === user_agent
end
end
end
context 'when random' do
it 'returns a random user agent each time' do
browser.available_user_agents = available_user_agents
browser.user_agent_mode = 'random'
ua_1 = browser.user_agent
ua_2 = browser.user_agent
ua_3 = browser.user_agent
fail if ua_1 === ua_2 and ua_2 === ua_3
end
end
end
describe 'proxy=' do
let(:exception) { 'Invalid proxy format. Should be [protocol://]host:port.' }