# frozen_string_literal: true require 'spec_helper' RSpec.describe Roqua::Healthy::Oru::Client do def stub_oru_response(status, body = 'ACK') stub_request(:post, oru_url) .with( body: \ '%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0A%3C' \ 'hash%3E%0A++%3Coru+type%3D%22symbol%22%3Edata%3C%2Foru%3E%0A%3C%2Fhash%3E%0A', headers: { 'Accept' => '*/*', 'Accept-Encoding' => /gzip.*?deflate.*/, 'Content-Length' => '140', 'Content-Type' => 'text/xml' } ) .to_return(status: status, body: body, headers: {}) end def stub_500_response stub_request(:post, oru_url) .to_return(status: 500, body: 'NACKERROR', headers: {}) end def stub_404_response stub_request(:post, oru_url) .to_return(status: 404, body: 'NACKERROR', headers: {}) end let(:subject) { Roqua::Healthy::Oru::Client } let(:oru_url) { 'https://example.com' } let(:timeout) { 5 } def execute_request subject.execute(oru_url, {oru: :data}, timeout: timeout) end context 'when Errno::ETIMEDOUT is triggered' do it 'will raise an ::Roqua::Healthy::Timeout exception' do expect(Roqua::Healthy::Oru::Client).to receive(:deliver_data).and_raise(Errno::ETIMEDOUT) expect { execute_request }.to raise_error(::Roqua::Healthy::Timeout) end end it 'sets the correct timeout' do expect(RestClient::Request).to receive(:execute).with( method: :post, url: 'https://example.com', payload: '%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0A%3Chash%3E%0A++%3C' \ 'oru+type%3D%22symbol%22%3Edata%3C%2Foru%3E%0A%3C%2Fhash%3E%0A', headers: {content_type: 'text/xml'}, timeout: timeout.to_i ).and_return(double(body: 'ACK')) execute_request end context "when upstream is responding" do it "does not raise an exception" do stub_oru_response 200, 'ACK' execute_request end context 'when a nack is received' do before { stub_oru_response 200, 'NACKNACK' } it 'raises a NACK exception' do expect { execute_request }.to raise_error(Roqua::Healthy::NACK) end end end context "when upstream is giving connection refused" do before do stub_request(:any, oru_url).to_raise Errno::ECONNREFUSED end it "should raise Oru::ConnectionRefused" do expect { execute_request }.to raise_exception(Roqua::Healthy::ConnectionRefused) end end context "when upstream is giving connection timeouts" do before do stub_request(:any, oru_url).to_timeout end it "should raise Oru::Timeout" do expect { execute_request }.to raise_exception(Roqua::Healthy::Timeout) end end context "when upstream is giving waiting for ack timeouts" do before do stub_oru_response \ 500, "FAILURERGOC - Outbound port 60102" \ "Timeout waiting for ACK" end it "raises a NACK exception" do expect { execute_request }.to raise_error(::Roqua::Healthy::Timeout, /Timeout waiting for ACK/) end end context 'when upstream responds with a 500 status' do before { stub_500_response } it 'transforms the exception to a NACK' do expect { execute_request }.to raise_error Roqua::Healthy::NACK end end context 'when upstream responds with a 404 status' do before { stub_404_response } it 'transforms the exception to a NACK' do expect { execute_request }.to raise_error Roqua::Healthy::NACK end end context "when the response doesn't contain an error portion" do it 'will raise a ... exception' do stub_request(:post, oru_url) .to_return(status: 500, body: 'Internal server error', headers: {}) expect { execute_request }.to raise_error Roqua::Healthy::UnknownFailure end end end