require File.dirname(__FILE__) + '/spec_helper.rb' require File.dirname(__FILE__) + '/../lib/thumbnail.rb' module SetupOpts def auth_opts {:access_key_id => "asdfsdfasdf", :secret_access_key => "asdfsadfasf"} end end module NoGo def stub_request Net::HTTP.stub!(:get) end end describe "Thumbnail::Client#new" do include SetupOpts it "should default to the thumbnail action" do t = Thumbnail::Client.new(auth_opts) t.action.should == :thumbnail end it "should set the action as redirect if specified" do t = Thumbnail::Client.new( auth_opts.merge(:action => :redirect) ) t.action.should == :redirect end it "should raise an error if the action is neither redirect nor thumbnail" do lambda{ Thumbnail::Client.new(auth_opts.merge(:action => :dogs) ) }.should raise_error(ArgumentError) end it "should raise an error if the access_key_id is not specified" do lambda{ Thumbnail::Client.new(:secret_access_key => "asdfsadfasf")}.should raise_error(ArgumentError) end it "should raise an error if the secret_access_key is not specified" do lambda{ Thumbnail::Client.new(:access_key_id => "asdfsadfasf")}.should raise_error(ArgumentError) end it "should default the size to large" do t = Thumbnail::Client.new(auth_opts) t.size.should == :large end it "should set the size to small if specified" do t = Thumbnail::Client.new( auth_opts.merge(:size => :small) ) t.size.should == :small end it "should raise an error if the size is neither large nor small" do lambda{ Thumbnail::Client.new(auth_opts.merge(:size => :largest) ) }.should raise_error(ArgumentError) end it "should raise an error if a default_image is specified for a Thumbnail action" do lambda{ Thumbnail::Client.new(auth_opts.merge(:default_image => "http://somewhere.com/my_pict.jpg")) }.should raise_error(ArgumentError) end end describe "Thumbnail::Client#new for a redirect" do include SetupOpts it "should set the default_image if specified" do t = Thumbnail::Client.new(auth_opts.merge(:action => :redirect, :default_image => "http://somewhere.com/my_pict.jpg")) t.instance_variable_get("@default_image").should == "http://somewhere.com/my_pict.jpg" end end describe "Thumbnail#query" do include SetupOpts it "should calculate the signature and timestamp" do t = Thumbnail::Client.new(auth_opts) t.should_receive(:calculate_signature_and_timestamp) t.send(:query) end end describe "Thumbnail#calculate_signature_and_timestamp" do include SetupOpts before do @t = Thumbnail::Client.new(auth_opts) end it "should calculate the signature" do Base64.should_receive(:encode64).and_return("se5wwsefaw435rawef=") @t.send(:calculate_signature_and_timestamp) end it "should set the (correctly formatted) timestamp" do Time.stub!(:now).and_return(Time.mktime(2007,7,1,12)) @t.send(:calculate_signature_and_timestamp) @t.instance_variable_get("@timestamp").should == "2007-07-01T19:00:00.000Z" end end describe "Thumbnail#query with thumbnail action and a single url" do include SetupOpts before do @t = Thumbnail::Client.new(auth_opts) @t.instance_variable_set("@url", "www.boingboing.net") end it "should compose a thumbnail single query" do @t.should_receive(:thumbnail_query_single) @t.send(:query) end end describe "Thumbnail::Client#query with thumbnail action and an array of urls" do include SetupOpts before do @t = Thumbnail::Client.new(auth_opts) @t.instance_variable_set("@url", ["www.boingboing.net", "www.craphound.com"]) end it "should compose a thumbnail batch query" do @t.should_receive(:thumbnail_query_batch) @t.send(:query) end end describe "Thumbnail::Client#query with redirect action" do include SetupOpts before do @t = Thumbnail::Client.new( auth_opts.merge(:action => :redirect) ) @t.instance_variable_set("@url", "www.boingboing.net") end it "should compose a redirect query" do @t.should_receive(:redirect_query) @t.send(:query) end end describe "Thumbnail::Client#thumbnail_query_single" do include SetupOpts before do Time.stub!(:now).and_return(Time.mktime(2007,7,1,15)) @t = Thumbnail::Client.new( auth_opts ) @t.send(:calculate_signature_and_timestamp) end it "should compose the query url correctly" do @t.instance_variable_set("@url", "www.boingboing.net") @t.send(:thumbnail_query_single).should == "http://ast.amazonaws.com/?Action=Thumbnail&AWSAccessKeyId=#{auth_opts[:access_key_id]}&Signature=f2g%2BLOp8T1ZfWLEip7l4RXiPQkI%3D&Timestamp=2007-07-01T22:00:00.000Z&Url=www.boingboing.net&Size=Large" end end describe "Thumbnail::Client#thumbnail_query_batch" do include SetupOpts before do Time.stub!(:now).and_return(Time.mktime(2007,7,1,15)) @t = Thumbnail::Client.new( auth_opts ) @t.send(:calculate_signature_and_timestamp) end it "should compose the query url correctly" do @t.instance_variable_set("@url", ["www.boingboing.net", "www.craphound.com"]) @t.send(:thumbnail_query_batch).should == "http://ast.amazonaws.com/?Action=Thumbnail&AWSAccessKeyId=#{auth_opts[:access_key_id]}&Signature=f2g%2BLOp8T1ZfWLEip7l4RXiPQkI%3D&Timestamp=2007-07-01T22:00:00.000Z&Shared.Size=Large&Thumbnail.1.Url=www.boingboing.net&Thumbnail.2.Url=www.craphound.com" end end describe "Thumbnail::Client#redirect_query with a default_image" do include SetupOpts before do Time.stub!(:now).and_return(Time.mktime(2007,7,1,15)) @t = Thumbnail::Client.new( auth_opts.merge(:action => :redirect, :default_image => "http://somewhere.com/my_pict.jpg") ) @t.send(:calculate_signature_and_timestamp) end it "should compose the query url correctly" do @t.instance_variable_set("@url", "www.boingboing.net") @t.send(:redirect_query).should == "http://ast.amazonaws.com/?Action=Redirect&AWSAccessKeyId=#{auth_opts[:access_key_id]}&Signature=%2FceiRH2lULi0rTvaY0mmJhxIwa4%3D&Timestamp=2007-07-01T22:00:00.000Z&Url=www.boingboing.net&Size=Large&DefaultImage=http://somewhere.com/my_pict.jpg" end end describe "Thumbnail::Client#redirect_query without a default_image" do include SetupOpts before do Time.stub!(:now).and_return(Time.mktime(2007,7,1,15)) @t = Thumbnail::Client.new( auth_opts.merge(:action => :redirect) ) @t.send(:calculate_signature_and_timestamp) end it "should compose the query url correctly" do @t.instance_variable_set("@url", "www.boingboing.net") @t.send(:redirect_query).should == "http://ast.amazonaws.com/?Action=Redirect&AWSAccessKeyId=#{auth_opts[:access_key_id]}&Signature=%2FceiRH2lULi0rTvaY0mmJhxIwa4%3D&Timestamp=2007-07-01T22:00:00.000Z&Url=www.boingboing.net&Size=Large" end end describe "Thumbnail::Client#get with a thumbnail action" do include SetupOpts include NoGo before do stub_request Thumbnail::Response.stub!(:build) @t = Thumbnail::Client.new(auth_opts) @t.stub!(:query).and_return(@q = "http://aws.com") @pq = mock('URI::HTTP') @pq.stub!(:host).and_return('aws.com') URI.stub!(:parse).and_return(@pq) Net::HTTP.stub!(:get).and_return(@xml = mock('xmldoc')) end it "should set the url string if given a single url" do @t.get("www.boingboing.net") @t.instance_variable_get("@url").should == "www.boingboing.net" end it "should set the url array if given an array of urls" do @t.get(["www.boingboing.net", "www.craphound.com"]) @t.instance_variable_get("@url").should == ["www.boingboing.net", "www.craphound.com"] end it "should parse the url before conducting the query" do URI.should_receive(:parse).with(@q).and_return(@pq) @t.get("www.craphound.com") end it "should use net/https to conduct the query" do Net::HTTP.should_receive(:get).with(@pq) @t.get("www.craphound.com") end it "should build a response object from the xml" do Thumbnail::Response.should_receive(:build).with(@xml) @t.get("www.craphound.com") end end describe "Thumbnail::Client#get with one or more invalid urls" do include SetupOpts before do @t = Thumbnail::Client.new(auth_opts) end it "should raise an error with one url that is invalid" do lambda{ @t.get("89u--=sdf-://as-df-") }.should raise_error(URI::InvalidURIError) end it "should raise an error if any of the urls in its give array are invalid" do lambda{ @t.get(["89u--=s:?df-as-df-", "www.google.com"]) }.should raise_error(URI::InvalidURIError) end end describe "Thumbnail::Client#get with a redirect action" do include SetupOpts include NoGo before do stub_request @t = Thumbnail::Client.new(auth_opts.merge(:action => :redirect)) end it "should raise an error if given an array of urls" do lambda{ @t.get(["www.boingboing.net", "www.craphound.com"]) }.should raise_error(ArgumentError) end it "should calculate the query url" do @t.should_receive(:query) @t.get("www.boingboing.net") end it "should return the redirect query" do @t.get("www.boingboing.net").should == @t.send(:redirect_query) end end