Skip to content

Commit

Permalink
Merge pull request #141 from yannrouillard/fix-140-relative_url_suppo…
Browse files Browse the repository at this point in the history
…rt_in_follow_redirect

Handle the 'relative url' case when follow_redirect is enabled
  • Loading branch information
tjarratt committed Feb 12, 2015
2 parents d6a3825 + 63b6148 commit 5749143
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/httpi.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "uri"
require "httpi/version"
require "httpi/logger"
require "httpi/request"
Expand Down Expand Up @@ -159,8 +160,8 @@ def request(method, request, adapter = nil)
response = adapter_class.request(method)

if response && HTTPI::Response::RedirectResponseCodes.member?(response.code) && request.follow_redirect?
log("Following redirect: '#{response.headers['location']}'.")
request.url = response.headers['location']
request.url = URI.join(request.url, response.headers['location'])
log("Following redirect: '#{request.url}'.")
return request(method, request, adapter)
end

Expand Down
30 changes: 28 additions & 2 deletions spec/httpi/httpi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
end

describe ".request" do
let(:request) { HTTPI::Request.new('http://example.com') }
let(:request) { HTTPI::Request.new('http://example.com/foo/') }

it "allows custom HTTP methods" do
httpclient.any_instance.expects(:request).with(:custom)
Expand All @@ -238,7 +238,33 @@
response = HTTPI::Response.new(200, {}, 'success')

httpclient.any_instance.expects(:request).twice.with(:custom).returns(redirect, response)
request.expects(:url=).with(redirect_location)
request.expects(:url=).with(URI.parse(redirect_location))

client.request(:custom, request, :httpclient)
end

it 'follows redirects with absolute path' do
request.follow_redirect = true
redirect_location = '/bar/foo'

redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
response = HTTPI::Response.new(200, {}, 'success')

httpclient.any_instance.expects(:request).twice.with(:custom).returns(redirect, response)
request.expects(:url=).with(URI.parse('http://example.com/bar/foo'))

client.request(:custom, request, :httpclient)
end

it 'follows redirects with relative path' do
request.follow_redirect = true
redirect_location = 'bar/foo'

redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
response = HTTPI::Response.new(200, {}, 'success')

httpclient.any_instance.expects(:request).twice.with(:custom).returns(redirect, response)
request.expects(:url=).with(URI.parse('http://example.com/foo/bar/foo'))

client.request(:custom, request, :httpclient)
end
Expand Down

0 comments on commit 5749143

Please sign in to comment.