Browser::Options done
This commit is contained in:
@@ -8,12 +8,16 @@ describe Browser do
|
||||
|
||||
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'
|
||||
INSTANCE_VARS_TO_CHECK = ['user_agent', 'user_agent_mode', 'available_user_agents', 'proxy', 'max_threads', 'cache_ttl']
|
||||
#CONFIG_FILE_WITH_PROXY_AND_AUTH = SPEC_FIXTURES_CONF_DIR + '/browser/browser.conf_proxy_auth.json'
|
||||
|
||||
subject(:browser) {
|
||||
Browser::reset
|
||||
Browser.instance
|
||||
Browser.reset
|
||||
Browser.instance(options)
|
||||
}
|
||||
let(:options) { {} }
|
||||
let(:instance_vars_to_check) {
|
||||
['user_agent', 'user_agent_mode', 'available_user_agents', 'proxy',
|
||||
'max_threads', 'cache_ttl']
|
||||
}
|
||||
|
||||
before :all do
|
||||
@@ -21,146 +25,11 @@ describe Browser do
|
||||
@json_config_with_proxy = JSON.parse(File.read(CONFIG_FILE_WITH_PROXY))
|
||||
end
|
||||
|
||||
before :each do
|
||||
Browser::reset
|
||||
@browser = Browser.instance(config_file: CONFIG_FILE_WITHOUT_PROXY)
|
||||
end
|
||||
|
||||
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 |instance_variable_name|
|
||||
browser.send(:"#{instance_variable_name}").should === json_expected_vars[instance_variable_name]
|
||||
end
|
||||
end
|
||||
|
||||
describe '#user_agent_mode setter / getter' do
|
||||
# Testing all valid modes
|
||||
Browser::USER_AGENT_MODES.each do |user_agent_mode|
|
||||
it "should set / return #{user_agent_mode}" do
|
||||
@browser.user_agent_mode = user_agent_mode
|
||||
@browser.user_agent_mode.should === user_agent_mode
|
||||
end
|
||||
end
|
||||
|
||||
it "shoud set the mode to 'static' if nil is given" do
|
||||
@browser.user_agent_mode = nil
|
||||
@browser.user_agent_mode.should === 'static'
|
||||
end
|
||||
|
||||
it 'should raise an error if the mode in not valid' do
|
||||
expect { @browser.user_agent_mode = 'invalid-mode' }.to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
#describe '#max_threads=' do
|
||||
# it 'should set max_threads to 1 if nil is given' do
|
||||
# @browser.max_threads = nil
|
||||
# @browser.max_threads.should === 1
|
||||
# end
|
||||
#
|
||||
# it 'should set max_threads to 1 if 0 is given' do
|
||||
# @browser.max_threads = 0
|
||||
# @browser.max_threads.should === 1
|
||||
# end
|
||||
#end
|
||||
|
||||
describe '#proxy_auth=' do
|
||||
after :each do
|
||||
if @raise_error
|
||||
expect { @browser.proxy_auth = @proxy_auth }.to raise_error
|
||||
else
|
||||
@browser.proxy_auth = @proxy_auth
|
||||
@browser.proxy_auth.should === @expected
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the auth supplied is' do
|
||||
|
||||
context 'not a String or a Hash' do
|
||||
it 'raises an error' do
|
||||
@proxy_auth = 10
|
||||
@raise_error = true
|
||||
end
|
||||
end
|
||||
|
||||
context 'a String with' do
|
||||
context 'invalid format' do
|
||||
it 'raises an error' do
|
||||
@proxy_auth = 'invaludauthformat'
|
||||
@raise_error = true
|
||||
end
|
||||
end
|
||||
|
||||
context 'valid format' do
|
||||
it 'sets the auth' do
|
||||
@proxy_auth = 'username:passwd'
|
||||
@expected = @proxy_auth
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'a Hash with' do
|
||||
context 'only :proxy_username' do
|
||||
it 'raises an error' do
|
||||
@proxy_auth = { proxy_username: 'username' }
|
||||
@raise_error = true
|
||||
end
|
||||
end
|
||||
|
||||
context 'only :proxy_password' do
|
||||
it 'raises an error' do
|
||||
@proxy_auth = { proxy_password: 'hello' }
|
||||
@raise_error = true
|
||||
end
|
||||
end
|
||||
|
||||
context ':proxy_username and :proxy_password' do
|
||||
it 'sets the auth' do
|
||||
@proxy_auth = { proxy_username: 'user', proxy_password: 'pass' }
|
||||
@expected = 'user:pass'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
describe '#user_agent' do
|
||||
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 }
|
||||
|
||||
it 'should always return the same user agent in static mode' do
|
||||
@browser.user_agent = 'fake UA'
|
||||
@browser.user_agent_mode = 'static'
|
||||
|
||||
(1..3).each do
|
||||
@browser.user_agent.should === 'fake UA'
|
||||
end
|
||||
end
|
||||
|
||||
it 'should choose a random user_agent in the available_user_agents array an 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
|
||||
|
||||
it 'should return 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
|
||||
instance_vars_to_check.each do |variable_name|
|
||||
browser.send(:"#{variable_name}").should === json_expected_vars[variable_name]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -170,48 +39,32 @@ describe Browser do
|
||||
end
|
||||
end
|
||||
|
||||
describe "#instance with :config_file = #{CONFIG_FILE_WITHOUT_PROXY}" do
|
||||
it 'will check the instance vars' do
|
||||
Browser.reset
|
||||
check_instance_variables(
|
||||
Browser.instance(config_file: CONFIG_FILE_WITHOUT_PROXY),
|
||||
@json_config_without_proxy
|
||||
)
|
||||
end
|
||||
end
|
||||
describe '::instance' do
|
||||
after { check_instance_variables(browser, @json_expected_vars) }
|
||||
|
||||
describe "#instance with :config_file = #{CONFIG_FILE_WITH_PROXY}" do
|
||||
it 'will check the instance vars' do
|
||||
Browser.reset
|
||||
check_instance_variables(
|
||||
Browser.instance(config_file: CONFIG_FILE_WITH_PROXY),
|
||||
@json_config_with_proxy
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# TODO Write something to test all possible overriding
|
||||
describe 'override option : user_agent & threads' do
|
||||
it 'will check the instance vars, with an overriden one' do
|
||||
Browser.reset
|
||||
check_instance_variables(
|
||||
Browser.instance(
|
||||
config_file: CONFIG_FILE_WITHOUT_PROXY,
|
||||
user_agent: 'fake IE'
|
||||
),
|
||||
@json_config_without_proxy.merge('user_agent' => 'fake IE')
|
||||
)
|
||||
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
|
||||
|
||||
it 'should not override the max_threads if max_threads = nil' do
|
||||
Browser.reset
|
||||
check_instance_variables(
|
||||
Browser.instance(
|
||||
config_file: CONFIG_FILE_WITHOUT_PROXY,
|
||||
max_threads: nil
|
||||
),
|
||||
@json_config_without_proxy
|
||||
)
|
||||
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
|
||||
|
||||
@@ -219,7 +72,6 @@ describe Browser do
|
||||
describe '#load_config' do
|
||||
it 'should raise an error if file is a symlink' do
|
||||
symlink = './rspec_symlink'
|
||||
browser = Browser.instance
|
||||
|
||||
File.symlink('./testfile', symlink)
|
||||
expect { browser.load_config(symlink) }.to raise_error("[ERROR] Config file is a symlink.")
|
||||
@@ -227,7 +79,7 @@ describe Browser do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#append_params_header_field' do
|
||||
describe '::append_params_header_field' do
|
||||
after :each do
|
||||
Browser.append_params_header_field(
|
||||
@params,
|
||||
@@ -264,7 +116,6 @@ describe Browser do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe '#merge_request_params' do
|
||||
@@ -280,10 +131,10 @@ describe Browser do
|
||||
}
|
||||
|
||||
after :each do
|
||||
@browser.stub(user_agent: 'SomeUA')
|
||||
@browser.cache_ttl = 250
|
||||
browser.stub(user_agent: 'SomeUA')
|
||||
browser.cache_ttl = 250
|
||||
|
||||
@browser.merge_request_params(params).should == @expected
|
||||
browser.merge_request_params(params).should == @expected
|
||||
end
|
||||
|
||||
it 'sets the User-Agent header field and cache_ttl' do
|
||||
@@ -296,27 +147,26 @@ describe Browser do
|
||||
let(:proxy_expectation) { default_expectation.merge(proxy: proxy) }
|
||||
|
||||
it 'merges the proxy' do
|
||||
@browser.proxy = proxy
|
||||
@expected = proxy_expectation
|
||||
browser.proxy = proxy
|
||||
@expected = proxy_expectation
|
||||
end
|
||||
|
||||
context 'when @proxy_auth' do
|
||||
it 'sets the proxy_auth' do
|
||||
@browser.proxy = proxy
|
||||
@browser.proxy_auth = 'user:pass'
|
||||
@expected = proxy_expectation.merge(proxyauth: 'user:pass')
|
||||
browser.proxy = proxy
|
||||
browser.proxy_auth = 'user:pass'
|
||||
@expected = proxy_expectation.merge(proxyauth: 'user:pass')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when @basic_auth' do
|
||||
it 'appends the basic_auth' do
|
||||
@browser.basic_auth = 'user:pass'
|
||||
browser.basic_auth = 'user:pass'
|
||||
@expected = default_expectation.merge(
|
||||
headers: default_expectation[:headers].merge('Authorization' => 'Basic '+Base64.encode64('user:pass'))
|
||||
headers: default_expectation[:headers].merge('Authorization' => 'Basic '+Base64.encode64('user:pass').chomp)
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'when the cache_ttl is alreday set' do
|
||||
@@ -326,11 +176,19 @@ describe Browser do
|
||||
@expected = default_expectation.merge(params)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# TODO
|
||||
describe '#forge_request' do
|
||||
let(:url) { 'http://example.localhost' }
|
||||
|
||||
it 'returns the correct Typhoeus::Request' do
|
||||
subject.stub(merge_request_params: { cache_ttl: 10 })
|
||||
|
||||
request = subject.forge_request(url)
|
||||
request.should be_a Typhoeus::Request
|
||||
request.url.should == url
|
||||
request.cache_ttl.should == 10
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -359,4 +217,3 @@ describe Browser do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user