diff --git a/lib/common/models/wp_timthumb.rb b/lib/common/models/wp_timthumb.rb index fa31a8ec..6261b94a 100755 --- a/lib/common/models/wp_timthumb.rb +++ b/lib/common/models/wp_timthumb.rb @@ -8,5 +8,4 @@ class WpTimthumb < WpItem include WpTimthumb::Versionable include WpTimthumb::Existable include WpTimthumb::Output - end diff --git a/lib/common/models/wp_timthumb/existable.rb b/lib/common/models/wp_timthumb/existable.rb index 7b022b11..c7f7369e 100644 --- a/lib/common/models/wp_timthumb/existable.rb +++ b/lib/common/models/wp_timthumb/existable.rb @@ -3,6 +3,10 @@ class WpTimthumb < WpItem module Existable + # @param [ Typhoeus::Response ] response + # @param [ Hash ] options + # + # @return [ Boolean ] def exists_from_response?(response, options = {}) response.code == 400 && response.body =~ /no image specified/i ? true : false end diff --git a/spec/lib/common/models/wp_timthumb_spec.rb b/spec/lib/common/models/wp_timthumb_spec.rb new file mode 100644 index 00000000..57d3e3e9 --- /dev/null +++ b/spec/lib/common/models/wp_timthumb_spec.rb @@ -0,0 +1,12 @@ +# encoding: UTF-8 + +require 'spec_helper' + +describe WpTimthumb do + it_behaves_like 'WpTimthumb::Existable' + + subject(:wp_timthumb) { WpTimthumb.new(uri, options) } + let(:uri) { URI.parse('http://example.com/') } + let(:options) { {} } + +end diff --git a/spec/shared_examples/wp_timthumb_existable.rb b/spec/shared_examples/wp_timthumb_existable.rb new file mode 100644 index 00000000..58b91dfc --- /dev/null +++ b/spec/shared_examples/wp_timthumb_existable.rb @@ -0,0 +1,37 @@ +# encoding: UTF-8 + +shared_examples 'WpTimthumb::Existable' do + + describe 'exists_from_response?' do + after do + response = Typhoeus::Response.new(@resp_opt) + subject.send(:exists_from_response?, response).should == @expected + end + + context 'when the status is not a 400' do + it 'returns false' do + @resp_opt = { code: 200 } + @expected = false + end + end + + context 'when the status is a 400' do + let(:opt) { { code: 400 } } + + context 'when the body contains "no image specified"' do + it 'returns true' do + @resp_opt = opt.merge(body: 'The following error(s) occured:
No image specified') + @expected = true + end + end + + context 'otherwise' do + it 'returns false' do + @resp_opt = opt.merge(body: 'im a fake one, hehe') + @expected = false + end + end + end + end + +end