2015-03-13 21:00:51 -04:00
|
|
|
require_relative '_lib'
|
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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(e.message).to 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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(e.message).to 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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::ResourceNotFound.new.message).to eq '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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Unauthorized.new).to be_a_kind_of(RestClient::Exception)
|
|
|
|
expect(RestClient::ServerBrokeConnection.new).to be_a_kind_of(RestClient::Exception)
|
2009-12-29 12:27:39 -05:00
|
|
|
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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(e.message).to 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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(e.response).to 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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::RequestFailed.new(@response).http_code).to 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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::RequestFailed.new(@response).http_code).to eq 502
|
|
|
|
expect(RestClient::RequestFailed.new(@response).message).to 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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::RequestFailed.new(@response).to_s).to match(/502/)
|
2009-12-29 12:27:39 -05:00
|
|
|
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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(e.response).to eq response
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
end
|
2010-02-06 16:32:52 -05:00
|
|
|
|
2015-04-15 19:27:03 -04:00
|
|
|
it 'stores the body on the response of the exception' do
|
2010-02-06 16:32:52 -05:00
|
|
|
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
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(e.response.body).to eq body
|
2010-02-06 16:32:52 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-12 14:21:39 -04:00
|
|
|
end
|
2015-04-15 19:29:11 -04:00
|
|
|
|
|
|
|
describe "backwards compatibility" do
|
|
|
|
it 'aliases RestClient::NotFound as ResourceNotFound' do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::ResourceNotFound).to eq RestClient::NotFound
|
2015-04-15 19:29:11 -04:00
|
|
|
end
|
2015-09-09 23:54:59 -04:00
|
|
|
|
2015-11-11 20:20:48 -05:00
|
|
|
it 'aliases old names for HTTP 413, 414, 416' do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::RequestEntityTooLarge).to eq RestClient::PayloadTooLarge
|
|
|
|
expect(RestClient::RequestURITooLong).to eq RestClient::URITooLong
|
|
|
|
expect(RestClient::RequestedRangeNotSatisfiable).to eq RestClient::RangeNotSatisfiable
|
2015-11-11 20:20:48 -05:00
|
|
|
end
|
|
|
|
|
2015-09-09 23:54:59 -04:00
|
|
|
it 'subclasses NotFound from RequestFailed, ExceptionWithResponse' do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::NotFound).to be < RestClient::RequestFailed
|
|
|
|
expect(RestClient::NotFound).to be < RestClient::ExceptionWithResponse
|
2015-09-09 23:54:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'subclasses timeout from RestClient::RequestTimeout, RequestFailed, EWR' do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Exceptions::OpenTimeout).to be < RestClient::Exceptions::Timeout
|
|
|
|
expect(RestClient::Exceptions::ReadTimeout).to be < RestClient::Exceptions::Timeout
|
2015-09-09 23:54:59 -04:00
|
|
|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Exceptions::Timeout).to be < RestClient::RequestTimeout
|
|
|
|
expect(RestClient::Exceptions::Timeout).to be < RestClient::RequestFailed
|
|
|
|
expect(RestClient::Exceptions::Timeout).to be < RestClient::ExceptionWithResponse
|
2015-09-09 23:54:59 -04:00
|
|
|
end
|
|
|
|
|
2015-04-15 19:29:11 -04:00
|
|
|
end
|