require File.join(File.dirname(__FILE__), '..', 'spec_helper' ) describe "Itrigga::NetHelper::Typhoeus" do before do # define Typhoeus stubs as not installed as gem dependancy Object.class_eval %{ module Typhoeus class Request attr_accessor :on_complete def initialize(url,opts = {}); end def on_complete(&block); @on_complete = block; end end class Hydra attr_accessor :request def queue(request); end def run; end end end } @opts = {:url => "blart"} @response = mock("Response", :success? => true, :body => "stuff") @request = Typhoeus::Request.new("blart", @opts) # @request.stub!(:on_complete).and_yield(@response) Typhoeus::Request.stub!(:new).and_return(@request) @hydra = Typhoeus::Hydra.new Typhoeus::Hydra.stub!(:new).and_return(@hydra) end after do Object.send(:remove_const, :Typhoeus) end it "should create a Request object" do Typhoeus::Request.should_receive(:new).with("blart",@opts).and_return(@request) Itrigga::NetHelper::Typhoeus.get @opts end it "should call queue on hydra" do @hydra.should_receive(:queue).with(@request) Itrigga::NetHelper::Typhoeus.get(@opts) end it "should call run on hydra" do @hydra.should_receive(:run) Itrigga::NetHelper::Typhoeus.get(@opts) end it "should return the response body" do @request.stub!(:on_complete).and_yield(@response) Itrigga::NetHelper::Typhoeus.get(@opts).should == "stuff" end context "a timeout" do before do @response = mock("Response", :success? => false, :timed_out? => true, :body => "stuff") @request.stub!(:on_complete).and_yield(@response) end it "should raise a TimeoutError" do lambda { Itrigga::NetHelper::Typhoeus.get(@opts) }.should raise_error(TimeoutError) end end context "response code == 0" do before do @response = mock("Response", :success? => false, :timed_out? => false, :code => 0, :body => "stuff", :curl_error_message => "blart") @request.stub!(:on_complete).and_yield(@response) end it "should raise a TimeoutError" do lambda { Itrigga::NetHelper::Typhoeus.get(@opts) }.should raise_error(IOError) end end context "any other http status code" do before do @response = mock("Response", :success? => false, :timed_out? => false, :code => 500, :body => "stuff") @request.stub!(:on_complete).and_yield(@response) end it "should raise a TimeoutError" do lambda { Itrigga::NetHelper::Typhoeus.get(@opts) }.should raise_error(IOError) end end end