diff --git a/lib/common/models/wp_timthumb/versionable.rb b/lib/common/models/wp_timthumb/versionable.rb index b53f2227..95f5a9b4 100755 --- a/lib/common/models/wp_timthumb/versionable.rb +++ b/lib/common/models/wp_timthumb/versionable.rb @@ -5,11 +5,17 @@ class WpTimthumb < WpItem # Get the version from the body of an invalid request # See https://code.google.com/p/timthumb/source/browse/trunk/timthumb.php#426 + # + # @return [ String ] The version def version - response = Browser.instance.get(url) - response.body[%r{TimThumb version\s*: ([^<]+)} , 1] + unless @version + response = Browser.instance.get(url) + @version = response.body[%r{TimThumb version\s*: ([^<]+)} , 1] + end + @version end + # @return [ String ] def to_s "#{url}#{ ' v' + version if version}" end diff --git a/spec/lib/common/models/wp_timthumb_spec.rb b/spec/lib/common/models/wp_timthumb_spec.rb index 57d3e3e9..534cb310 100644 --- a/spec/lib/common/models/wp_timthumb_spec.rb +++ b/spec/lib/common/models/wp_timthumb_spec.rb @@ -4,9 +4,10 @@ require 'spec_helper' describe WpTimthumb do it_behaves_like 'WpTimthumb::Existable' + it_behaves_like 'WpTimthumb::Versionable' subject(:wp_timthumb) { WpTimthumb.new(uri, options) } let(:uri) { URI.parse('http://example.com/') } - let(:options) { {} } + let(:options) { { path: 'path-to/a/timtuhumb.php' } } end diff --git a/spec/shared_examples/wp_timthumb_versionable.rb b/spec/shared_examples/wp_timthumb_versionable.rb new file mode 100644 index 00000000..455f5541 --- /dev/null +++ b/spec/shared_examples/wp_timthumb_versionable.rb @@ -0,0 +1,52 @@ +# encoding: UTF-8 + +shared_examples 'WpTimthumb::Versionable' do + + describe '#version' do + after do + stub_request(:get, subject.url).to_return(status: 200, body: @body) + + subject.version.should === @expected + end + + context 'when a version is already sets' do + it 'returns it' do + subject.version = '2.3.1' + @expected = '2.3.1' + end + end + + context 'when the body match' do + it 'returns the version' do + @body = 'Query String :
TimThumb version : 2.8.10' + @expected = '2.8.10' + end + end + + context 'otherwise' do + it 'returns nil' do + @body = 'not in here' + @expected = nil + end + end + end + + describe '#to_s' do + after { subject.to_s.should == @expected } + + context 'when there is a version' do + it 'returns it with the url' do + subject.version = '1.3' + @expected = uri.merge(options[:path]).to_s + ' v1.3' + end + end + + context 'when there is not a version' do + it 'returns only the url' do + subject.stub(:version).and_return(nil) + @expected = uri.merge(options[:path]).to_s + end + end + end + +end