diff --git a/lib/restclient/components.rb b/lib/restclient/components.rb index 5f698cd..4dd391e 100644 --- a/lib/restclient/components.rb +++ b/lib/restclient/components.rb @@ -213,14 +213,18 @@ def to_hash env['restclient.hash'][:error] = e response = e.response end - # to satisfy Rack::Lint - response.headers.delete(:status) - header = RestClient.debeautify_headers( response.headers ) - body = response.to_s - # return the real content-length since RestClient does not do it when - # decoding gzip responses - header['Content-Length'] = body.length.to_s if header.has_key?('Content-Length') - [response.code, header, [body]] + if response.is_a?(RestClient::Response) + # to satisfy Rack::Lint + response.headers.delete(:status) + header = RestClient.debeautify_headers( response.headers ) + body = response.to_s + # return the real content-length since RestClient does not do it when + # decoding gzip responses + header['Content-Length'] = body.length.to_s if header.has_key?('Content-Length') + [response.code, header, [body]] + else + response + end } end diff --git a/spec/components_spec.rb b/spec/components_spec.rb index 2f4858c..cfe58f8 100644 --- a/spec/components_spec.rb +++ b/spec/components_spec.rb @@ -84,6 +84,16 @@ def call(env) RestClient.post "http://server.ltd/resource", 'some stupid message', :content_type => "text/plain", :content_length => 19 end + it "should follow redirects" do + #see https://github.com/crohr/rest-client-components/issues/17 + stub_request(:get, "http://server.ltd/resource").to_return(:status => 302, :body => "", :headers => {'Location' => "http://server.ltd/resource/"}) + stub_request(:get, "http://server.ltd/resource/").to_return(:status => 200, :body => "body", :headers => {'Content-Type' => 'text/plain', 'Content-Length' => 4}) + response = RestClient.get "http://server.ltd/resource" + response.code.should == 200 + response.headers.should == {:content_type=>"text/plain", :content_length =>"4"} + response.body.should == "body" + end + describe "and another component" do before do class AnotherRackMiddleware @@ -163,6 +173,17 @@ def call(env) body.each{|block| content << block} content.should == "body" end + it "should follow redirects" do + #see https://github.com/crohr/rest-client-components/issues/17 + stub_request(:get, "http://server.ltd/resource").to_return(:status => 302, :body => "", :headers => {'Location' => "http://server.ltd/resource/"}) + stub_request(:get, "http://server.ltd/resource/").to_return(:status => 200, :body => "body", :headers => {'Content-Type' => 'text/plain', 'Content-Length' => 4}) + status, headers, body = RestClient.get "http://server.ltd/resource" + status.should == 200 + headers.should == {"Content-Type"=>"text/plain", "Content-Length"=>"4"} + content = "" + body.each{|block| content << block} + content.should == "body" + end it "should not raise ExceptionWithResponse exceptions" do stub_request(:get, "http://server.ltd/resource").to_return(:status => 404, :body => "body", :headers => {'Content-Type' => 'text/plain', 'Content-Length' => 4}) status, headers, body = RestClient.get "http://server.ltd/resource"