require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe Trigga::AdminApiClient do before do Trigga.stub!(:add_log_line).and_return(true) # dont need logging info outputted end it "should include Trigga::ParamFu" do Trigga::AdminApiClient.should include Trigga::ParamFu end it "should include Admin proxy" do Trigga::AdminApiClient.should include Trigga::AdminApiClient::Proxies::Admin end it "should include API proxy" do Trigga::AdminApiClient.should include Trigga::AdminApiClient::Proxies::Api end describe "make_call" do before do TRIGGA_ADMIN_API_CLIENT_CONFIG = {"test" => Hashie::Mash.new(:base_url => "base_url")} Trigga::AdminApiClient.stub!(:require_param).and_return(true) Trigga::AdminApiClient.stub!(:build_url).and_return("a url") Itrigga::NetHelper.stub!(:do_get).and_return({:response => "text"}.to_json) @opts = {:api_key => "1234", :abc => 123} end it "should call require_param for api_key" do Trigga::AdminApiClient.should_receive(:require_param).with(@opts, :api_key).and_return(true) Trigga::AdminApiClient.make_call("test","stats",@opts) end it "should call build_url with correct params" do Trigga::AdminApiClient.should_receive(:build_url).with("http://base_url","stats",@opts).and_return("a url") Trigga::AdminApiClient.make_call("test","stats",@opts) end it "should convert the url to absolute" do Trigga::AdminApiClient.should_receive(:make_absolute_url).with("base_url").and_return("http://base_url") Trigga::AdminApiClient.make_call("test","stats",@opts) end it "should override the :host" do Trigga::AdminApiClient.should_receive(:make_absolute_url).with("new_base_url").and_return("http://new_base_url") Trigga::AdminApiClient.make_call("test","stats",@opts.merge(:host => "new_base_url")) end it "should call net_helper with the url" do Itrigga::NetHelper.should_receive(:do_get).with("a url").and_return({:response => "text"}.to_json) Trigga::AdminApiClient.make_call("test","stats",@opts) end it "should return a mash with the status code" do Trigga::AdminApiClient.make_call("test","stats",@opts).should == Hashie::Mash.new(:response => "text", :status_code => 200) end describe "when an error occurs" do before do Itrigga::NetHelper.stub!(:do_get).and_raise("500 An error") end it "should return an error mash when errord out" do Trigga::AdminApiClient.make_call("test","stats",@opts).should == Hashie::Mash.new(:error => "500 An error", :status_code => 500 ) end it "should call status_code_from_error" do Trigga::AdminApiClient.should_receive(:status_code_from_error).with("500 An error").and_return(500) Trigga::AdminApiClient.make_call("test","stats",@opts) end end end describe "make_endpoint_absolute" do it "should add a /" do Trigga::AdminApiClient.make_endpoint_absolute("stats").should == "/stats" end it "should not add an extra /" do Trigga::AdminApiClient.make_endpoint_absolute("/stats").should == "/stats" end end describe "build_url" do it "should return the correct url" do Trigga::AdminApiClient.build_url("http://site.com","stats",:abc => 123).should == "http://site.com/stats.json?abc=123" end it "should use the given format" do Trigga::AdminApiClient.build_url("http://site.com","stats",:abc => 123,:format => "xml").should == "http://site.com/stats.xml?abc=123" end end describe "status_code_from_error" do it "should return the status code" do Trigga::AdminApiClient.status_code_from_error("404 I got lost").should == 404 end it "should return 0 when no status" do Trigga::AdminApiClient.status_code_from_error("I got lost").should == 0 end end end