Sha256: 766c49539c6acb90d1c1658310de494351a4b7b60741842608e3f885fb373555

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 KB

Contents

require_relative 'minitest_helper'

describe TrackerApi::Error do
  let(:pt_user) { PT_USER_1 }
  let(:client) { TrackerApi::Client.new token: pt_user[:token] }
  let(:options) { { url: nil, headers: nil } }

  it 'raises ClientError for 4xx HTTP status codes' do
    (400..499).each do |status_code|
      mock_faraday_error(status_code)
      assert_raises TrackerApi::Errors::ClientError do
        client.send(:request, :get, options)
      end
    end
  end

  it 'raises ServerError for 5xx HTTP status codes' do
    (500..599).each do |status_code|
      mock_faraday_error(status_code)
      assert_raises TrackerApi::Errors::ServerError do
        client.send(:request, :get, options)
      end
    end
  end

  it 'raises RuntimeError for HTTP status codes < 400 and > 500' do
    [399, 600].each do |status_code|
      mock_faraday_error(status_code)
      assert_raises RuntimeError, "Expected 4xx or 5xx HTTP status code" do
        client.send(:request, :get, options)
      end
    end
  end

  # Simulate the error Faraday will raise with a specific HTTP status code so
  # we can test our rescuing of those errors
  def mock_faraday_error(status_code)
    mocked_error_class = if (500..599).include?(status_code) && Faraday::VERSION.to_f >= 16.0
      Faraday::ServerError
    else
      Faraday::ClientError
    end

    ::Faraday::Connection.any_instance.stubs(:get).
      raises(mocked_error_class.new(nil, { status: status_code}))
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tracker_api-1.16.0 test/error_test.rb
tracker_api-1.15.0 test/error_test.rb
tracker_api-1.14.0 test/error_test.rb
tracker_api-1.13.0 test/error_test.rb
tracker_api-1.12.0 test/error_test.rb
tracker_api-1.11.0 test/error_test.rb
tracker_api-1.10.0 test/error_test.rb