Sha256: f47c6d1643cb023e652592f0c369ef73477f437759e5248d7fe5accf60a6b8fe

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

require 'spec_helper'
require 'webmock/rspec'

describe Gibbon::APIRequest do
  let(:api_key) { "1234-us1" }

  before do
    @gibbon = Gibbon::Request.new(api_key: api_key)
    @api_root = "https://apikey:#{api_key}@us1.api.mailchimp.com/3.0"
  end

  it "surfaces client request exceptions as a Gibbon::MailChimpError" do
    exception = Faraday::Error::ClientError.new("the server responded with status 503")
    stub_request(:get, "#{@api_root}/lists").to_raise(exception)
    expect { @gibbon.lists.retrieve }.to raise_error(Gibbon::MailChimpError)
  end

  it "surfaces an unparseable client request exception as a Gibbon::MailChimpError" do
    exception = Faraday::Error::ClientError.new(
      "the server responded with status 503")
    stub_request(:get, "#{@api_root}/lists").to_raise(exception)
    expect { @gibbon.lists.retrieve }.to raise_error(Gibbon::MailChimpError)
  end

  it "surfaces an unparseable response body as a Gibbon::MailChimpError" do
    response_values = {:status => 503, :headers => {}, :body => '[foo]'}
    exception = Faraday::Error::ClientError.new("the server responded with status 503", response_values)

    stub_request(:get, "#{@api_root}/lists").to_raise(exception)
    expect { @gibbon.lists.retrieve }.to raise_error(Gibbon::MailChimpError)
  end

  context "handle_error" do
    it "includes status and raw body even when json can't be parsed" do
      response_values = {:status => 503, :headers => {}, :body => 'A non JSON response'}
      exception = Faraday::Error::ClientError.new("the server responded with status 503", response_values)
      api_request = Gibbon::APIRequest.new(builder: Gibbon::Request)
      begin
        api_request.send :handle_error, exception
      rescue => boom
        expect(boom.status_code).to eq 503
        expect(boom.raw_body).to eq "A non JSON response"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gibbon-3.0.1 spec/gibbon/api_request_spec.rb
gibbon-2.2.5 spec/gibbon/api_request_spec.rb