1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Only redirect when location is present

This commit is contained in:
Sandro Turriate 2010-01-27 02:08:07 -05:00
parent 0f606b2402
commit 9a35f6cd2c
2 changed files with 26 additions and 6 deletions

View file

@ -141,6 +141,7 @@ module HTTParty
Net::HTTPSeeOther, # 303
Net::HTTPUseProxy, # 305
Net::HTTPTemporaryRedirect
if response.key?('location')
options[:limit] -= 1
self.path = response['location']
@redirect = true
@ -148,8 +149,11 @@ module HTTParty
capture_cookies(response)
perform
else
Response.new(parse_response(response.body), response.body, response.code, response.message, response.to_hash)
response
end
else
Response.new(parse_response(response.body), response.body, response.code, response.message, response.to_hash)
end
end
def parse_response(body)

View file

@ -232,11 +232,27 @@ describe HTTParty::Request do
end
describe 'with non-200 responses' do
it 'should return a valid object for 304 not modified' do
stub_response '', 304
resp = @request.perform
resp.code.should == 304
resp.body.should == "" # or nil?
context "3xx responses" do
it 'returns a valid object for 304 not modified' do
stub_response '', 304
resp = @request.perform
resp.code.should == 304
resp.body.should == ''
resp.should be_nil
end
it "redirects if a 300 contains a location header" do
redirect = stub_response '', 300
redirect['location'] = 'http://foo.com/foo'
ok = stub_response('<hash><foo>bar</foo></hash>', 200)
@http.stub!(:request).and_return(redirect, ok)
@request.perform.should == {"hash" => {"foo" => "bar"}}
end
it "returns the Net::HTTP response if the 300 does not contain a location header" do
net_response = stub_response '', 300
@request.perform.should be_kind_of(Net::HTTPMultipleChoice)
end
end
it 'should return a valid object for 4xx response' do