Sha256: 91ba7f2435c4ff285f43b9cb07794f8927d69bf274722a169b9337015d9e9950

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

require 'spec_helper'

describe Jamnagar::Utilities::UrlExpander do
  it 'should have the option to use a cache' do
    cache = {}
    client = Jamnagar::SpecHelpers::FakeHttpClient
    sut = Jamnagar::Utilities::UrlExpander.new(client, cache)
  end

  context 'Expanding Urls' do
    it 'should first check the cache for the URL' do
      cache = double(Jamnagar::SpecHelpers::FakeCache, :set => true)
      client = Jamnagar::SpecHelpers::FakeHttpClient
      sut = Jamnagar::Utilities::UrlExpander.new(client, cache)
      expect(cache).to receive(:get).with("http://bit.ly/foo")
      sut.expand("http://bit.ly/foo")
    end
    context 'cache miss' do
      it 'should cache the expansion' do
        cache = double(Jamnagar::SpecHelpers::FakeCache, :get => nil)
        client = Jamnagar::SpecHelpers::FakeHttpClient
        parser = Jamnagar::SpecHelpers::Parser
        sut = Jamnagar::Utilities::UrlExpander.new(client, cache, parser)
        expect(cache).to receive(:set).with("http://bit.ly/foo", {"final_url" => "http://bit.ly/foo", "body" => "null", "final_url_host" => "bit.ly"})
        sut.expand("http://bit.ly/foo")
      end
    end
    context 'cache hit' do
      it 'should not try to expand the url' do
        cache = Jamnagar::SpecHelpers::FakeCache.new
        client = double(Jamnagar::SpecHelpers::FakeHttpClient)
        sut = Jamnagar::Utilities::UrlExpander.new(client, cache)
        expect(client).to_not receive(:get)
        sut.expand("http://bit.ly/foo")
      end
      it 'should not try to cache the result' do
        cache = Jamnagar::SpecHelpers::FakeCache.new
        client = double(Jamnagar::SpecHelpers::FakeHttpClient)
        sut = Jamnagar::Utilities::UrlExpander.new(client, cache)
        expect(client).to_not receive(:get)
        expect(cache).to_not receive(:set)
        sut.expand("http://bit.ly/foo")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jamnagar-1.3.9.1 spec/url_expander_spec.rb
jamnagar-1.3.9 spec/url_expander_spec.rb
jamnagar-1.3.8 spec/url_expander_spec.rb