Sha256: 33f08d1dbcab77467e246e575c6c875e0c70b9529887def0b0ccb0ddaccb0cb8

Contents?: true

Size: 1.93 KB

Versions: 9

Compression:

Stored size: 1.93 KB

Contents

shared_examples "API request" do
  context "without API key" do
    before(:each) do
      Europeana::API.api_key = nil
    end

    it "sends no HTTP request" do
      begin
        subject
      rescue Europeana::API::Errors::MissingAPIKeyError
      end
      expect(a_request(:get, /www.europeana.eu\/api\/v2/)).not_to have_been_made
    end
  end

  context "with API key" do
    before(:all) do
      Europeana::API.api_key = "xyz"
    end

    it "returns the response as a Hash" do
      response = subject
      expect(response).to be_a(Hash)
    end

    context "when the API is unavailable" do
      before(:each) do
        Europeana::API.retry_delay = 0
      end

      it "waits then retries" do
        stub_request(:get, /www.europeana.eu\/api\/v2/).
          to_timeout.times(1).then.
          to_return(:body => '{"success":true}')
        subject
        expect(a_request(:get, /www.europeana.eu\/api\/v2/)).to have_been_made.times(2)
      end
    end

    context "when API response is unsuccessful" do
      context "with error msg" do
        it "raises a RequestError with error msg" do
          stub_request(:get, /www.europeana.eu\/api\/v2/).to_return(body: '{"success":false,"error":"Something went wrong"}')
          expect { subject }.to raise_error(Europeana::API::Errors::RequestError, "Something went wrong")
        end
      end

      context "without error msg" do
        it "raises a RequestError with status code" do
          stub_request(:get, /www.europeana.eu\/api\/v2/).to_return(status: 400, body: '{"success":false}')
          expect { subject }.to raise_error(Europeana::API::Errors::RequestError, "400")
        end
      end
    end

    context "when API response is invalid JSON" do
      it "raises a ResponseError" do
        stub_request(:get, /www.europeana.eu\/api\/v2/).to_return(:body => 'invalid JSON')
        expect { subject }.to raise_error(Europeana::API::Errors::ResponseError)
      end
    end
  end
end


Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
europeana-api-0.5.2 spec/support/shared_examples/api_request.rb
europeana-api-0.5.0 spec/support/shared_examples/api_request.rb
europeana-api-0.4.3 spec/support/shared_examples/api_request.rb
europeana-api-0.4.2 spec/support/shared_examples/api_request.rb
europeana-api-0.4.1 spec/support/shared_examples/api_request.rb
europeana-api-0.3.6 spec/support/shared_examples/api_request.rb
europeana-api-0.3.5 spec/support/shared_examples/api_request.rb
europeana-api-0.3.4 spec/support/shared_examples/api_request.rb
europeana-api-0.3.3 spec/support/shared_examples/api_request.rb