mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Added compatibility: the response should mimic a net::response when in an exception
This commit is contained in:
parent
a683de6a1b
commit
ccb591958c
2 changed files with 35 additions and 4 deletions
|
@ -1,5 +1,17 @@
|
|||
module RestClient
|
||||
|
||||
# Compatibility : make the Response act like a Net::HTTPResponse when needed
|
||||
module ResponseForException
|
||||
def method_missing symbol, *args
|
||||
if net_http_res.respond_to? symbol
|
||||
warn "[warning] The response contained in an RestClient::Exception is now a RestClient::Response instead of a Net::HTTPResponse, please update your code"
|
||||
net_http_res.send symbol, *args
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# This is the base RestClient exception class. Rescue it if you want to
|
||||
# catch any exception that your request might raise
|
||||
# You can get the status code by e.http_code, or see anything about the
|
||||
|
@ -11,6 +23,9 @@ module RestClient
|
|||
|
||||
def initialize response = nil
|
||||
@response = response
|
||||
|
||||
# compatibility: this make the exception behave like a Net::HTTPResponse
|
||||
response.extend ResponseForException
|
||||
end
|
||||
|
||||
def http_code
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
require File.dirname(__FILE__) + '/base'
|
||||
|
||||
require 'webmock/rspec'
|
||||
include WebMock
|
||||
|
||||
describe RestClient::Exception do
|
||||
it "sets the exception message to ErrorMessage" do
|
||||
RestClient::ResourceNotFound.new.message.should == 'Resource Not Found'
|
||||
|
@ -17,10 +20,11 @@ describe RestClient::RequestFailed do
|
|||
end
|
||||
|
||||
it "stores the http response on the exception" do
|
||||
response = "response"
|
||||
begin
|
||||
raise RestClient::RequestFailed, :response
|
||||
raise RestClient::RequestFailed, response
|
||||
rescue RestClient::RequestFailed => e
|
||||
e.response.should == :response
|
||||
e.response.should == response
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -40,10 +44,11 @@ end
|
|||
|
||||
describe RestClient::ResourceNotFound do
|
||||
it "also has the http response attached" do
|
||||
response = "response"
|
||||
begin
|
||||
raise RestClient::ResourceNotFound, :response
|
||||
raise RestClient::ResourceNotFound, response
|
||||
rescue RestClient::ResourceNotFound => e
|
||||
e.response.should == :response
|
||||
e.response.should == response
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -60,4 +65,15 @@ describe "backwards compatibility" do
|
|||
it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
|
||||
RestClient::Request::RequestFailed.should == RestClient::RequestFailed
|
||||
end
|
||||
|
||||
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
|
||||
e.response.body.should == body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue