1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

Add some testing for AbstractResponse#return! and fix the generic fallback exception that gets raised.

In some versions of Ruby, a parenthesis ambiguity warning would be emitted:
  [...] ruby/1.8/gems/rest-client-1.6.1/lib/restclient/abstract_response.rb:50: warning: parenthesize argument(s) for future version
Better to be unambiguous about the exception and include the response code too.
This commit is contained in:
Kyle VanderBeek 2010-12-10 15:11:57 -08:00
parent 2caea9b9f0
commit ccabff0e10
2 changed files with 20 additions and 2 deletions

View file

@ -31,7 +31,7 @@ module RestClient
# Return the default behavior corresponding to the response code:
# the response itself for code in 200..206, redirection for 301, 302 and 307 in get and head cases, redirection for 303 and an exception in other cases
def return! request = nil, result = nil, & block
def return! request = nil, result = nil, & block
if (200..207).include? code
self
elsif [301, 302, 307].include? code
@ -47,7 +47,7 @@ module RestClient
elsif Exceptions::EXCEPTIONS_MAP[code]
raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
else
raise RequestFailed self
raise RequestFailed.new(self, code)
end
end

View file

@ -64,4 +64,22 @@ describe RestClient::AbstractResponse do
it "can access the net http result directly" do
@response.net_http_res.should == @net_http_res
end
describe "#return!" do
it "should return the response itself on 200-codes" do
@net_http_res.should_receive(:code).and_return('200')
@response.return!.should be_equal(@response)
end
it "should raise RequestFailed on unknown codes" do
@net_http_res.should_receive(:code).and_return('1000')
lambda { @response.return! }.should raise_error RestClient::RequestFailed
end
it "should raise an error on a redirection after non-GET/HEAD requests" do
@net_http_res.should_receive(:code).and_return('301')
@response.args.merge(:method => :put)
lambda { @response.return! }.should raise_error RestClient::RequestFailed
end
end
end