Some Browser::Options work

This commit is contained in:
erwanlr
2013-04-10 18:34:50 +02:00
parent b9524499bf
commit 1615c0f84e
8 changed files with 82 additions and 41 deletions

View File

@@ -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"

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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: %{<form id="setupform" method="post" action="wp-signup.php">} }
@expected = true
end
@@ -48,12 +48,12 @@ shared_examples 'WpTarget::WpRegistrable' do
context 'when not multisite' do
let(:multisite) { false }
it 'returns false (not multisite)' do
it 'returns false' do
@stub = { status: 302, headers: { 'Location' => 'wp-login.php?registration=disabled' } }
@expected = false
end
it 'returns true (not multisite)' do
it 'returns true' do
@stub = { status: 200, body: %{<form name="registerform" id="registerform" action="wp-login.php"} }
@expected = true
end

View File

@@ -1,8 +1,5 @@
# encoding: UTF-8
# https://github.com/bblimke/webmock
# https://github.com/colszowka/simplecov
require 'webmock/rspec'
# Code Coverage (only works with ruby >= 1.9)
require 'simplecov' if RUBY_VERSION >= '1.9'
@@ -11,12 +8,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../lib/common/common_helper'
SPEC_DIR = ROOT_DIR + '/spec'
SPEC_LIB_DIR = SPEC_DIR + '/lib'
SPEC_CACHE_DIR = SPEC_DIR + '/cache'
SPEC_CACHE_DIR = SPEC_DIR + '/cache' # FIXME remove it
SPEC_FIXTURES_DIR = SPEC_DIR + '/samples'
SHARED_EXAMPLES_DIR = SPEC_DIR + '/shared_examples'
SPEC_FIXTURES_CONF_DIR = SPEC_FIXTURES_DIR + '/conf'
SPEC_FIXTURES_CONF_DIR = SPEC_FIXTURES_DIR + '/conf' # FIXME Remove it
SPEC_FIXTURES_WP_VERSIONS_DIR = SPEC_FIXTURES_DIR + '/wp_versions'
redefine_constant(:CACHE_DIR, SPEC_DIR + '/cache')
redefine_constant(:CONF_DIR, SPEC_FIXTURES_DIR + '/conf/browser') # FIXME Remove the /browser
MODELS_FIXTURES = SPEC_FIXTURES_DIR + '/common/models'
COLLECTIONS_FIXTURES = SPEC_FIXTURES_DIR + '/common/collections'