Sha256: ec867945f675d1c58b19ba3e8028ad62434ff916c756760d4268ffe7bc5ed947

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 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
      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

1 entries across 1 versions & 1 rubygems

Version Path
gibbon-2.2.4 spec/gibbon/api_request_spec.rb