spec/routemaster/integration/cache_spec.rb in routemaster-drain-2.3.0 vs spec/routemaster/integration/cache_spec.rb in routemaster-drain-2.4.0

- old
+ new

@@ -1,73 +1,86 @@ require 'spec_helper' require 'spec/support/uses_redis' require 'spec/support/uses_dotenv' +require 'spec/support/uses_webmock' +require 'spec/support/server' require 'routemaster/cache' require 'webrick' -RSpec.describe 'Requests with caching' do +describe Routemaster::Cache do uses_dotenv uses_redis + uses_webmock - let!(:log) { WEBrick::Log.new '/dev/null' } + def time_us + Integer(Time.now.to_f * 1e6) + end + let(:service) do - WEBrick::HTTPServer.new(Port: 8000, DocumentRoot: Dir.pwd, Logger: log).tap do |server| + TestServer.new(8000) do |server| server.mount_proc '/test' do |req, res| - res.body = { field: 'test' }.to_json + res['content-type'] = 'application/json' + res['x-timestamp'] = time_us.to_s + res.body = { value: time_us }.to_json end end end - before do - @pid = fork do - trap 'INT' do service.shutdown end - service.start - end - sleep(0.5) # leave sometime for the previous webrick to teardown - end + before { service.start } + after { service.stop } - after do - Process.kill('KILL', @pid) - Process.wait(@pid) - end + before { WebMock.disable_net_connect!(allow_localhost: true) } - subject { Routemaster::Cache.new } - - describe 'GET request' do - let(:body_cache_keys) { ["cache:#{url}", "v:,l:,body"] } - let(:headers_cache_keys) { ["cache:#{url}", "v:,l:,headers"] } + shared_examples 'a cached GET' do let(:url) { 'http://localhost:8000/test' } + let(:response) { perform.call } + context 'when there is no previous cached response' do - it 'makes an http call' do - response = subject.get(url) - expect(response.headers['server']).to be + it 'makes a HTTP request' do + perform.call + expect(WebMock).to have_requested(:get, url) end - it 'sets the new response onto the cache' do - expect { subject.get(url) } - .to change { Routemaster::Config.cache_redis.hget(*body_cache_keys)} - .from(nil) - .to({ field: 'test'}.to_json) + it 'returns fresh headers' do + time_before = time_us() + expect(response.headers['x-timestamp'].to_i).to be > time_before end - it 'sets the response headers onto the cache' do - expect { subject.get(url) } - .to change { Routemaster::Config.cache_redis.hget(*headers_cache_keys)} - .from(nil) + it 'returns a fresh body' do + time_before = time_us() + expect(response.body.value).to be > time_before end end - context 'when there is a previous cached response' do - before { subject.get(url) } + context 'when there is a cached response' do + before { perform.call } - it 'fetches the cached response' do - expect(subject.get(url).body).to eq({ field: 'test' }.to_json) + it 'returns cached headers' do + time_before = time_us() + expect(response.headers['x-timestamp'].to_i).to be < time_before end - it 'does not make an http call' do - response = subject.get(url) - expect(response.env.request).to be_empty + it 'returns a cached body' do + time_before = time_us() + expect(response.body.value).to be < time_before end + + it 'makes no HTTP requests' do + WebMock.reset! + perform.call + expect(WebMock).not_to have_requested(:any, //) + end end + end + + + describe '#get' do + let(:perform) { -> { subject.get(url) } } + it_behaves_like 'a cached GET' + end + + describe '#fget' do + let(:perform) { -> { subject.fget(url).value } } + it_behaves_like 'a cached GET' end end