Adding error handling and more test cases

This commit is contained in:
Eugen Rochko 2016-02-17 16:18:46 +01:00
parent fd04a39022
commit 102ac2c99f
4 changed files with 57 additions and 10 deletions

View File

@ -1,7 +1,7 @@
PATH
remote: .
specs:
goldfinger (0.0.0)
goldfinger (0.1.0)
addressable (~> 2.4)
http (~> 1.0)
nokogiri (~> 1.6)

View File

@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'goldfinger'
s.version = '0.0.0'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.required_ruby_version = '>= 2.0.0'
s.date = '2016-02-17'

View File

@ -10,9 +10,23 @@ module Goldfinger
end
def finger
_, template = perform_get(url)
ssl = true
begin
_, template = perform_get(url(ssl))
rescue HTTP::Error
if ssl
ssl = false
retry
else
raise Goldfinger::Error::NotFound
end
end
headers, body = perform_get(url_from_template(template))
Goldfinger::Result.new(headers, body)
rescue HTTP::Error
raise Goldfinger::Error::NotFound
end
private

View File

@ -1,14 +1,47 @@
describe Goldfinger::Client do
describe '#finger' do
before do
stub_request(:get, 'https://quitter.no/.well-known/host-meta').to_return(body: fixture('quitter.no_.well-known_host-meta'))
stub_request(:get, 'https://quitter.no/.well-known/webfinger?resource=acct:gargron@quitter.no').to_return(body: fixture('quitter.no_.well-known_webfinger.json'))
context 'with HTTPS available' do
describe '#finger' do
before do
stub_request(:get, 'https://quitter.no/.well-known/host-meta').to_return(body: fixture('quitter.no_.well-known_host-meta'))
stub_request(:get, 'https://quitter.no/.well-known/webfinger?resource=acct:gargron@quitter.no').to_return(body: fixture('quitter.no_.well-known_webfinger.json'))
end
subject { Goldfinger::Client.new('acct:gargron@quitter.no') }
it 'returns a result' do
expect(subject.finger).to be_instance_of Goldfinger::Result
end
end
end
subject { Goldfinger::Client.new('acct:gargron@quitter.no') }
context 'with only HTTP available' do
describe '#finger' do
before do
stub_request(:get, 'https://quitter.no/.well-known/host-meta').to_raise(HTTP::Error)
stub_request(:get, 'http://quitter.no/.well-known/host-meta').to_return(body: fixture('quitter.no_.well-known_host-meta'))
stub_request(:get, 'https://quitter.no/.well-known/webfinger?resource=acct:gargron@quitter.no').to_return(body: fixture('quitter.no_.well-known_webfinger.json'))
end
it 'returns a result' do
expect(subject.finger).to be_instance_of Goldfinger::Result
subject { Goldfinger::Client.new('acct:gargron@quitter.no') }
it 'returns a result' do
expect(subject.finger).to be_instance_of Goldfinger::Result
end
end
end
context 'with XRD missing' do
describe '#finger' do
before do
stub_request(:get, 'https://quitter.no/.well-known/host-meta').to_raise(HTTP::Error)
stub_request(:get, 'http://quitter.no/.well-known/host-meta').to_raise(HTTP::Error)
end
subject { Goldfinger::Client.new('acct:gargron@quitter.no') }
it 'raises an error' do
expect {subject.finger }.to raise_error(Goldfinger::Error::NotFound)
end
end
end
end