Keine Beschreibung http://j1x-huginn.herokuapp.com

http_status_agent_spec.rb 9.3KB

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