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

Move max_redirects check into Response#return!

Previous refactoring would have broken the ability to call
`.follow_redirection` on a redirection response exception like
RestClient::Found. Instead, raise the exception at the time that the
response is first prepared.
This commit is contained in:
Andy Brody 2016-06-05 18:39:31 -04:00
parent 19cbd576a9
commit 00e12b35c2
3 changed files with 26 additions and 3 deletions

View file

@ -82,11 +82,13 @@ module RestClient
when 301, 302, 307
case request.method
when 'get', 'head'
check_max_redirects
follow_redirection(&block)
else
raise exception_with_response
end
when 303
check_max_redirects
follow_get_redirection(&block)
else
raise exception_with_response
@ -178,9 +180,6 @@ module RestClient
end
new_args[:url] = url
if request.max_redirects <= 0
raise exception_with_response
end
new_args[:password] = request.password
new_args[:user] = request.user
new_args[:headers] = request.headers
@ -201,6 +200,12 @@ module RestClient
new_req.execute(&block)
end
def check_max_redirects
if request.max_redirects <= 0
raise exception_with_response
end
end
def exception_with_response
begin
klass = Exceptions::EXCEPTIONS_MAP.fetch(code)

View file

@ -128,6 +128,7 @@ describe RestClient::AbstractResponse, :include_helpers do
it "should follow 302 redirect" do
@net_http_res.should_receive(:code).and_return('302')
@response.should_receive(:check_max_redirects).and_return('fake-check')
@response.should_receive(:follow_redirection).and_return('fake-redirection')
@response.return!.should eq 'fake-redirection'
end
@ -136,6 +137,7 @@ describe RestClient::AbstractResponse, :include_helpers do
@net_http_res = response_double(code: 302, location: nil)
@request = request_double()
@response = MyAbstractResponse.new(@net_http_res, @request)
@response.should_receive(:check_max_redirects).and_return('fake-check')
lambda { @response.return! }.should raise_error RestClient::Found
end
end

View file

@ -216,6 +216,22 @@ describe RestClient::Response, :include_helpers do
}
WebMock.should have_requested(:get, 'http://some/redirect-2').times(5)
end
it "allows for manual following of redirects" do
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/resource'})
stub_request(:get, 'http://some/resource').to_return(:body => 'Qux', :status => 200)
begin
RestClient::Request.execute(url: 'http://some/redirect-1', method: :get, max_redirects: 0)
rescue RestClient::MovedPermanently => err
resp = err.response.follow_redirection
else
raise 'notreached'
end
resp.code.should eq 200
resp.body.should eq 'Qux'
end
end
end