Sha256: c30aee80ebd707ab574661fef1bc1e8880ec90555a9c202c35753384c7027865

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 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)
    ::Faraday::Connection.any_instance.stubs(:get).
      raises(::Faraday::Error::ClientError.new(nil, { status: status_code}))
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tracker_api-1.9.1 test/error_test.rb
tracker_api-1.9.0 test/error_test.rb
tracker_api-1.8.0 test/error_test.rb