jerrett / simple-flickr

Yeah, another wrapper for the REST API of Flickr

This URL has Read+Write access

simple-flickr / spec / client_spec.rb
100644 52 lines (40 sloc) 2.398 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require File.dirname(__FILE__) + '/spec_helper.rb'
 
describe Flickr::Client do
  before( :each ) do
    @flickr = Flickr::Client.new( 'my-api-key' )
  end
  
  it 'should make a request to a flickr method with no arguments' do
    @flickr.should_receive( :open ).with( Flickr::Client::REST_ENDPOINT + '?api_key=my-api-key&method=flickr.test.echo' ).and_return( VALID_FLICKR_RESPONSE )
    @flickr.request( 'test.echo' )
  end
 
  it 'should make a request to a flickr method with arguments passed' do
    @flickr.should_receive( :open ).with( Flickr::Client::REST_ENDPOINT + '?api_key=my-api-key&method=flickr.favorites.getList&per_page=4&user_id=123456789N00' ).and_return( VALID_FLICKR_RESPONSE )
    @flickr.request( 'favorites.getList', :user_id => '123456789N00', :per_page => 4 )
  end
  
  [Errno::ETIMEDOUT,Timeout::Error.new(nil), OpenURI::HTTPError.new(nil,nil), Errno::ECONNRESET, SocketError, Errno::ECONNREFUSED].each do |e|
    it "should raise an appropriate error if open-uri raises #{e}" do
      @flickr.should_receive( :open ).and_raise( e )
      proc { @flickr.request( 'test.echo' ) }.should raise_error( Flickr::RequestError )
    end
  end
 
  it 'should raise an appropriate error if something goes wrong with the request' do
    @flickr.should_receive( :open ).and_return( INVALID_FLICKR_RESPONSE )
    proc { @flickr.request( 'test.echo' ) }.should raise_error( Flickr::RequestError )
  end
  
  it 'should cache requests returning the correct result and not make uneccesary calls to flickr' do
    @flickr.should_receive( :open ).once.and_return( VALID_FLICKR_RESPONSE )
    
    response = Hpricot.XML( VALID_FLICKR_RESPONSE.read ).at( 'rsp' )
    @flickr.request( 'favorites.getList', :user_id => '123456789N00', :per_page => 4 ).to_s.should == response.to_s
    @flickr.request( 'favorites.getList', :user_id => '123456789N00', :per_page => 4 ).to_s.should == response.to_s
  end
  
  it 'should request a user passing self in as the client' do
    user = mock('flickr user')
    Flickr::Person.should_receive( :find ).with( 'ennoia', @flickr ).and_return( user )
    @flickr.person( 'ennoia' ).should == user
  end
 
  it 'should request a photo passing self in as the client' do
    photo = mock('flickr photo')
    Flickr::Photo.should_receive( :find ).with( 'myphoto', @flickr ).and_return( photo )
    @flickr.photo( 'myphoto' ).should == photo
  end
end