HttpPost job now follows redirects

This commit is contained in:
danielvincent 2010-12-04 15:23:54 -08:00
parent 177cc16e2f
commit d1ad2efed1
3 changed files with 16 additions and 7 deletions

View file

@ -3,11 +3,20 @@ module Jobs
@queue = :http @queue = :http
def self.perform(url, body, tries_remaining) def self.perform(url, body, tries_remaining)
begin begin
RestClient.post(url, :xml => body) RestClient.post(url, :xml => body){ |response, request, result, &block|
if [301, 302, 307].include? response.code
response.follow_redirection(request, result, &block)
else
response.return!(request, result, &block)
end
}
rescue Exception => e rescue Exception => e
Resque.enqueue(self, url, body, tries_remaining -1) unless tries_remaining <= 1 unless tries_remaining <= 1
Resque.enqueue(self, url, body, tries_remaining -1)
else
raise e raise e
end end
end end
end end
end end
end

View file

@ -6,11 +6,11 @@ describe Jobs::HttpPost do
@body = '<xml>California</xml>' @body = '<xml>California</xml>'
end end
it 'POSTs to a given URL' do it 'POSTs to a given URL' do
RestClient.should_receive(:post).with(@url, @body).and_return(true) RestClient.should_receive(:post).with(@url, {:xml=>@body}).and_return(true)
Jobs::HttpPost.perform(@url, @body, 3) Jobs::HttpPost.perform(@url, @body, 3)
end end
it 'retries' do it 'retries' do
RestClient.should_receive(:post).with(@url, @body).and_raise(SocketError) RestClient.should_receive(:post).with(@url, {:xml=>@body}).and_raise(SocketError)
Resque.should_receive(:enqueue).with(Jobs::HttpPost, @url, @body, 1).once Resque.should_receive(:enqueue).with(Jobs::HttpPost, @url, @body, 1).once
Jobs::HttpPost.perform(@url, @body, 2) Jobs::HttpPost.perform(@url, @body, 2)
end end

View file

@ -30,8 +30,8 @@ RSpec.configure do |config|
config.before(:each) do config.before(:each) do
I18n.locale = :en I18n.locale = :en
EventMachine::HttpRequest.stub!(:new).and_return(FakeHttpRequest.new(:success)) EventMachine::HttpRequest.stub!(:new).and_return(FakeHttpRequest.new(:success))
EventMachine::HttpRequest.any_instance.stubs(:post) RestClient.stub!(:post).and_return(FakeHttpRequest.new(:success))
EventMachine::HttpRequest.any_instance.stubs(:get)
DatabaseCleaner.clean DatabaseCleaner.clean
UserFixer.load_user_fixtures UserFixer.load_user_fixtures
Notifier.stub!(:send_request_accepted!).and_return(true) Notifier.stub!(:send_request_accepted!).and_return(true)