WpItem::Existable specs

This commit is contained in:
erwanlr
2013-03-22 21:53:20 +01:00
parent e058b204f4
commit 6f49584546
4 changed files with 101 additions and 4 deletions

View File

@@ -3,6 +3,14 @@
class WpItem
module Existable
# Check the existence of the WpItem
# If the response is supplied, it's used for the verification
# Otherwise a new request is done
#
# @param [ Hash ] options See exists_from_response?
# @param [ Typhoeus::Response ] response
#
# @return [ Boolean ]
def exists?(options = {}, response = nil)
unless response
response = Browser.instance.get(url)
@@ -12,15 +20,18 @@ class WpItem
protected
# options:
# :error_404_hash
# :homepage_hash
# :exclude_content REGEXP
# @param [ Typhoeus::Response ] response
# @param [ options ] options
#
# @option options [ Hash ] :error_404_hash The hash of the error 404 page
# @option options [ Hash ] :homepage_hash The hash of the homepage
# @option options [ Hash ] :exclude_content A regexp with the pattern to exclude from the body of the response
#
# @return [ Boolean ]
def exists_from_response?(response, options = {})
# FIXME : The response is supposed to follow locations, so we should not have 301 or 302.
# However, due to an issue with Typhoeus or Webmock, the location is not followed in specs
# See https://github.com/typhoeus/typhoeus/issues/279
if [200, 301, 302, 401, 403].include?(response.code)
if response.has_valid_hash?(options[:error_404_hash], options[:homepage_hash])
if options[:exclude_content]

View File

@@ -3,6 +3,8 @@
require 'spec_helper'
describe WpItem do
it_behaves_like 'WpItem::Existable'
subject(:wp_item) { WpItem.new(uri, options) }
let(:uri) { URI.parse('http://example.com') }
let(:options) { {} }

View File

@@ -0,0 +1,80 @@
# encoding: UTF-8
shared_examples 'WpItem::Existable' do
describe '#exists?' do
context 'when the response is supplied' do
let(:response) { Typhoeus::Response.new }
it 'does not create a request' do
Browser.instance.should_not_receive(:get)
subject.stub(:exists_from_response?).and_return(true)
subject.exists?({}, response).should be_true
end
end
context 'when the response is not supplied' do
it 'creates a request' do
Browser.instance.should_receive(:get)
subject.stub(:exists_from_response?).and_return(false)
subject.exists?.should be_false
end
end
end
describe '#exists_from_response?' do
let(:exists_options) { {} }
let(:body) { 'hello world!' }
after do
response = Typhoeus::Response.new(@resp_opt)
subject.send(:exists_from_response?, response, exists_options).should == @expected
end
context 'when invalid response.code' do
it 'returns false' do
@resp_opt = { code: 500 }
@expected = false
end
end
context 'when the body hash = homepage_hash or error_404_hash' do
let(:exists_options) { { homepage_hash: Digest::MD5.hexdigest(body) } }
it 'returns false' do
@resp_opt = { code: 200, body: body }
@expected = false
end
end
context 'w/o exclude_content' do
[200, 301, 302, 401, 403].each do |code|
it "returns true on #{code}" do
@resp_opt = { code: code, body: '' }
@expected = true
end
end
end
context 'with exclude_content' do
let(:exists_options) { { exclude_content: %r{world!} } }
context 'when the body match' do
it 'returns false' do
@resp_opt = { code: 200, body: body }
@expected = false
end
end
context 'when the body does not match' do
it 'returns true' do
@resp_opt = { code: 200, body: 'hello dude!' }
@expected = true
end
end
end
end
end

View File

@@ -30,11 +30,15 @@ SPEC_DIR = ROOT_DIR + '/spec'
SPEC_LIB_DIR = SPEC_DIR + '/lib'
SPEC_CACHE_DIR = SPEC_DIR + '/cache'
SPEC_FIXTURES_DIR = SPEC_DIR + '/samples'
SHARED_EXAMPLES_DIR = SPEC_DIR + '/shared_examples'
SPEC_FIXTURES_CONF_DIR = SPEC_FIXTURES_DIR + '/conf'
SPEC_FIXTURES_WP_VERSIONS_DIR = SPEC_FIXTURES_DIR + '/wp_versions'
MODELS_FIXTURES = SPEC_FIXTURES_DIR + '/common/models'
# Load all the shared examples
require_files_from_directory(SHARED_EXAMPLES_DIR)
def count_files_in_dir(absolute_dir_path, files_pattern = '*')
Dir.glob(File.join(absolute_dir_path, files_pattern)).count
end