-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make HttpStatusAgent provide redirect info #1590
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,9 @@ def f.get url | |
|
||
def f.set url, response, time = nil | ||
sleep(time/1000) if time | ||
def response.to_hash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oy, this is getting ugly. Not your addition, particularly, but this whole mocking code. The reason to_hash wasn't working is because the mocked responses are built with It'd be nice if we could at least make a mock response class near the top, something like: class MockResponse < Struct.new(:status, :headers)
alias_method :to_h, :to_hash
end then replace instances of If you don't want to dive into refactoring this spec, I'm happy to merge your change and then work on it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cantino implemented In principle I'm happy to try my hand at refactoring this spec, but I suspect I'm not experienced enough at Ruby to do a good job. So you're probably better off either giving me some pointers, or just doing it yourself :) Either way is OK by me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think going with the new Struct.new(:status, :headers, :to_hash)
.new(200, {}, { url: url }) Then you can test the url coming out. That's all Given what I've learned about how tests are normally written on huginn agents, I can throw my hat into possible devs to refactor. I think the better approach at this point would be to use webmock to fake the web response instead of faking the result with a fake object. Then the tests would have everything usually available when making a web call. It's more complicated, but it leads to simpler tests in this situation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @darrencauthon that's fine too, although I might define a method to return the struct instead of repeating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's cool. I won't rush but it'll be on my list. |
||
to_h | ||
end | ||
programmed_responses[url] = response | ||
end | ||
end | ||
|
@@ -109,7 +112,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, Struct.new(:status, :headers, :url).new(status_code, {}, successful_url)) | ||
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } } | ||
end | ||
|
||
|
@@ -208,10 +211,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, Struct.new(:status, :headers, :url).new(0, {}, successful_url)) | ||
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } } | ||
end | ||
|
||
|
@@ -241,7 +254,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, Struct.new(:status, :headers, :url).new(-1, {}, successful_url)) | ||
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } } | ||
end | ||
|
||
|
@@ -302,7 +315,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, Struct.new(:status, :headers, :url).new(status_code, {header => header_value}, successful_url)) | ||
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: header } } | ||
end | ||
|
||
|
@@ -318,7 +331,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, Struct.new(:status, :headers, :url).new(status_code, {header => header_value}, successful_url)) | ||
Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: header + "," + nonexistant_header } } | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@strugee Just looking at the code... perhaps
measured_result.url
would work here?The
measured_result
is a wrapper around the result, with amethod_missing
property to pass any method calls back to the original result. (https://github.com/cantino/huginn/blob/29007c9019e35220964b16fb578c44c6effd0763/lib/time_tracker.rb#L15)The other difference is that the tests have fake objects that may not have
to_hash
defined. Ifto_hash
is a method on the object in production, you could appendto_hash
to the fake and bingo it should be fine.Sorry, this is a point where faking/mocking can be a pain. And I say this as the one who wrote the original tests. 😄 I thought the tests would be easier, since faking a bunch of different types of responses could be difficult or add more dependencies to Huginn. But now the objects getting faked are more and more complicated. This "given and take" decision is now firmly in "take" mode. My apologies on that. 😭
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
measured_result.result.url
doesn't work anywhere.measured_result.url
works in RSpec, weirdly, but not in actual Rails. ????In any case, I ended up just mocking
to_hash
(it's basically an alias toto_h
). So specs pass now! \o/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have said
measured_result.result.env.url.to_s
. Theurl
here is a URI object, andurl
is a string, sourl != final_url
which is set toredirected
always evaluates to true. The specs are not actually testing anything becauseagent.faraday
is mocked to return what they want.