Skip to content

Commit

Permalink
Merge aee0d6e into c7934aa
Browse files Browse the repository at this point in the history
  • Loading branch information
sobrinho committed Aug 15, 2015
2 parents c7934aa + aee0d6e commit 7733064
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/httpi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,18 @@ def delete(request, adapter = nil, &block)
end

# Executes an HTTP request for the given +method+.
def request(method, request, adapter = nil)
def request(method, request, adapter = nil, redirects = 0)
adapter_class = load_adapter(adapter, request)

yield adapter_class.client if block_given?
log_request(method, request, Adapter.identify(adapter_class.class))

response = adapter_class.request(method)

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

response
Expand Down
9 changes: 8 additions & 1 deletion lib/httpi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module HTTPI
class Request

# Available attribute writers.
ATTRIBUTES = [:url, :proxy, :headers, :body, :open_timeout, :read_timeout, :follow_redirect, :query]
ATTRIBUTES = [:url, :proxy, :headers, :body, :open_timeout, :read_timeout, :follow_redirect, :redirect_limit, :query]

# Accepts a Hash of +args+ to mass assign attributes and authentication credentials.
def initialize(args = {})
Expand Down Expand Up @@ -130,6 +130,13 @@ def follow_redirect?
@follow_redirect ||= false
end

attr_writer :redirect_limit

# Returns how many redirects should be followed - defaults to 3 if not set.
def redirect_limit
@redirect_limit ||= 3
end

private

# Stores the cookies from past requests.
Expand Down
14 changes: 14 additions & 0 deletions spec/httpi/httpi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@

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

it 'follows redirects at maximum of the redirect limit' do
request.follow_redirect = true
request.redirect_limit = 2
redirect_location = 'http://foo.bar'

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

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

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

HTTPI::REQUEST_METHODS.each do |method|
Expand Down

0 comments on commit 7733064

Please sign in to comment.