diff --git a/lib/restclient/abstract_response.rb b/lib/restclient/abstract_response.rb index a22b4e2..f0aed01 100644 --- a/lib/restclient/abstract_response.rb +++ b/lib/restclient/abstract_response.rb @@ -167,6 +167,11 @@ module RestClient # parse location header and merge into existing URL url = headers[:location] + # cannot follow redirection if there is no location header + unless url + raise exception_with_response + end + # handle relative redirects unless url.start_with?('http') url = URI.parse(request.url).merge(url).to_s diff --git a/spec/unit/abstract_response_spec.rb b/spec/unit/abstract_response_spec.rb index 06494f9..7ffd10c 100644 --- a/spec/unit/abstract_response_spec.rb +++ b/spec/unit/abstract_response_spec.rb @@ -104,5 +104,12 @@ describe RestClient::AbstractResponse, :include_helpers do @response.should_receive(:follow_redirection).and_return('fake-redirection') @response.return!.should eq 'fake-redirection' 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