Faraday follows redirects

This commit is contained in:
Raphael Sofaer 2011-06-22 12:53:18 -07:00
parent 58ba4db3c8
commit 04b0705733
5 changed files with 30 additions and 7 deletions

View file

@ -21,6 +21,10 @@ gem 'devise_invitable', '0.5.0'
gem 'omniauth', '0.2.6'
gem 'twitter', '1.5.0'
#Web
gem 'faraday'
gem 'faraday-stack'
#Views
gem 'haml', '3.0.25'
gem 'will_paginate', '3.0.pre2'

View file

@ -154,6 +154,8 @@ GEM
addressable (~> 2.2.4)
multipart-post (~> 1.1.0)
rack (>= 1.1.0, < 2)
faraday-stack (0.1.2)
faraday (~> 0.6)
faraday_middleware (0.6.3)
faraday (~> 0.6.0)
fastercsv (1.5.4)
@ -415,6 +417,8 @@ DEPENDENCIES
em-websocket!
excon (= 0.2.4)
factory_girl_rails
faraday
faraday-stack
fastercsv (= 1.5.4)
fixture_builder (~> 0.2.0)
fog (= 0.3.25)

View file

@ -2,4 +2,7 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
Faraday.default_connection = Faraday::Connection.new( :ssl => {:ca_file => AppConfig[:ca_file]} )
Faraday.default_connection = Faraday::Connection.new( :ssl => {:ca_file => AppConfig[:ca_file]}, :timeout => 5 ) do |b|
b.use FaradayStack::FollowRedirects
b.adapter Faraday.default_adapter
end

View file

@ -3,9 +3,6 @@ require File.join(Rails.root, 'lib/webfinger_profile')
class Webfinger
class WebfingerFailedError < RuntimeError; end
TIMEOUT = 5
REDIRECTS = 3
OPTS = {:timeout => TIMEOUT, :redirects => REDIRECTS}
def initialize(account)
@account = account.strip.gsub('acct:','').to_s
@ssl = true
@ -47,7 +44,7 @@ class Webfinger
private
def get_xrd
begin
http = Faraday.get xrd_url, OPTS
http = Faraday.get xrd_url
profile_url = webfinger_profile_url(http.body)
if profile_url
@ -69,7 +66,7 @@ class Webfinger
def get_webfinger_profile(profile_url)
begin
http = Faraday.get(profile_url, OPTS)
http = Faraday.get(profile_url)
rescue
raise I18n.t('webfinger.fetch_failed', :profile_url => profile_url)
@ -83,7 +80,7 @@ class Webfinger
@wf_profile = WebfingerProfile.new(@account, webfinger_profile)
begin
hcard = Faraday.get(@wf_profile.hcard, OPTS)
hcard = Faraday.get(@wf_profile.hcard)
rescue
return I18n.t('webfinger.hcard_fetch_failed', :account => @account)
end

View file

@ -67,6 +67,21 @@ describe Webfinger do
end
end
describe '#get_xrd' do
it 'follows redirects' do
puts "Now in spec."
redirect_url = "http://whereami.whatisthis/host-meta"
stub_request(:get, "https://tom.joindiaspora.com/.well-known/host-meta").
to_return(:status => 302, :headers => { 'Location' => redirect_url })
stub_request(:get, redirect_url).
to_return(:status => 200, :body => diaspora_xrd)
begin
finger.send :get_xrd
rescue; end
a_request(:get, redirect_url).should have_been_made
end
end
context 'webfingering local people' do
it 'should return a person from the database if it matches its handle' do