plugins/observed-http/spec/http_spec.rb in observed-0.1.1 vs plugins/observed-http/spec/http_spec.rb in observed-0.2.0.rc1

- old
+ new

@@ -1,14 +1,69 @@ require 'spec_helper' -require 'observed/application/oneshot' +require 'observed/http' -describe Observed::Application::Oneshot do +describe Observed::Plugins::HTTP do + subject { - Observed::Application::Oneshot.create( - config_file: 'spec/fixtures/observed.conf' - ) + Observed::Plugins::HTTP.new } - it 'initializes' do - expect(subject.run.size).not_to eq(0) + + before { + subject.configure config + } + + let(:config) { + { + timeout_in_milliseconds: 1000, + method: 'get', + url: 'http://google.com/', + tag: 'test', + system: sys + } + } + + let(:sys) { + sys = mock('system') + sys.stubs(:now).returns(before).then.returns(after) + sys + } + + let(:before) { + Time.now + } + + let(:after) { + Time.now + 1 + } + + let(:response) { + res = stub('response') + res.stubs(body: 'the response body') + res + } + + context 'when timed out' do + before { + Timeout.expects(:timeout).raises(Timeout::Error) + + sys.expects(:report).with('test.error', {status: :error, error: {message: 'Timed out.'}, timed_out: true}) + } + + it 'reports an error' do + expect { subject.observe }.to_not raise_error + end end + + context 'when not timed out' do + before { + Timeout.expects(:timeout).returns({ status: :success, result: 'Get http://google.com/' }) + + sys.expects(:report).with('test.success', {status: :success, result: 'Get http://google.com/', elapsed_time: after - before}) + } + + it 'reports an success' do + expect { subject.observe }.to_not raise_error + end + end + end