# frozen_string_literal: true require 'spec_helper' describe UplandMobileCommonsRest::Client do describe 'default_options' do it 'should set some defaults' do expect(subject.default_options[:user_agent]).to match(/ruby/) expect(subject.default_options[:host]).to eq('secure.mcommons.com') end end describe 'helpers' do it 'should return campaigns helper' do campaigns = subject.campaigns expect(campaigns).to be_a(UplandMobileCommonsRest::Campaigns) expect(campaigns.client).to eq(subject) end end describe 'error handling' do let(:request_params) { {example: 'data'} } subject { UplandMobileCommonsRest::Client.new(username: 'foo', password: 'bar') } before :each do stub_request(:post, 'https://secure.mcommons.com/api/do_something') .to_return(status: response_status, body: response_body) end shared_examples_for 'error code parsing' do UplandMobileCommonsRest::TypedErrorMiddleware::POSSIBLE_ERRORS.each do |code, ex_type| context "response body with success=false and error code #{code}" do let(:response_body) do <<~XML XML end it "should raise #{ex_type}" do expect{ subject.post_request('do_something', request_params) }.to raise_error(ex_type) end end end end context '200 status response' do let(:response_status) { 200 } context 'no response body' do let(:response_body) { nil } it 'should raise an UnknownError' do expect{ subject.post_request('do_something', request_params) }.to raise_error(UplandMobileCommonsRest::UnknownError) end end context 'response body without tag' do let(:response_body) { 'Well, this is unexpected!' } it 'should raise an UnknownError' do expect{ subject.post_request('do_something', request_params) }.to raise_error(UplandMobileCommonsRest::UnknownError) end end context 'response body with success=true' do let(:response_body) { 'hurray!' } it 'should not raise' do expect{ subject.post_request('do_something', request_params) }.not_to raise_error end end include_examples 'error code parsing' end context '400 status response' do let(:response_status) { 400 } include_examples 'error code parsing' end context '502 status response' do let(:response_status) { 502 } let(:response_body) { '502 Bad Gateway

502 Bad Gateway

' } it 'should raise an BadGatewayError' do expect do subject.post_request('do_something', request_params) end.to raise_error(UplandMobileCommonsRest::BadGatewayError) do |error| expect(error.body).to eq response_body expect(error.to_s).to include(response_body) end end end context '503 status response' do let(:response_status) { 503 } let(:response_body) { '503 Internal Server Error

503 Internal Server Error

' } it 'should raise an UnknownError' do expect do subject.post_request('do_something', request_params) end.to raise_error(UplandMobileCommonsRest::UnknownError) do |error| expect(error.to_s).to include(response_body) end end end context '504 status response' do let(:response_status) { 504 } let(:response_body) { '504 Gateway Time-out

504 Gateway Time-out

' } it 'should raise an GatewayTimeoutError' do expect do subject.post_request('do_something', request_params) end.to raise_error(UplandMobileCommonsRest::GatewayTimeoutError) do |error| expect(error.body).to eq response_body expect(error.to_s).to include(response_body) end end end end end