diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 7194d16..f4d0b8b 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -15,7 +15,7 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e with: - ruby-version: 2.7 + ruby-version: 3.0 bundler-cache: true # runs 'bundle install' and caches installed gems automatically - name: Install cc-test-reporter and prebuild notification run: | diff --git a/spec/client_spec.rb b/spec/client_spec.rb index ef8152c..bbddd0b 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -27,20 +27,42 @@ def post(*) describe '#shorten_link' do shared_examples 'short link created correctly' do |api_key_location| - it 'shorten link correctly' do - link = 'http://saiqulhaq.com' - VCR.use_cassette("shorten_link-SHORT-#{ENV['BUNDLE_GEMFILE']}-#{api_key_location}") do - options = { suffix_option: 'SHORT' } - result = subject.shorten_link(link, options) - expect(result[:link]).not_to eq('') - expect(result[:link]).not_to eq(link) + context 'when faraday default adapter is not defined' do + it 'raises error' do + if FirebaseDynamicLink::USE_FARADAY_2 + Faraday.default_adapter = nil + + link = 'http://saiqulhaq.com' + options = { suffix_option: 'SHORT' } + expect { + subject.shorten_link(link, options) + }.to raise_error + else + expect(true).to be_truthy + end + end + end + + context 'when faraday default adapter is defined' do + before do + Faraday.default_adapter = :net_http if FirebaseDynamicLink::USE_FARADAY_2 end - VCR.use_cassette("shorten_link-UNGUESSABLE-#{ENV['BUNDLE_GEMFILE']}-#{api_key_location}") do - options = { suffix_option: 'UNGUESSABLE', timout: 5 } - result = subject.shorten_link(link, options) - expect(result[:link]).not_to eq('') - expect(result[:link]).not_to eq(link) + it 'shorten link correctly' do + link = 'http://saiqulhaq.com' + VCR.use_cassette("shorten_link-SHORT-#{ENV['BUNDLE_GEMFILE']}-#{api_key_location}") do + options = { suffix_option: 'SHORT' } + result = subject.shorten_link(link, options) + expect(result[:link]).not_to eq('') + expect(result[:link]).not_to eq(link) + end + + VCR.use_cassette("shorten_link-UNGUESSABLE-#{ENV['BUNDLE_GEMFILE']}-#{api_key_location}") do + options = { suffix_option: 'UNGUESSABLE', timout: 5 } + result = subject.shorten_link(link, options) + expect(result[:link]).not_to eq('') + expect(result[:link]).not_to eq(link) + end end end end @@ -112,46 +134,68 @@ def post(*) } end - it 'shorten link correctly' do - VCR.use_cassette("shorten_parameters-SHORT-#{ENV['BUNDLE_GEMFILE']}") do - options = { - suffix_option: 'SHORT' - # dynamic_link_domain: 'foo' # optional - } + context 'when faraday default adapter is not defined' do + it 'raises error' do + if FirebaseDynamicLink::USE_FARADAY_2 + Faraday.default_adapter = nil - expect do - result = subject.shorten_parameters(parameters, options) - expect(result[:link]).not_to eq('') - expect(result[:link]).not_to eq(link) - end.not_to raise_error + link = 'http://saiqulhaq.com' + options = { suffix_option: 'SHORT' } + expect { + subject.shorten_parameters(parameters, options) + }.to raise_error + else + expect(true).to be_truthy + end end + end - VCR.use_cassette("shorten_parameters-UNGUESSABLE-#{ENV['BUNDLE_GEMFILE']}") do - options = { - suffix_option: 'UNGUESSABLE' - # dynamic_link_domain: 'foo' # optional - } + context 'when faraday default adapter is defined' do + before do + Faraday.default_adapter = :net_http if FirebaseDynamicLink::USE_FARADAY_2 + end - expect do - result = subject.shorten_parameters(parameters, options) - expect(result[:link]).not_to eq('') - expect(result[:link]).not_to eq(link) - end.not_to raise_error + it 'shorten link correctly' do + VCR.use_cassette("shorten_parameters-SHORT-#{ENV['BUNDLE_GEMFILE']}") do + options = { + suffix_option: 'SHORT' + # dynamic_link_domain: 'foo' # optional + } + + expect do + result = subject.shorten_parameters(parameters, options) + expect(result[:link]).not_to eq('') + expect(result[:link]).not_to eq(link) + end.not_to raise_error + end + + VCR.use_cassette("shorten_parameters-UNGUESSABLE-#{ENV['BUNDLE_GEMFILE']}") do + options = { + suffix_option: 'UNGUESSABLE' + # dynamic_link_domain: 'foo' # optional + } + + expect do + result = subject.shorten_parameters(parameters, options) + expect(result[:link]).not_to eq('') + expect(result[:link]).not_to eq(link) + end.not_to raise_error + end end - end - it 'raise FirebaseDynamicLink::ConnectionError if Faraday::ConnectionFailed raised' do - allow_any_instance_of(described_class).to receive(:connection).and_return(connection_failed_class.new) - expect do - subject.shorten_parameters(parameters) - end.to raise_error(FirebaseDynamicLink::ConnectionError) - end + it 'raise FirebaseDynamicLink::ConnectionError if Faraday::ConnectionFailed raised' do + allow_any_instance_of(described_class).to receive(:connection).and_return(connection_failed_class.new) + expect do + subject.shorten_parameters(parameters) + end.to raise_error(FirebaseDynamicLink::ConnectionError) + end - it 'raise FirebaseDynamicLink::ConnectionError if Faraday::TimeoutError raised' do - allow_any_instance_of(described_class).to receive(:connection).and_return(timout_error_class.new) - expect do - subject.shorten_link(parameters) - end.to raise_error(FirebaseDynamicLink::ConnectionError) + it 'raise FirebaseDynamicLink::ConnectionError if Faraday::TimeoutError raised' do + allow_any_instance_of(described_class).to receive(:connection).and_return(timout_error_class.new) + expect do + subject.shorten_parameters(parameters) + end.to raise_error(FirebaseDynamicLink::ConnectionError) + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c19b8f7..812dbf4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -51,6 +51,10 @@ module FirebaseDynamicLink config.expect_with :rspec do |c| c.syntax = :expect end + + config.after(:each) do + Faraday.default_connection = nil + end end VCR.configure do |config|