mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Don't attempt to follow redirection w/o Location.
When there is no Location header, we cannot follow any redirection. Per RFC 7231, a server SHOULD generate a Location header field in the response for redirection requests, but servers do not always do this. https://tools.ietf.org/html/rfc7231#section-6.4.3 Fixes: #481
This commit is contained in:
parent
b44ee2c44b
commit
28f287cfb2
2 changed files with 12 additions and 0 deletions
|
@ -167,6 +167,11 @@ module RestClient
|
||||||
# parse location header and merge into existing URL
|
# parse location header and merge into existing URL
|
||||||
url = headers[:location]
|
url = headers[:location]
|
||||||
|
|
||||||
|
# cannot follow redirection if there is no location header
|
||||||
|
unless url
|
||||||
|
raise exception_with_response
|
||||||
|
end
|
||||||
|
|
||||||
# handle relative redirects
|
# handle relative redirects
|
||||||
unless url.start_with?('http')
|
unless url.start_with?('http')
|
||||||
url = URI.parse(request.url).merge(url).to_s
|
url = URI.parse(request.url).merge(url).to_s
|
||||||
|
|
|
@ -104,5 +104,12 @@ describe RestClient::AbstractResponse, :include_helpers do
|
||||||
@response.should_receive(:follow_redirection).and_return('fake-redirection')
|
@response.should_receive(:follow_redirection).and_return('fake-redirection')
|
||||||
@response.return!.should eq 'fake-redirection'
|
@response.return!.should eq 'fake-redirection'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should gracefully handle 302 redirect with no location header" do
|
||||||
|
@net_http_res = response_double(code: 302, location: nil)
|
||||||
|
@request = request_double()
|
||||||
|
@response = MyAbstractResponse.new(@net_http_res, @request)
|
||||||
|
lambda { @response.return! }.should raise_error RestClient::Found
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue