Skip to content

Commit

Permalink
Make HttpStatusAgent provide redirect info (#1590)
Browse files Browse the repository at this point in the history
* Make HttpStatusAgent provide redirect info

* Mock to_hash for HttpStatusAgent specs

* Refactor HttpStatusAgent spec mock responses
  • Loading branch information
strugee authored and cantino committed Jul 20, 2016
1 parent 29007c9 commit c109533
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/models/agents/http_status_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def check_this_url(url, local_headers)

# Deal with failures
if measured_result.result
payload.merge!({ 'response_received' => true, 'status' => current_status })
final_url = boolify(interpolated['disable_redirect_follow']) ? url : measured_result.result.to_hash[:url]
payload.merge!({ 'final_url' => final_url, 'redirected' => (url != final_url), 'response_received' => true, 'status' => current_status })
# Deal with headers
if local_headers.present?
header_results = measured_result.result.headers.select {|header, value| local_headers.include?(header)}
Expand Down
24 changes: 19 additions & 5 deletions spec/controllers/http_status_agent_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require 'rails_helper'

class MockResponse < Struct.new(:status, :headers, :url)
alias_method :to_hash, :to_h
end

describe 'HttpStatusAgent' do

let(:agent) do
Expand Down Expand Up @@ -109,7 +113,7 @@ def agent.checked_url
let(:header_value) { SecureRandom.uuid }

let(:event_with_a_successful_ping) do
agent.faraday.set(successful_url, Struct.new(:status, :headers).new(status_code, {}))
agent.faraday.set(successful_url, MockResponse.new(status_code, {}, successful_url))
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } }
end

Expand Down Expand Up @@ -208,10 +212,20 @@ def agent.checked_url
expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
end

it "should return the final url" do
agent.receive events
expect(agent.the_created_events[0][:payload]['final_url']).to eq(successful_url)
end

it "should return whether the url redirected" do
agent.receive events
expect(agent.the_created_events[0][:payload]['redirected']).to eq(false)
end

describe "but the ping returns a status code of 0" do

let(:event_with_a_successful_ping) do
agent.faraday.set(successful_url, Struct.new(:status, :headers).new(0, {}))
agent.faraday.set(successful_url, MockResponse.new(0, {}, successful_url))
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } }
end

Expand Down Expand Up @@ -241,7 +255,7 @@ def agent.checked_url
describe "but the ping returns a status code of -1" do

let(:event_with_a_successful_ping) do
agent.faraday.set(successful_url, Struct.new(:status, :headers).new(-1, {}))
agent.faraday.set(successful_url, MockResponse.new(-1, {}, successful_url))
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } }
end

Expand Down Expand Up @@ -302,7 +316,7 @@ def agent.checked_url

describe "with a header specified" do
let(:event_with_a_successful_ping) do
agent.faraday.set(successful_url, Struct.new(:status, :headers).new(status_code, {header => header_value}))
agent.faraday.set(successful_url, MockResponse.new(status_code, {header => header_value}, successful_url))
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: header } }
end

Expand All @@ -318,7 +332,7 @@ def agent.checked_url
let(:nonexistant_header) { SecureRandom.uuid }

let(:event_with_a_successful_ping) do
agent.faraday.set(successful_url, Struct.new(:status, :headers).new(status_code, {header => header_value}))
agent.faraday.set(successful_url, MockResponse.new(status_code, {header => header_value}, successful_url))
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: header + "," + nonexistant_header } }
end

Expand Down

0 comments on commit c109533

Please sign in to comment.