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

Fixes #67 - Follow HTTP redirects. #117

Merged
merged 1 commit into from
Jun 26, 2014
Merged
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
10 changes: 9 additions & 1 deletion lib/httpi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,15 @@ def request(method, request, adapter = nil)
yield adapter_class.client if block_given?
log_request(method, request, Adapter.identify(adapter_class.class))

adapter_class.request(method)
response = adapter_class.request(method)

if response and response.code == 302 and request.follow_redirect?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should check for [301, 302, 303, 307, 308]. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection

Agree @tjarratt and @mikeantonelli?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely agree.

log('Following redirect...')
request.url = response.headers['location']
return request(method, request, adapter)
end

response
end

# Shortcut for setting the default adapter to use.
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]
ATTRIBUTES = [:url, :proxy, :headers, :body, :open_timeout, :read_timeout, :follow_redirect]

# Accepts a Hash of +args+ to mass assign attributes and authentication credentials.
def initialize(args = {})
Expand Down Expand Up @@ -123,6 +123,13 @@ def mass_assign(args)
ATTRIBUTES.each { |key| send("#{key}=", args[key]) if args[key] }
end

attr_writer :follow_redirect

# Returns whether or not redirects should be followed - defaults to false if not set.
def follow_redirect?
@follow_redirect ||= false
end

private

# Stores the cookies from past requests.
Expand Down
18 changes: 16 additions & 2 deletions spec/httpi/httpi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,26 @@
end

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

it "allows custom HTTP methods" do
request = HTTPI::Request.new("http://example.com")
httpclient.any_instance.expects(:request).with(:custom)

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

it 'follows redirects' do
request.follow_redirect = true
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).twice.with(:custom).returns(redirect, response)
request.expects(:url=).with(redirect_location)

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

HTTPI::REQUEST_METHODS.each do |method|
Expand Down Expand Up @@ -249,7 +263,7 @@

describe ".log" do
it "defaults to true" do
expect(HTTPI.log?).to be_true
expect(HTTPI.log?).to be_truthy
end
end

Expand Down
15 changes: 13 additions & 2 deletions spec/httpi/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,22 @@ def response_with_cookie(cookie)
describe "#auth?" do
it "returns true when auth credentials are specified" do
request.auth.basic "username", "password"
expect(request.auth?).to be_true
expect(request.auth?).to be_truthy
end

it "returns false otherwise" do
expect(request.auth?).to be_false
expect(request.auth?).to be_falsey
end
end

describe '#follow_redirect?' do
it 'returns true when follow_redirect is set to true' do
request.follow_redirect = true
expect(request.follow_redirect?).to be_truthy
end

it 'returns false by default' do
expect(request.follow_redirect?).to be_falsey
end
end

Expand Down