From 22d83ec5023cfc25dc38d61ddfd0a73a5e501c8e Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Fri, 25 Feb 2011 14:46:21 -0800 Subject: [PATCH] update http_multi to adjust urls when you get a redirect --- app/models/jobs/http_multi.rb | 8 +++++++- spec/models/jobs/http_multi_spec.rb | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/jobs/http_multi.rb b/app/models/jobs/http_multi.rb index c6f530382..26359d70e 100644 --- a/app/models/jobs/http_multi.rb +++ b/app/models/jobs/http_multi.rb @@ -3,7 +3,7 @@ module Job @queue = :http MAX_RETRIES = 3 - OPTS = {:max_redirects => 3, :follow_location => true, :timeout => 5000, :method => :post} + OPTS = {:max_redirects => 3, :timeout => 5000, :method => :post} def self.perform_delegate(user_id, enc_object_xml, person_ids, retry_count=0) user = User.find(user_id) @@ -22,6 +22,12 @@ module Job request = Typhoeus::Request.new(url, OPTS.merge(:params => {:xml => CGI::escape(xml)})) request.on_complete do |response| + if response.code >= 300 && response.code < 400 + if response.headers_hash['Location'] == response.request.url.sub('http://', 'https://') + person.url = response.headers_hash['Location'] + person.save + end + end unless response.success? Rails.logger.info("event=http_multi_fail sender_id=#{user_id} recipient_id=#{person.id} url=#{response.effective_url} response_code='#{response.code}' xml='#{Base64.decode64(enc_object_xml)}'") failed_request_people << person.id diff --git a/spec/models/jobs/http_multi_spec.rb b/spec/models/jobs/http_multi_spec.rb index 1e2a2426d..509c9a800 100644 --- a/spec/models/jobs/http_multi_spec.rb +++ b/spec/models/jobs/http_multi_spec.rb @@ -60,4 +60,18 @@ describe Job::HttpMulti do Job::HttpMulti.perform(bob.id, @post_xml, [person.id]) end + + it 'updates http users who have moved to https' do + person = @people.first + person.url = 'http://remote.net/' + person.save + response = Typhoeus::Response.new(:code => 301, :headers_hash => {"Location" => person.receive_url.sub('http://', 'https://')}, :body => "", :time => 0.2) + @hydra.stub(:post, person.receive_url).and_return(response) + + Typhoeus::Hydra.stub!(:new).and_return(@hydra) + + Job::HttpMulti.perform(bob.id, @post_xml, [person.id]) + person.reload + person.url.should =~ /https:\/\/remote.net\// + end end