http_status_agent_spec.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. require 'rails_helper'
  2. class MockResponse < Struct.new(:status, :headers, :url)
  3. alias_method :to_hash, :to_h
  4. end
  5. describe 'HttpStatusAgent' do
  6. let(:agent) do
  7. Agents::HttpStatusAgent.new(:name => SecureRandom.uuid, :options => valid_params).tap do |a|
  8. a.service = services(:generic)
  9. a.user = users(:jane)
  10. a.options['url'] = 'http://google.com'
  11. a.options['headers_to_save'] = 'Server'
  12. a.save!
  13. def a.interpolate_with(e, &block)
  14. @the_event = e
  15. block.call
  16. end
  17. def a.interpolated
  18. @the_event.payload
  19. end
  20. def a.create_event event
  21. @the_created_events ||= []
  22. @the_created_events << event
  23. end
  24. def a.the_created_events
  25. @the_created_events || []
  26. end
  27. def a.faraday
  28. @faraday ||= Struct.new(:programmed_responses).new({}).tap do |f|
  29. def f.get url
  30. programmed_responses[url] || raise('invalid url')
  31. end
  32. def f.set url, response, time = nil
  33. sleep(time/1000) if time
  34. programmed_responses[url] = response
  35. end
  36. end
  37. end
  38. end
  39. end
  40. let(:valid_params) { {} }
  41. describe "working" do
  42. it "should be working when the last status is 200" do
  43. agent.memory['last_status'] = '200'
  44. expect(agent.working?).to eq(true)
  45. end
  46. it "should be working when the last status is 304" do
  47. agent.memory['last_status'] = '304'
  48. expect(agent.working?).to eq(true)
  49. end
  50. it "should not be working if the status is 0" do
  51. agent.memory['last_status'] = '0'
  52. expect(agent.working?).to eq(false)
  53. end
  54. it "should not be working if the status is missing" do
  55. agent.memory['last_status'] = nil
  56. expect(agent.working?).to eq(false)
  57. end
  58. it "should not be working if the status is -1" do
  59. agent.memory['last_status'] = '-1'
  60. expect(agent.working?).to eq(false)
  61. end
  62. end
  63. describe "check" do
  64. before do
  65. def agent.interpolated
  66. @interpolated ||= { :url => SecureRandom.uuid, :headers_to_save => '' }
  67. end
  68. def agent.check_this_url url, local_headers
  69. @url = url
  70. @local_headers = local_headers
  71. end
  72. def agent.checked_url
  73. @url
  74. end
  75. end
  76. it "should check the url" do
  77. agent.check
  78. expect(agent.checked_url).to eq(agent.interpolated[:url])
  79. end
  80. end
  81. describe "receive" do
  82. describe "with an event with a successful ping" do
  83. let(:successful_url) { SecureRandom.uuid }
  84. let(:status_code) { 200 }
  85. let(:header) { SecureRandom.uuid }
  86. let(:header_value) { SecureRandom.uuid }
  87. let(:event_with_a_successful_ping) do
  88. agent.faraday.set(successful_url, MockResponse.new(status_code, {}, successful_url))
  89. Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } }
  90. end
  91. let(:events) do
  92. [event_with_a_successful_ping]
  93. end
  94. it "should create one event" do
  95. agent.receive events
  96. expect(agent.the_created_events.count).to eq(1)
  97. end
  98. it "should note that the successful response succeeded" do
  99. agent.receive events
  100. expect(agent.the_created_events[0][:payload]['response_received']).to eq(true)
  101. end
  102. it "should return the status code" do
  103. agent.receive events
  104. expect(agent.the_created_events[0][:payload]['status']).to eq('200')
  105. end
  106. it "should remember the status" do
  107. agent.receive events
  108. expect(agent.memory['last_status']).to eq('200')
  109. end
  110. it "should record the time spent waiting for the reply" do
  111. agent.receive events
  112. expect(agent.the_created_events[0][:payload]['elapsed_time']).not_to be_nil
  113. end
  114. it "should not return a header" do
  115. agent.receive events
  116. expect(agent.the_created_events[0][:payload]['headers']).to be_nil
  117. end
  118. describe "but the last status code was 200" do
  119. before { agent.memory['last_status'] = '200' }
  120. describe "and no duplication settings have been set" do
  121. it "should create one event" do
  122. agent.receive events
  123. expect(agent.the_created_events.count).to eq(1)
  124. end
  125. end
  126. describe "and change settings have been set to true" do
  127. before { agent.options['changes_only'] = 'true' }
  128. it "should NOT create any events" do
  129. agent.receive events
  130. expect(agent.the_created_events.count).to eq(0)
  131. end
  132. describe "but actually, the ping failed" do
  133. let(:failing_url) { SecureRandom.uuid }
  134. let(:event_with_a_failing_ping) { Event.new.tap { |e| e.payload = { url: failing_url, headers_to_save: "" } } }
  135. let(:events) do
  136. [event_with_a_successful_ping, event_with_a_failing_ping]
  137. end
  138. it "should create an event" do
  139. agent.receive events
  140. expect(agent.the_created_events.count).to eq(1)
  141. end
  142. end
  143. end
  144. describe "and change settings have been set to false" do
  145. before { agent.options['changes_only'] = 'false' }
  146. it "should create one event" do
  147. agent.receive events
  148. expect(agent.the_created_events.count).to eq(1)
  149. end
  150. end
  151. end
  152. describe "but the status code is not 200" do
  153. let(:status_code) { 500 }
  154. it "should return the status code" do
  155. agent.receive events
  156. expect(agent.the_created_events[0][:payload]['status']).to eq('500')
  157. end
  158. it "should remember the status" do
  159. agent.receive events
  160. expect(agent.memory['last_status']).to eq('500')
  161. end
  162. end
  163. it "should return the original url" do
  164. agent.receive events
  165. expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
  166. end
  167. it "should return the final url" do
  168. agent.receive events
  169. expect(agent.the_created_events[0][:payload]['final_url']).to eq(successful_url)
  170. end
  171. it "should return whether the url redirected" do
  172. agent.receive events
  173. expect(agent.the_created_events[0][:payload]['redirected']).to eq(false)
  174. end
  175. describe "but the ping returns a status code of 0" do
  176. let(:event_with_a_successful_ping) do
  177. agent.faraday.set(successful_url, MockResponse.new(0, {}, successful_url))
  178. Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } }
  179. end
  180. it "should create one event" do
  181. agent.receive events
  182. expect(agent.the_created_events.count).to eq(1)
  183. end
  184. it "should note that no response was received" do
  185. agent.receive events
  186. expect(agent.the_created_events[0][:payload]['response_received']).to eq(false)
  187. end
  188. it "should return the original url" do
  189. agent.receive events
  190. expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
  191. end
  192. it "should remember no status" do
  193. agent.memory['last_status'] = '200'
  194. agent.receive events
  195. expect(agent.memory['last_status']).to be_nil
  196. end
  197. end
  198. describe "but the ping returns a status code of -1" do
  199. let(:event_with_a_successful_ping) do
  200. agent.faraday.set(successful_url, MockResponse.new(-1, {}, successful_url))
  201. Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } }
  202. end
  203. it "should create one event" do
  204. agent.receive events
  205. expect(agent.the_created_events.count).to eq(1)
  206. end
  207. it "should note that no response was received" do
  208. agent.receive events
  209. expect(agent.the_created_events[0][:payload]['response_received']).to eq(false)
  210. end
  211. it "should return the original url" do
  212. agent.receive events
  213. expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
  214. end
  215. end
  216. describe "and with one event with a failing ping" do
  217. let(:failing_url) { SecureRandom.uuid }
  218. let(:event_with_a_failing_ping) { Event.new.tap { |e| e.payload = { url: failing_url, headers_to_save: "" } } }
  219. let(:events) do
  220. [event_with_a_successful_ping, event_with_a_failing_ping]
  221. end
  222. it "should create two events" do
  223. agent.receive events
  224. expect(agent.the_created_events.count).to eq(2)
  225. end
  226. it "should note that the failed response failed" do
  227. agent.receive events
  228. expect(agent.the_created_events[1][:payload]['response_received']).to eq(false)
  229. end
  230. it "should note that the successful response succeeded" do
  231. agent.receive events
  232. expect(agent.the_created_events[0][:payload]['response_received']).to eq(true)
  233. end
  234. it "should return the original url on both events" do
  235. agent.receive events
  236. expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
  237. expect(agent.the_created_events[1][:payload]['url']).to eq(failing_url)
  238. end
  239. it "should record the time spent waiting for the reply" do
  240. agent.receive events
  241. expect(agent.the_created_events[0][:payload]['elapsed_time']).not_to be_nil
  242. expect(agent.the_created_events[1][:payload]['elapsed_time']).not_to be_nil
  243. end
  244. end
  245. describe "with a header specified" do
  246. let(:event_with_a_successful_ping) do
  247. agent.faraday.set(successful_url, MockResponse.new(status_code, {header => header_value}, successful_url))
  248. Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: header } }
  249. end
  250. it "should return the header value" do
  251. agent.receive events
  252. expect(agent.the_created_events[0][:payload]['headers']).not_to be_nil
  253. expect(agent.the_created_events[0][:payload]['headers'][header]).to eq(header_value)
  254. end
  255. end
  256. describe "with existing and non-existing headers specified" do
  257. let(:nonexistant_header) { SecureRandom.uuid }
  258. let(:event_with_a_successful_ping) do
  259. agent.faraday.set(successful_url, MockResponse.new(status_code, {header => header_value}, successful_url))
  260. Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: header + "," + nonexistant_header } }
  261. end
  262. it "should return the existing header's value" do
  263. agent.receive events
  264. expect(agent.the_created_events[0][:payload]['headers'][header]).to eq(header_value)
  265. end
  266. it "should return nil for the nonexistant header" do
  267. agent.receive events
  268. expect(agent.the_created_events[0][:payload]['headers'][nonexistant_header]).to be_nil
  269. end
  270. end
  271. end
  272. describe "validations" do
  273. before do
  274. expect(agent).to be_valid
  275. end
  276. it "should validate url" do
  277. agent.options['url'] = ""
  278. expect(agent).not_to be_valid
  279. agent.options['url'] = "http://www.google.com"
  280. expect(agent).to be_valid
  281. end
  282. end
  283. end
  284. end