Sha256: 7d73f3209b1e6fcb2fb44d3a8b9c5ae37cb1ce213a0e076ab90ff55788ced222

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

require File.dirname(__FILE__) + '/spec_helper'

describe APICache::API do
  before :each do
    FakeWeb.register_uri(:get, "http://www.google.com/", :body => "Google")
    FakeWeb.register_uri(:get, "http://froogle.google.com/", :status => 302, :location => "http://products.google.com")
    FakeWeb.register_uri(:get, "http://products.google.com/", :body => "Google Product Search")

    @options = {
      :period => 1,
      :timeout => 5
    }

    # Reset the store otherwise get queried too recently erros
    APICache.store = nil
  end

  it "should not be queryable for :period seconds after a request" do
    api = APICache::API.new('http://www.google.com/', @options)

    api.get
    lambda {
      api.get
    }.should raise_error(APICache::CannotFetch, "Cannot fetch http://www.google.com/: queried too recently")

    sleep 1
    lambda {
      api.get
    }.should_not raise_error
  end

  describe "without a block - key is the url" do

    it "should return body of a http GET against the key" do
      api = APICache::API.new('http://www.google.com/', @options)
      api.get.should =~ /Google/
    end

    it "should handle redirecting get requests" do
      api = APICache::API.new('http://froogle.google.com/', @options)
      api.get.should =~ /Google Product Search/
    end

  end

  describe "with a block" do

    it "should return the return value of the block" do
      api = APICache::API.new('http://www.google.com/', @options) do
        42
      end
      api.get.should == 42
    end

    it "should return the raised exception if the block raises one" do
      api = APICache::API.new('foobar', @options) do
        raise RuntimeError, 'foo'
      end
      lambda {
        api.get
      }.should raise_error(StandardError, 'foo')
    end

  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
mloughran-api_cache-0.2.0 spec/api_spec.rb
api_cache-0.2.0 spec/api_spec.rb