WpTimthumb::Versionable specs

This commit is contained in:
erwanlr
2013-03-26 17:12:57 +01:00
parent 65ca256a73
commit be0aad2c66
3 changed files with 62 additions and 3 deletions

View File

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

View File

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

View File

@@ -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 :<br />TimThumb version : 2.8.10</pre>'
@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