From 6f49584546ad344265fa51560a62e15d360ab391 Mon Sep 17 00:00:00 2001 From: erwanlr Date: Fri, 22 Mar 2013 21:53:20 +0100 Subject: [PATCH] WpItem::Existable specs --- lib/common/models/wp_item/existable.rb | 19 ++++-- spec/lib/common/models/wp_item_spec.rb | 2 + spec/shared_examples/wp_item_existable.rb | 80 +++++++++++++++++++++++ spec/spec_helper.rb | 4 ++ 4 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 spec/shared_examples/wp_item_existable.rb diff --git a/lib/common/models/wp_item/existable.rb b/lib/common/models/wp_item/existable.rb index 14cd7117..0daa45d0 100755 --- a/lib/common/models/wp_item/existable.rb +++ b/lib/common/models/wp_item/existable.rb @@ -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] diff --git a/spec/lib/common/models/wp_item_spec.rb b/spec/lib/common/models/wp_item_spec.rb index 23d468b0..c3e844df 100644 --- a/spec/lib/common/models/wp_item_spec.rb +++ b/spec/lib/common/models/wp_item_spec.rb @@ -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) { {} } diff --git a/spec/shared_examples/wp_item_existable.rb b/spec/shared_examples/wp_item_existable.rb new file mode 100644 index 00000000..63b3d3e8 --- /dev/null +++ b/spec/shared_examples/wp_item_existable.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1029c9e9..60786bae 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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