http_status_agent_spec.rb 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. require 'rails_helper'
  2. describe 'HttpStatusAgent' do
  3. let(:agent) do
  4. Agents::HttpStatusAgent.new(:name => SecureRandom.uuid, :options => valid_params).tap do |a|
  5. a.service = services(:generic)
  6. a.user = users(:jane)
  7. a.options['url'] = 'http://google.com'
  8. a.save!
  9. def a.interpolate_with(e, &block)
  10. @the_event = e
  11. block.call
  12. end
  13. def a.interpolated
  14. @the_event.payload
  15. end
  16. def a.create_event event
  17. @the_created_events ||= []
  18. @the_created_events << event
  19. end
  20. def a.the_created_events
  21. @the_created_events || []
  22. end
  23. def a.faraday
  24. @faraday ||= Struct.new(:programmed_responses).new({}).tap do |f|
  25. def f.get url
  26. programmed_responses[url] || raise('invalid url')
  27. end
  28. def f.set url, response, time = nil
  29. sleep(time/1000) if time
  30. programmed_responses[url] = response
  31. end
  32. end
  33. end
  34. end
  35. end
  36. let(:valid_params) { {} }
  37. describe "working" do
  38. it "should be working when the last status is 200" do
  39. agent.memory['last_status'] = '200'
  40. expect(agent.working?).to eq(true)
  41. end
  42. it "should be working when the last status is 304" do
  43. agent.memory['last_status'] = '304'
  44. expect(agent.working?).to eq(true)
  45. end
  46. it "should not be working if the status is 0" do
  47. agent.memory['last_status'] = '0'
  48. expect(agent.working?).to eq(false)
  49. end
  50. it "should not be working if the status is missing" do
  51. agent.memory['last_status'] = nil
  52. expect(agent.working?).to eq(false)
  53. end
  54. it "should not be working if the status is -1" do
  55. agent.memory['last_status'] = '-1'
  56. expect(agent.working?).to eq(false)
  57. end
  58. end
  59. describe "check" do
  60. before do
  61. def agent.interpolated
  62. @interpolated ||= { :url => SecureRandom.uuid }
  63. end
  64. def agent.check_this_url url
  65. @url = url
  66. end
  67. def agent.checked_url
  68. @url
  69. end
  70. end
  71. it "should check the url" do
  72. agent.check
  73. expect(agent.checked_url).to eq(agent.interpolated[:url])
  74. end
  75. end
  76. describe "receive" do
  77. describe "with an event with a successful ping" do
  78. let(:successful_url) { SecureRandom.uuid }
  79. let(:status_code) { 200 }
  80. let(:event_with_a_successful_ping) do
  81. agent.faraday.set(successful_url, Struct.new(:status).new(status_code))
  82. Event.new.tap { |e| e.payload = { url: successful_url } }
  83. end
  84. let(:events) do
  85. [event_with_a_successful_ping]
  86. end
  87. it "should create one event" do
  88. agent.receive events
  89. expect(agent.the_created_events.count).to eq(1)
  90. end
  91. it "should note that the successful response succeeded" do
  92. agent.receive events
  93. expect(agent.the_created_events[0][:payload]['response_received']).to eq(true)
  94. end
  95. it "should return the status code" do
  96. agent.receive events
  97. expect(agent.the_created_events[0][:payload]['status']).to eq('200')
  98. end
  99. it "should remember the status" do
  100. agent.receive events
  101. expect(agent.memory['last_status']).to eq('200')
  102. end
  103. it "should record the time spent waiting for the reply" do
  104. agent.receive events
  105. expect(agent.the_created_events[0][:payload]['elapsed_time']).not_to be_nil
  106. end
  107. describe "but the status code is not 200" do
  108. let(:status_code) { 500 }
  109. it "should return the status code" do
  110. agent.receive events
  111. expect(agent.the_created_events[0][:payload]['status']).to eq('500')
  112. end
  113. it "should remember the status" do
  114. agent.receive events
  115. expect(agent.memory['last_status']).to eq('500')
  116. end
  117. end
  118. it "should return the original url" do
  119. agent.receive events
  120. expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
  121. end
  122. describe "but the ping returns a status code of 0" do
  123. let(:event_with_a_successful_ping) do
  124. agent.faraday.set(successful_url, Struct.new(:status).new(0))
  125. Event.new.tap { |e| e.payload = { url: successful_url } }
  126. end
  127. it "should create one event" do
  128. agent.receive events
  129. expect(agent.the_created_events.count).to eq(1)
  130. end
  131. it "should note that no response was received" do
  132. agent.receive events
  133. expect(agent.the_created_events[0][:payload]['response_received']).to eq(false)
  134. end
  135. it "should return the original url" do
  136. agent.receive events
  137. expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
  138. end
  139. it "should remember no status" do
  140. agent.memory['last_status'] = '200'
  141. agent.receive events
  142. expect(agent.memory['last_status']).to be_nil
  143. end
  144. end
  145. describe "but the ping returns a status code of -1" do
  146. let(:event_with_a_successful_ping) do
  147. agent.faraday.set(successful_url, Struct.new(:status).new(-1))
  148. Event.new.tap { |e| e.payload = { url: successful_url } }
  149. end
  150. it "should create one event" do
  151. agent.receive events
  152. expect(agent.the_created_events.count).to eq(1)
  153. end
  154. it "should note that no response was received" do
  155. agent.receive events
  156. expect(agent.the_created_events[0][:payload]['response_received']).to eq(false)
  157. end
  158. it "should return the original url" do
  159. agent.receive events
  160. expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
  161. end
  162. end
  163. describe "and with one event with a failing ping" do
  164. let(:failing_url) { SecureRandom.uuid }
  165. let(:event_with_a_failing_ping) { Event.new.tap { |e| e.payload = { url: failing_url } } }
  166. let(:events) do
  167. [event_with_a_successful_ping, event_with_a_failing_ping]
  168. end
  169. it "should create two events" do
  170. agent.receive events
  171. expect(agent.the_created_events.count).to eq(2)
  172. end
  173. it "should note that the failed response failed" do
  174. agent.receive events
  175. expect(agent.the_created_events[1][:payload]['response_received']).to eq(false)
  176. end
  177. it "should note that the successful response succeeded" do
  178. agent.receive events
  179. expect(agent.the_created_events[0][:payload]['response_received']).to eq(true)
  180. end
  181. it "should return the original url on both events" do
  182. agent.receive events
  183. expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
  184. expect(agent.the_created_events[1][:payload]['url']).to eq(failing_url)
  185. end
  186. it "should record the time spent waiting for the reply" do
  187. agent.receive events
  188. expect(agent.the_created_events[0][:payload]['elapsed_time']).not_to be_nil
  189. expect(agent.the_created_events[1][:payload]['elapsed_time']).not_to be_nil
  190. end
  191. end
  192. end
  193. describe "validations" do
  194. before do
  195. expect(agent).to be_valid
  196. end
  197. it "should validate url" do
  198. agent.options['url'] = ""
  199. expect(agent).not_to be_valid
  200. agent.options['url'] = "http://www.google.com"
  201. expect(agent).to be_valid
  202. end
  203. end
  204. end
  205. end