Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle redirects when RestClient::Rack::Compatibility is disabled #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions lib/restclient/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions spec/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down