Sha256: 8dababa30a8c83c6b14710e0c5207e97c36d5b4cdcf797bb9954bdb91404cd3a

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

require 'rspec'
require 'travis_check_rubies/fetcher'

describe TravisCheckRubies::Fetcher do
  describe '#data' do
    subject{ described_class.new(url) }

    let(:url){ 'https://example.com/index.txt' }
    let(:cache_path){ FSPath.temp_file_path }

    before do
      allow(subject).to receive(:cache_path).and_return(cache_path)
    end

    it 'returns data from url' do
      allow(Net::HTTP).to receive(:get).with(URI(url)).
        and_return("one\ntwo\nthree")

      expect(subject.data).to eq("one\ntwo\nthree")
    end

    it 'caches result' do
      allow(Net::HTTP).to receive(:get).with(URI(url)).
        once.and_return("a\nb\nc")

      3.times{ expect(subject.data).to eq("a\nb\nc") }
    end

    it 'reads cache from file if it is new' do
      cache_path.write "foo\nbar"
      allow(cache_path).to receive(:size?).and_return(616)
      allow(cache_path).to receive(:mtime).and_return(Time.now - described_class::CACHE_TIME / 2)

      expect(Net::HTTP).not_to receive(:get)
      expect(subject.data).to eq("foo\nbar")
    end

    it 'writes cache file if it is stale' do
      allow(cache_path).to receive(:size?).and_return(616)
      allow(cache_path).to receive(:mtime).and_return(Time.now - described_class::CACHE_TIME * 2)
      allow(Net::HTTP).to receive(:get).with(URI(url)).
        once.and_return("brave\nnew\nworld")

      expect(subject.data).to eq("brave\nnew\nworld")
      expect(cache_path.read).to eq("brave\nnew\nworld")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
travis_check_rubies-0.6.0 spec/travis_check_rubies/fetcher_spec.rb
travis_check_rubies-0.5.1 spec/travis_check_rubies/fetcher_spec.rb
travis_check_rubies-0.5.0 spec/travis_check_rubies/fetcher_spec.rb