require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe 'get comments' do include Rack::Test::Methods def app TestApp end before do Cachai.domain = 'domain.com' Cachai.cache = Redis.new end describe 'no domain' do it 'returns 400' do get '/comments.json', :path => '/foobar' puts last_response.body last_response.status.should == 400 end end describe 'no path' do it 'returns 400' do get '/comments.json', :domain => 'invalid.com' last_response.status.should == 400 end end describe 'existing domain and path' do describe 'invalid domain' do it 'returns 400' do get '/comments.json', :domain => 'invalid.com' last_response.status.should == 400 end end describe 'valid domain' do let(:cache_key) { Cachai.key_for('/blog/post.html') } before do Cachai.cache.del(cache_key) end describe 'unexisting post' do it 'returns 200' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' File.open('/tmp/error.html', 'w') { |f| f.write(last_response.body) } last_response.status.should == 200 end it 'returns an empty JSON array' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' last_response.body.should == '[]' end it 'does not create a new entry for that post' do expect do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' end.not_to change(Cachai::Post, :count) end end describe 'existing post, no comments' do before do Cachai::Post.create(:path => '/blog/post.html') Cachai::Post.count.should == 1 end it 'returns 200' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' last_response.status.should == 200 end it 'queries the database' do # ActiveRecord::Base.should_receive(:query) Cachai::Post.should_receive(:find_by_path) get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' end it 'returns an empty JSON array' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' last_response.body.should == '[]' end end describe 'existing post, with comments' do before do Cachai::Post.delete_all post = Cachai::Post.create(:path => '/blog/post.html') Cachai::Post.count.should == 1 post.responses.create({ :content => 'Foobar', :author_name => 'Someone', :author_email => 'test@email.com' }) post.responses.count.should == 1 end describe 'not in cache' do before do Cachai.cache.get(cache_key).should be_nil end it 'returns 200' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' last_response.status.should == 200 end it 'queries the database' do # ActiveRecord::Base.connection.should_receive(:execute) Cachai::Post.should_receive(:find_by_path) get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' end it 'returns an JSON array with comment' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' data = JSON.parse(last_response.body) data.count.should == 1 end it 'stores data in cache' do Cachai.cache.should_receive(:set) get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' end end describe 'in cache' do before do Cachai.cache.set(cache_key, 'something') Cachai.cache.get(cache_key).should == 'something' end describe 'without nocache param' do it 'returns 200' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' last_response.status.should == 200 end it 'does not query the database' do # ActiveRecord::Base.should_not_receive(:query) Cachai::Post.should_not_receive(:find_by_path) get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' end it 'returns an JSON array with comment' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' last_response.body.should == 'something' end it 'does not store data in cache' do Cachai.cache.should_not_receive(:set) get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html' end end describe 'with nocache=1' do it 'returns 200' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html', :nocache => 1 last_response.status.should == 200 end it 'queries the database' do # ActiveRecord::Base.should_receive(:query) Cachai::Post.should_receive(:find_by_path) get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html', :nocache => 1 end it 'returns an JSON array with comment' do get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html', :nocache => 1 data = JSON.parse(last_response.body) data.count.should == 1 end it 'stores data in cache' do Cachai.cache.should_receive(:set) get '/comments.json', :domain => 'domain.com', :path => '/blog/post.html', :nocache => 1 end end end end end end end