require File.dirname(__FILE__) + '/../../spec_helper' require 'useless/doc/client' describe Useless::Doc::Client::Standard do before(:each) do @client = Useless::Doc::Client.standard @client.instance_variable_set(:@cache, {}) end it 'should make a normal request if the cache is empty.' do Typhoeus.should_receive(:options). with('http://some-api.granmal.com/some/resource', headers: { 'Accept' => 'application/json' }). and_return(mock(:response, response_code: 200, response_body: load_document('resource.json').read)) resource = @client.get('http://some-api.granmal.com/some/resource') resource.path.should == '/twonks/:id' end it 'should return an API if that is what\'s returned' do Typhoeus.should_receive(:options). with('http://some-api.granmal.com/', headers: { 'Accept' => 'application/json' }). and_return(mock(:response, response_code: 200, response_body: load_document('api.json').read)) api = @client.get('http://some-api.granmal.com/') api.url.should == 'twonk.useless.io' end it 'should make a request with a cache control header if there is a cache hit.' do now = Time.now Time.should_receive(:now).once.and_return(now) Typhoeus.should_receive(:options).once. with('http://some-api.granmal.com/some/resource', headers: { 'Accept' => 'application/json' }). and_return(mock(:response, response_code: 200, response_body: load_document('resource.json').read)) Typhoeus.should_receive(:options).once. with('http://some-api.granmal.com/some/resource', headers: { 'Accept' => 'application/json', 'If-Modified-Since' => now.httpdate}). and_return(mock(:response, response_code: 304, response_body: '')) @client.get('http://some-api.granmal.com/some/resource') resource = @client.get('http://some-api.granmal.com/some/resource') resource.path.should == '/twonks/:id' end end describe Useless::Doc::Client::Stub do it 'should serve files from the spec/documents directory' do client = Useless::Doc::Client.stub resource = client.get('http://anything.useless.io/resource') resource.path.should == '/twonks/:id' end it 'should return nil if the corresponding file doesn\'t exist' do client = Useless::Doc::Client.stub resource = client.get('http://anything.useless.io/nonexistant') resource.should be_nil end end