diff --git a/Gemfile b/Gemfile index b27aafd4..a11f8caa 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,6 @@ source "https://rubygems.org" -gem "typhoeus", ">=0.6.2" -gem "ethon", :git => "https://github.com/typhoeus/ethon.git" +gem "typhoeus", ">=0.6.3" gem "nokogiri" gem "json" diff --git a/lib/common/browser.rb b/lib/common/browser.rb index 250f9ed6..cb6d0ab2 100644 --- a/lib/common/browser.rb +++ b/lib/common/browser.rb @@ -12,6 +12,7 @@ class Browser attr_reader :hydra, :config_file + # @param [ Hash ] options def initialize(options = {}) @config_file = options[:config_file] || CONF_DIR + '/browser.conf.json' @cache_dir = CACHE_DIR + '/browser' @@ -56,10 +57,12 @@ class Browser data = JSON.parse(File.read(@config_file)) end - ACCESSOR_OPTIONS.each do |option| + Options::OPTIONS.each do |option| option_name = option.to_s - self.send(:"#{option_name}=", data[option_name]) + if data[option_name] + self.send(:"#{option_name}=", data[option_name]) + end end end diff --git a/lib/common/browser/options.rb b/lib/common/browser/options.rb index a91850db..23ec2949 100644 --- a/lib/common/browser/options.rb +++ b/lib/common/browser/options.rb @@ -31,7 +31,7 @@ class Browser # @return [ void ] def basic_auth=(auth) if auth.index(':') - @basic_auth = "Basic #{Base64.encode64(basic_auth.chomp)}" + @basic_auth = "Basic #{Base64.encode64(auth.chomp)}" elsif auth =~ /\ABasic .*\z/ @basic_auth = auth.chomp else @@ -44,19 +44,6 @@ class Browser @max_threads || 1 end - # @return [ String ] The user agent, according to the user_agent_mode - def user_agent - case @user_agent_mode - when 'semi-static' - unless @user_agent - @user_agent = @available_user_agents.sample - end - when 'random' - @user_agent = @available_user_agents.sample - end - @user_agent - end - # Sets the user_agent_mode, which can be one of the following: # static: The UA is defined by the user, and will be the same in each requests # semi-static: The UA is randomly chosen at the first request, and will not change @@ -78,6 +65,19 @@ class Browser end end + # @return [ String ] The user agent, according to the user_agent_mode + def user_agent + case @user_agent_mode + when 'semi-static' + unless @user_agent + @user_agent = @available_user_agents.sample + end + when 'random' + @user_agent = @available_user_agents.sample + end + @user_agent + end + # Sets the proxy # Accepted format: # host:post diff --git a/lib/common/common_helper.rb b/lib/common/common_helper.rb index 2c5d2bed..71f54a36 100644 --- a/lib/common/common_helper.rb +++ b/lib/common/common_helper.rb @@ -107,3 +107,8 @@ def xml(file) config.noblanks end end + +def redefine_constant(constant, value) + Object.send(:remove_const, constant) + Object.const_set(constant, value) +end diff --git a/spec/lib/common/browser_spec.rb b/spec/lib/common/browser_spec.rb index ae62ea60..e3c4b0b2 100644 --- a/spec/lib/common/browser_spec.rb +++ b/spec/lib/common/browser_spec.rb @@ -4,11 +4,17 @@ require 'spec_helper' 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' - INSTANCE_VARS_TO_CHECK = ['user_agent', 'user_agent_mode', 'available_user_agents', 'proxy', 'max_threads', 'request_timeout', 'cache_ttl'] + INSTANCE_VARS_TO_CHECK = ['user_agent', 'user_agent_mode', 'available_user_agents', 'proxy', 'max_threads', 'cache_ttl'] + + subject(:browser) { + Browser::reset + Browser.instance + } before :all do @json_config_without_proxy = JSON.parse(File.read(CONFIG_FILE_WITHOUT_PROXY)) @@ -47,17 +53,17 @@ describe Browser do 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 '#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 @@ -305,9 +311,9 @@ describe Browser do context 'when @basic_auth' do it 'appends the basic_auth' do - @browser.basic_auth = 'basic-auth' + @browser.basic_auth = 'user:pass' @expected = default_expectation.merge( - headers: default_expectation[:headers].merge('Authorization' => 'basic-auth') + headers: default_expectation[:headers].merge('Authorization' => 'Basic '+Base64.encode64('user:pass')) ) end diff --git a/spec/shared_examples/browser/options.rb b/spec/shared_examples/browser/options.rb index 93ba5bab..a6a6378b 100644 --- a/spec/shared_examples/browser/options.rb +++ b/spec/shared_examples/browser/options.rb @@ -2,4 +2,32 @@ shared_examples 'Browser::Options' do + describe 'basic_auth=' do + + end + + describe 'max_threads' do + + end + + describe 'user_agent=' do + + end + + describe 'user_agent' do + + end + + describe 'proxy=' do + + end + + describe 'proxy_auth=' do + + end + + describe 'override_config_with_options' do + + end + end diff --git a/spec/shared_examples/wp_target/wp_registrable.rb b/spec/shared_examples/wp_target/wp_registrable.rb index 58c2d2fa..bdcb99b7 100644 --- a/spec/shared_examples/wp_target/wp_registrable.rb +++ b/spec/shared_examples/wp_target/wp_registrable.rb @@ -34,12 +34,12 @@ shared_examples 'WpTarget::WpRegistrable' do context 'when multisite' do let(:multisite) { true } - it 'returns false (multisite)' do + it 'returns false' do @stub = { status: 302, headers: { 'Location' => 'wp-login.php?registration=disabled' } } @expected = false end - it 'returns true (multisite)' do + it 'returns true' do @stub = { status: 200, body: %{