2013-08-06 11:44:47 -04:00
|
|
|
require 'spec_helper'
|
2010-02-06 16:32:52 -05:00
|
|
|
|
2008-06-25 22:02:02 -04:00
|
|
|
describe RestClient::Exception do
|
2010-11-20 11:50:10 -05:00
|
|
|
it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do
|
|
|
|
e = RestClient::Exception.new
|
2013-08-11 16:16:02 -04:00
|
|
|
e.message.should eq "RestClient::Exception"
|
2010-11-20 11:50:10 -05:00
|
|
|
end
|
2013-07-31 22:24:18 -04:00
|
|
|
|
2010-11-20 11:50:10 -05:00
|
|
|
it "returns the 'message' that was set" do
|
|
|
|
e = RestClient::Exception.new
|
|
|
|
message = "An explicitly set message"
|
|
|
|
e.message = message
|
2013-08-11 16:16:02 -04:00
|
|
|
e.message.should eq message
|
2010-11-20 11:50:10 -05:00
|
|
|
end
|
2013-07-31 22:24:18 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "sets the exception message to ErrorMessage" do
|
2013-08-11 16:16:02 -04:00
|
|
|
RestClient::ResourceNotFound.new.message.should eq 'Resource Not Found'
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2008-06-25 22:02:02 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "contains exceptions in RestClient" do
|
|
|
|
RestClient::Unauthorized.new.should be_a_kind_of(RestClient::Exception)
|
|
|
|
RestClient::ServerBrokeConnection.new.should be_a_kind_of(RestClient::Exception)
|
|
|
|
end
|
2008-06-25 22:02:02 -04:00
|
|
|
end
|
|
|
|
|
2010-11-20 11:50:10 -05:00
|
|
|
describe RestClient::ServerBrokeConnection do
|
|
|
|
it "should have a default message of 'Server broke connection'" do
|
|
|
|
e = RestClient::ServerBrokeConnection.new
|
2013-08-11 16:16:02 -04:00
|
|
|
e.message.should eq 'Server broke connection'
|
2010-11-20 11:50:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-11 15:31:39 -04:00
|
|
|
describe RestClient::RequestFailed do
|
2009-12-29 12:27:39 -05:00
|
|
|
before do
|
2013-07-26 23:09:42 -04:00
|
|
|
@response = double('HTTP Response', :code => '502')
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-06-29 19:22:54 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "stores the http response on the exception" do
|
2010-02-06 16:32:52 -05:00
|
|
|
response = "response"
|
2009-12-29 12:27:39 -05:00
|
|
|
begin
|
2010-02-06 16:32:52 -05:00
|
|
|
raise RestClient::RequestFailed, response
|
2009-12-29 12:27:39 -05:00
|
|
|
rescue RestClient::RequestFailed => e
|
2013-08-11 16:16:02 -04:00
|
|
|
e.response.should eq response
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-11 15:31:39 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "http_code convenience method for fetching the code as an integer" do
|
2013-08-11 16:16:02 -04:00
|
|
|
RestClient::RequestFailed.new(@response).http_code.should eq 502
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-06-29 19:22:54 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "http_body convenience method for fetching the body (decoding when necessary)" do
|
2013-08-11 16:16:02 -04:00
|
|
|
RestClient::RequestFailed.new(@response).http_code.should eq 502
|
|
|
|
RestClient::RequestFailed.new(@response).message.should eq 'HTTP status code 502'
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2008-06-11 15:31:39 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "shows the status code in the message" do
|
|
|
|
RestClient::RequestFailed.new(@response).to_s.should match(/502/)
|
|
|
|
end
|
2008-06-12 14:21:39 -04:00
|
|
|
end
|
|
|
|
|
2008-11-26 20:42:16 -05:00
|
|
|
describe RestClient::ResourceNotFound do
|
2009-12-29 12:27:39 -05:00
|
|
|
it "also has the http response attached" do
|
2010-02-06 16:32:52 -05:00
|
|
|
response = "response"
|
2009-12-29 12:27:39 -05:00
|
|
|
begin
|
2010-02-06 16:32:52 -05:00
|
|
|
raise RestClient::ResourceNotFound, response
|
2009-12-29 12:27:39 -05:00
|
|
|
rescue RestClient::ResourceNotFound => e
|
2013-08-11 16:16:02 -04:00
|
|
|
e.response.should eq response
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
end
|
2008-11-26 20:42:16 -05:00
|
|
|
end
|
|
|
|
|
2008-06-12 14:21:39 -04:00
|
|
|
describe "backwards compatibility" do
|
2009-12-29 12:27:39 -05:00
|
|
|
it "alias RestClient::Request::Redirect to RestClient::Redirect" do
|
2013-08-11 16:16:02 -04:00
|
|
|
RestClient::Request::Redirect.should eq RestClient::Redirect
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2008-06-12 14:21:39 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do
|
2013-08-11 16:16:02 -04:00
|
|
|
RestClient::Request::Unauthorized.should eq RestClient::Unauthorized
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2008-06-12 14:21:39 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
|
2013-08-11 16:16:02 -04:00
|
|
|
RestClient::Request::RequestFailed.should eq RestClient::RequestFailed
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2010-02-06 16:32:52 -05:00
|
|
|
|
|
|
|
it "make the exception's response act like an Net::HTTPResponse" do
|
|
|
|
body = "body"
|
|
|
|
stub_request(:get, "www.example.com").to_return(:body => body, :status => 404)
|
|
|
|
begin
|
|
|
|
RestClient.get "www.example.com"
|
|
|
|
raise
|
|
|
|
rescue RestClient::ResourceNotFound => e
|
2013-08-11 16:16:02 -04:00
|
|
|
e.response.body.should eq body
|
2010-02-06 16:32:52 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-12 14:21:39 -04:00
|
|
|
end
|