Sha256: 158c14718e134ada88c786d27f088bf8d6ba501e670850d7cb17f7b53c273d41

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

if respond_to?(:require_relative, true)
  require_relative 'common'
else
  require File.dirname(__FILE__) + '/common'
end

describe RestGraph do
  after do
    WebMock.reset!
    RR.verify
  end

  describe 'cache' do
    before do
      @url, @body = "https://graph.facebook.com/cache", '{"message":"ok"}'
      @cache = {}
      @rg = RestGraph.new(:cache => @cache, :auto_decode => false)
      stub_request(:get, @url).to_return(:body => @body).times(1)
    end

    should 'enable cache if passing cache' do
      3.times{ @rg.get('cache').should == @body }
      @cache.should == {@rg.send(:cache_key, {}, @url) => @body}
    end

    should 'respect expires_in' do
      mock(@cache).method(:store){ mock!.arity{ -3 } }
      mock(@cache).store(@rg.send(:cache_key, {}, @url), @body,
                         :expires_in => 3)
      @rg.get('cache', {}, :expires_in => 3).should == @body
    end

    should 'update cache if there is cache option set to false' do
      @rg.get('cache')                     .should == @body
      stub_request(:get, @url).to_return(:body => @body.reverse).times(2)
      @rg.get('cache')                     .should == @body
      @rg.get('cache', {}, :cache => false).should == @body.reverse
      @rg.get('cache')                     .should == @body.reverse
      @rg.cache = nil
      @rg.get('cache', {}, :cache => false).should == @body.reverse
    end
  end

  should 'not cache post/put/delete' do
    [:put, :post, :delete].each{ |meth|
      url, body = "https://graph.facebook.com/cache", '{"message":"ok"}'
      stub_request(meth, url).to_return(:body => body).times(3)

      cache = {}
      rg = RestGraph.new(:cache => cache)
      3.times{
        if meth == :delete
          rg.send(meth, 'cache').should == {'message' => 'ok'}
        else
          rg.send(meth, 'cache', 'payload').should == {'message' => 'ok'}
        end
      }
      cache.should == {}
    }
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rest-graph-2.0.3 test/test_cache.rb
rest-graph-2.0.2 test/test_cache.rb
rest-graph-2.0.1 test/test_cache.rb
rest-graph-2.0.0 test/test_cache.rb
rest-graph-1.9.1 test/test_cache.rb
rest-graph-1.9.0 test/test_cache.rb