@@ -110,7 +110,9 @@ module WebRequestConcern |
||
| 110 | 110 |
|
| 111 | 111 |
builder.headers[:user_agent] = user_agent |
| 112 | 112 |
|
| 113 |
- builder.use FaradayMiddleware::FollowRedirects |
|
| 113 |
+ unless boolify(interpolated['disable_redirect_follow']) |
|
| 114 |
+ builder.use FaradayMiddleware::FollowRedirects |
|
| 115 |
+ end |
|
| 114 | 116 |
builder.request :url_encoded |
| 115 | 117 |
|
| 116 | 118 |
if boolify(interpolated['disable_url_encoding']) |
@@ -0,0 +1,81 @@ |
||
| 1 |
+module Agents |
|
| 2 |
+ |
|
| 3 |
+ class HttpStatusAgent < Agent |
|
| 4 |
+ |
|
| 5 |
+ include WebRequestConcern |
|
| 6 |
+ include FormConfigurable |
|
| 7 |
+ |
|
| 8 |
+ can_dry_run! |
|
| 9 |
+ can_order_created_events! |
|
| 10 |
+ |
|
| 11 |
+ default_schedule "every_12h" |
|
| 12 |
+ |
|
| 13 |
+ form_configurable :url |
|
| 14 |
+ form_configurable :disable_redirect_follow, type: :array, values: ['true', 'false'] |
|
| 15 |
+ |
|
| 16 |
+ description <<-MD |
|
| 17 |
+ The HttpStatusAgent will check a url and emit the resulting HTTP status code. |
|
| 18 |
+ |
|
| 19 |
+ Specify a `Url` and the Http Status Agent will produce an event with the http status code. |
|
| 20 |
+ |
|
| 21 |
+ The `disable redirect follow` option causes the Agent to not follow HTTP redirects. For example, setting this to `true` will cause an agent that receives a 301 redirect to `http://yahoo.com` to return a status of 301 instead of following the redirect and returning 200. |
|
| 22 |
+ MD |
|
| 23 |
+ |
|
| 24 |
+ event_description <<-MD |
|
| 25 |
+ Events will have the following fields: |
|
| 26 |
+ |
|
| 27 |
+ {
|
|
| 28 |
+ "url": "...", |
|
| 29 |
+ "status": "..." |
|
| 30 |
+ } |
|
| 31 |
+ MD |
|
| 32 |
+ |
|
| 33 |
+ def working? |
|
| 34 |
+ memory['last_status'].to_i > 0 |
|
| 35 |
+ end |
|
| 36 |
+ |
|
| 37 |
+ def default_options |
|
| 38 |
+ {
|
|
| 39 |
+ 'url' => "http://google.com", |
|
| 40 |
+ 'disable_redirect_follow' => "true", |
|
| 41 |
+ } |
|
| 42 |
+ end |
|
| 43 |
+ |
|
| 44 |
+ def validate_options |
|
| 45 |
+ errors.add(:base, "a url must be specified") unless options['url'].present? |
|
| 46 |
+ end |
|
| 47 |
+ |
|
| 48 |
+ def check |
|
| 49 |
+ check_this_url interpolated[:url] |
|
| 50 |
+ end |
|
| 51 |
+ |
|
| 52 |
+ def receive(incoming_events) |
|
| 53 |
+ incoming_events.each do |event| |
|
| 54 |
+ interpolate_with(event) do |
|
| 55 |
+ check_this_url interpolated[:url] |
|
| 56 |
+ end |
|
| 57 |
+ end |
|
| 58 |
+ end |
|
| 59 |
+ |
|
| 60 |
+ private |
|
| 61 |
+ |
|
| 62 |
+ def check_this_url(url) |
|
| 63 |
+ if result = ping(url) |
|
| 64 |
+ create_event payload: { 'url' => url, 'status' => result.status.to_s, 'response_received' => true }
|
|
| 65 |
+ memory['last_status'] = result.status.to_s |
|
| 66 |
+ else |
|
| 67 |
+ create_event payload: { 'url' => url, 'response_received' => false }
|
|
| 68 |
+ memory['last_status'] = nil |
|
| 69 |
+ end |
|
| 70 |
+ end |
|
| 71 |
+ |
|
| 72 |
+ def ping(url) |
|
| 73 |
+ result = faraday.get url |
|
| 74 |
+ result.status > 0 ? result : nil |
|
| 75 |
+ rescue |
|
| 76 |
+ nil |
|
| 77 |
+ end |
|
| 78 |
+ |
|
| 79 |
+ end |
|
| 80 |
+ |
|
| 81 |
+end |
@@ -0,0 +1,260 @@ |
||
| 1 |
+require 'rails_helper' |
|
| 2 |
+ |
|
| 3 |
+describe 'HttpStatusAgent' do |
|
| 4 |
+ |
|
| 5 |
+ let(:agent) do |
|
| 6 |
+ Agents::HttpStatusAgent.new(:name => SecureRandom.uuid, :options => valid_params).tap do |a| |
|
| 7 |
+ a.service = services(:generic) |
|
| 8 |
+ a.user = users(:jane) |
|
| 9 |
+ a.options['url'] = 'http://google.com' |
|
| 10 |
+ a.save! |
|
| 11 |
+ |
|
| 12 |
+ def a.interpolate_with(e, &block) |
|
| 13 |
+ @the_event = e |
|
| 14 |
+ block.call |
|
| 15 |
+ end |
|
| 16 |
+ |
|
| 17 |
+ def a.interpolated |
|
| 18 |
+ @the_event.payload |
|
| 19 |
+ end |
|
| 20 |
+ |
|
| 21 |
+ def a.create_event event |
|
| 22 |
+ @the_created_events ||= [] |
|
| 23 |
+ @the_created_events << event |
|
| 24 |
+ end |
|
| 25 |
+ |
|
| 26 |
+ def a.the_created_events |
|
| 27 |
+ @the_created_events || [] |
|
| 28 |
+ end |
|
| 29 |
+ |
|
| 30 |
+ def a.faraday |
|
| 31 |
+ @faraday ||= Struct.new(:programmed_responses).new({}).tap do |f|
|
|
| 32 |
+ def f.get url |
|
| 33 |
+ programmed_responses[url] || raise('invalid url')
|
|
| 34 |
+ end |
|
| 35 |
+ |
|
| 36 |
+ def f.set url, response, time = nil |
|
| 37 |
+ sleep(time/1000) if time |
|
| 38 |
+ programmed_responses[url] = response |
|
| 39 |
+ end |
|
| 40 |
+ end |
|
| 41 |
+ end |
|
| 42 |
+ end |
|
| 43 |
+ end |
|
| 44 |
+ |
|
| 45 |
+ let(:valid_params) { {} }
|
|
| 46 |
+ |
|
| 47 |
+ describe "working" do |
|
| 48 |
+ it "should be working when the last status is 200" do |
|
| 49 |
+ agent.memory['last_status'] = '200' |
|
| 50 |
+ expect(agent.working?).to eq(true) |
|
| 51 |
+ end |
|
| 52 |
+ |
|
| 53 |
+ it "should be working when the last status is 304" do |
|
| 54 |
+ agent.memory['last_status'] = '304' |
|
| 55 |
+ expect(agent.working?).to eq(true) |
|
| 56 |
+ end |
|
| 57 |
+ |
|
| 58 |
+ it "should not be working if the status is 0" do |
|
| 59 |
+ agent.memory['last_status'] = '0' |
|
| 60 |
+ expect(agent.working?).to eq(false) |
|
| 61 |
+ end |
|
| 62 |
+ |
|
| 63 |
+ it "should not be working if the status is missing" do |
|
| 64 |
+ agent.memory['last_status'] = nil |
|
| 65 |
+ expect(agent.working?).to eq(false) |
|
| 66 |
+ end |
|
| 67 |
+ |
|
| 68 |
+ it "should not be working if the status is -1" do |
|
| 69 |
+ agent.memory['last_status'] = '-1' |
|
| 70 |
+ expect(agent.working?).to eq(false) |
|
| 71 |
+ end |
|
| 72 |
+ end |
|
| 73 |
+ |
|
| 74 |
+ describe "check" do |
|
| 75 |
+ |
|
| 76 |
+ before do |
|
| 77 |
+ |
|
| 78 |
+ def agent.interpolated |
|
| 79 |
+ @interpolated ||= { :url => SecureRandom.uuid }
|
|
| 80 |
+ end |
|
| 81 |
+ |
|
| 82 |
+ def agent.check_this_url url |
|
| 83 |
+ @url = url |
|
| 84 |
+ end |
|
| 85 |
+ |
|
| 86 |
+ def agent.checked_url |
|
| 87 |
+ @url |
|
| 88 |
+ end |
|
| 89 |
+ |
|
| 90 |
+ end |
|
| 91 |
+ |
|
| 92 |
+ it "should check the url" do |
|
| 93 |
+ agent.check |
|
| 94 |
+ expect(agent.checked_url).to eq(agent.interpolated[:url]) |
|
| 95 |
+ end |
|
| 96 |
+ |
|
| 97 |
+ end |
|
| 98 |
+ |
|
| 99 |
+ describe "receive" do |
|
| 100 |
+ |
|
| 101 |
+ describe "with an event with a successful ping" do |
|
| 102 |
+ |
|
| 103 |
+ let(:successful_url) { SecureRandom.uuid }
|
|
| 104 |
+ |
|
| 105 |
+ let(:status_code) { 200 }
|
|
| 106 |
+ |
|
| 107 |
+ let(:event_with_a_successful_ping) do |
|
| 108 |
+ agent.faraday.set(successful_url, Struct.new(:status).new(status_code)) |
|
| 109 |
+ Event.new.tap { |e| e.payload = { url: successful_url } }
|
|
| 110 |
+ end |
|
| 111 |
+ |
|
| 112 |
+ let(:events) do |
|
| 113 |
+ [event_with_a_successful_ping] |
|
| 114 |
+ end |
|
| 115 |
+ |
|
| 116 |
+ it "should create one event" do |
|
| 117 |
+ agent.receive events |
|
| 118 |
+ expect(agent.the_created_events.count).to eq(1) |
|
| 119 |
+ end |
|
| 120 |
+ |
|
| 121 |
+ it "should note that the successful response succeeded" do |
|
| 122 |
+ agent.receive events |
|
| 123 |
+ expect(agent.the_created_events[0][:payload]['response_received']).to eq(true) |
|
| 124 |
+ end |
|
| 125 |
+ |
|
| 126 |
+ it "should return the status code" do |
|
| 127 |
+ agent.receive events |
|
| 128 |
+ expect(agent.the_created_events[0][:payload]['status']).to eq('200')
|
|
| 129 |
+ end |
|
| 130 |
+ |
|
| 131 |
+ it "should remember the status" do |
|
| 132 |
+ agent.receive events |
|
| 133 |
+ expect(agent.memory['last_status']).to eq('200')
|
|
| 134 |
+ end |
|
| 135 |
+ |
|
| 136 |
+ describe "but the status code is not 200" do |
|
| 137 |
+ let(:status_code) { 500 }
|
|
| 138 |
+ |
|
| 139 |
+ it "should return the status code" do |
|
| 140 |
+ agent.receive events |
|
| 141 |
+ expect(agent.the_created_events[0][:payload]['status']).to eq('500')
|
|
| 142 |
+ end |
|
| 143 |
+ |
|
| 144 |
+ it "should remember the status" do |
|
| 145 |
+ agent.receive events |
|
| 146 |
+ expect(agent.memory['last_status']).to eq('500')
|
|
| 147 |
+ end |
|
| 148 |
+ end |
|
| 149 |
+ |
|
| 150 |
+ it "should return the original url" do |
|
| 151 |
+ agent.receive events |
|
| 152 |
+ expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url) |
|
| 153 |
+ end |
|
| 154 |
+ |
|
| 155 |
+ describe "but the ping returns a status code of 0" do |
|
| 156 |
+ |
|
| 157 |
+ let(:event_with_a_successful_ping) do |
|
| 158 |
+ agent.faraday.set(successful_url, Struct.new(:status).new(0)) |
|
| 159 |
+ Event.new.tap { |e| e.payload = { url: successful_url } }
|
|
| 160 |
+ end |
|
| 161 |
+ |
|
| 162 |
+ it "should create one event" do |
|
| 163 |
+ agent.receive events |
|
| 164 |
+ expect(agent.the_created_events.count).to eq(1) |
|
| 165 |
+ end |
|
| 166 |
+ |
|
| 167 |
+ it "should note that no response was received" do |
|
| 168 |
+ agent.receive events |
|
| 169 |
+ expect(agent.the_created_events[0][:payload]['response_received']).to eq(false) |
|
| 170 |
+ end |
|
| 171 |
+ |
|
| 172 |
+ it "should return the original url" do |
|
| 173 |
+ agent.receive events |
|
| 174 |
+ expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url) |
|
| 175 |
+ end |
|
| 176 |
+ |
|
| 177 |
+ it "should remember no status" do |
|
| 178 |
+ agent.memory['last_status'] = '200' |
|
| 179 |
+ agent.receive events |
|
| 180 |
+ expect(agent.memory['last_status']).to be_nil |
|
| 181 |
+ end |
|
| 182 |
+ |
|
| 183 |
+ end |
|
| 184 |
+ |
|
| 185 |
+ describe "but the ping returns a status code of -1" do |
|
| 186 |
+ |
|
| 187 |
+ let(:event_with_a_successful_ping) do |
|
| 188 |
+ agent.faraday.set(successful_url, Struct.new(:status).new(-1)) |
|
| 189 |
+ Event.new.tap { |e| e.payload = { url: successful_url } }
|
|
| 190 |
+ end |
|
| 191 |
+ |
|
| 192 |
+ it "should create one event" do |
|
| 193 |
+ agent.receive events |
|
| 194 |
+ expect(agent.the_created_events.count).to eq(1) |
|
| 195 |
+ end |
|
| 196 |
+ |
|
| 197 |
+ it "should note that no response was received" do |
|
| 198 |
+ agent.receive events |
|
| 199 |
+ expect(agent.the_created_events[0][:payload]['response_received']).to eq(false) |
|
| 200 |
+ end |
|
| 201 |
+ |
|
| 202 |
+ it "should return the original url" do |
|
| 203 |
+ agent.receive events |
|
| 204 |
+ expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url) |
|
| 205 |
+ end |
|
| 206 |
+ |
|
| 207 |
+ end |
|
| 208 |
+ |
|
| 209 |
+ describe "and with one event with a failing ping" do |
|
| 210 |
+ |
|
| 211 |
+ let(:failing_url) { SecureRandom.uuid }
|
|
| 212 |
+ let(:event_with_a_failing_ping) { Event.new.tap { |e| e.payload = { url: failing_url } } }
|
|
| 213 |
+ |
|
| 214 |
+ let(:events) do |
|
| 215 |
+ [event_with_a_successful_ping, event_with_a_failing_ping] |
|
| 216 |
+ end |
|
| 217 |
+ |
|
| 218 |
+ it "should create two events" do |
|
| 219 |
+ agent.receive events |
|
| 220 |
+ expect(agent.the_created_events.count).to eq(2) |
|
| 221 |
+ end |
|
| 222 |
+ |
|
| 223 |
+ it "should note that the failed response failed" do |
|
| 224 |
+ agent.receive events |
|
| 225 |
+ expect(agent.the_created_events[1][:payload]['response_received']).to eq(false) |
|
| 226 |
+ end |
|
| 227 |
+ |
|
| 228 |
+ it "should note that the successful response succeeded" do |
|
| 229 |
+ agent.receive events |
|
| 230 |
+ expect(agent.the_created_events[0][:payload]['response_received']).to eq(true) |
|
| 231 |
+ end |
|
| 232 |
+ |
|
| 233 |
+ it "should return the original url on both events" do |
|
| 234 |
+ agent.receive events |
|
| 235 |
+ expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url) |
|
| 236 |
+ expect(agent.the_created_events[1][:payload]['url']).to eq(failing_url) |
|
| 237 |
+ end |
|
| 238 |
+ |
|
| 239 |
+ end |
|
| 240 |
+ |
|
| 241 |
+ end |
|
| 242 |
+ |
|
| 243 |
+ describe "validations" do |
|
| 244 |
+ before do |
|
| 245 |
+ expect(agent).to be_valid |
|
| 246 |
+ end |
|
| 247 |
+ |
|
| 248 |
+ it "should validate url" do |
|
| 249 |
+ agent.options['url'] = "" |
|
| 250 |
+ expect(agent).not_to be_valid |
|
| 251 |
+ |
|
| 252 |
+ agent.options['url'] = "http://www.google.com" |
|
| 253 |
+ expect(agent).to be_valid |
|
| 254 |
+ end |
|
| 255 |
+ end |
|
| 256 |
+ |
|
| 257 |
+ end |
|
| 258 |
+ |
|
| 259 |
+ |
|
| 260 |
+end |
@@ -156,6 +156,17 @@ shared_examples_for WebRequestConcern do |
||
| 156 | 156 |
agent.options['disable_url_encoding'] = 'true' |
| 157 | 157 |
expect(agent.faraday.options.params_encoder).to eq(WebRequestConcern::DoNotEncoder) |
| 158 | 158 |
end |
| 159 |
+ |
|
| 160 |
+ describe "redirect follow" do |
|
| 161 |
+ it "should use FollowRedirects by default" do |
|
| 162 |
+ expect(agent.faraday.builder.handlers).to include(FaradayMiddleware::FollowRedirects) |
|
| 163 |
+ end |
|
| 164 |
+ |
|
| 165 |
+ it "should not use FollowRedirects when disabled" do |
|
| 166 |
+ agent.options['disable_redirect_follow'] = true |
|
| 167 |
+ expect(agent.faraday.builder.handlers).not_to include(FaradayMiddleware::FollowRedirects) |
|
| 168 |
+ end |
|
| 169 |
+ end |
|
| 159 | 170 |
end |
| 160 | 171 |
|
| 161 | 172 |
describe WebRequestConcern::DoNotEncoder do |