spec/rapidash/http_client_spec.rb in rapidash-0.0.5 vs spec/rapidash/http_client_spec.rb in rapidash-0.0.6

- old
+ new

@@ -13,10 +13,16 @@ def self.url_extension :json end end +class HTTPErrorTester < HTTPTester + def self.raise_error + true + end +end + describe Rapidash::HTTPClient do let!(:subject) { HTTPTester.new } describe ".site=" do @@ -55,39 +61,64 @@ describe ".request" do let!(:valid_response) { OpenStruct.new(:status => "200")} let!(:redirect_response) { OpenStruct.new(:status => "301", :headers => {"location" => "http://example.com/redirect"})} + let!(:error_response) { OpenStruct.new(:status => "404")} before(:each) do subject.site = "http://example.com" - Rapidash::Response.should_receive(:new).and_return("response") end - it "should add an extension if one is set" do - subject.extension = :json - subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo.json", nil, nil).and_return(valid_response) - subject.request(:get, "foo") - end + describe "valid response" do - it "should use the class extension if one is set" do - subject = HTTPExtensionTester.new - subject.connection.should_receive(:run_request).with(:get, "http://mysite.com/foo.json", nil, nil).and_return(valid_response) - subject.request(:get, "foo") - end + before(:each) do + Rapidash::Response.should_receive(:new).and_return("response") + end + + it "should add an extension if one is set" do + subject.extension = :json + subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo.json", nil, nil).and_return(valid_response) + subject.request(:get, "foo") + end + it "should use the class extension if one is set" do + subject = HTTPExtensionTester.new + subject.connection.should_receive(:run_request).with(:get, "http://mysite.com/foo.json", nil, nil).and_return(valid_response) + subject.request(:get, "foo") + end - it "should return a response object" do - subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return(valid_response) - response = subject.request(:get, "foo") - response.should eql("response") + + it "should return a response object" do + subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return(valid_response) + response = subject.request(:get, "foo") + response.should eql("response") + end + + it "should perform a redirect" do + subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return(redirect_response) + subject.connection.should_receive(:run_request).with(:get, "http://example.com/redirect", nil, nil).and_return(valid_response) + response = subject.request(:get, "foo") + response.should eql("response") + end + end - it "should perform a redirect" do - subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return(redirect_response) - subject.connection.should_receive(:run_request).with(:get, "http://example.com/redirect", nil, nil).and_return(valid_response) - response = subject.request(:get, "foo") - response.should eql("response") + describe "error response" do + + it "should not raise an error by default" do + subject.connection.should_receive(:run_request).with(:get, "http://example.com/error", nil, nil).and_return(error_response) + response = subject.request(:get, "error") + response.should be_nil + end + + it "should raise an error if the option is set" do + subject = HTTPErrorTester.new + subject.connection.should_receive(:run_request).with(:get, anything, nil, nil).and_return(error_response) + expect { + response = subject.request(:get, "error") + }.to raise_error(Rapidash::ResponseError) + end + end end - end