1
0

http_status_agent_spec.rb 11 KB

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