diff --git a/app/models/jobs/http_post.rb b/app/models/jobs/http_post.rb index 7aa48c894..b96d3aa9e 100644 --- a/app/models/jobs/http_post.rb +++ b/app/models/jobs/http_post.rb @@ -3,10 +3,19 @@ module Jobs @queue = :http def self.perform(url, body, tries_remaining) 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 - Resque.enqueue(self, url, body, tries_remaining -1) unless tries_remaining <= 1 - raise e + unless tries_remaining <= 1 + Resque.enqueue(self, url, body, tries_remaining -1) + else + raise e + end end end end diff --git a/spec/models/jobs/http_post_spec.rb b/spec/models/jobs/http_post_spec.rb index 633228d67..18461996b 100644 --- a/spec/models/jobs/http_post_spec.rb +++ b/spec/models/jobs/http_post_spec.rb @@ -6,11 +6,11 @@ describe Jobs::HttpPost do @body = 'California' end 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) end 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 Jobs::HttpPost.perform(@url, @body, 2) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cef376b5c..e67237c33 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -30,8 +30,8 @@ RSpec.configure do |config| config.before(:each) do I18n.locale = :en EventMachine::HttpRequest.stub!(:new).and_return(FakeHttpRequest.new(:success)) - EventMachine::HttpRequest.any_instance.stubs(:post) - EventMachine::HttpRequest.any_instance.stubs(:get) + RestClient.stub!(:post).and_return(FakeHttpRequest.new(:success)) + DatabaseCleaner.clean UserFixer.load_user_fixtures Notifier.stub!(:send_request_accepted!).and_return(true)