Permalink
Browse files

Added a bunch of code which adds a follow_location option to Typhoeus…

…::Request

objects. This really just involved adding an accessor to Request objects, and
making sure it gets passed through to the Easy objects that actually perform the
request.

* Added action to the test server which always returns a redirect. Intended
for testing the follow_location option.

* Implemented couple of specs for existing functionality to verify that
the follow_location option on Typhoeus::Easy does indeed cause Typhoeus
to follow redirects.

* Added follow_location option to Typhoeus::Request.

* Added code to retrieve the follow_location setting on a Typhoeus::Request,
and pass it through to the Typhoeus::Easy object actually performing the
request, plus a simple spec to just verify that the option is being set properly
when set on a Request queued up in Hydra.
  • Loading branch information...
1 parent 96554eb commit 7aebf71b72ed37cd736428d54488f6a9070356bb @smulube smulube committed Nov 16, 2009
@@ -122,6 +122,7 @@ def get_easy_object(request)
easy.headers = request.headers if request.headers
easy.request_body = request.body if request.body
easy.timeout = request.timeout if request.timeout
+ easy.follow_location = request.follow_location if request.follow_location
easy.on_success do |easy|
queue_next
handle_request(request, response_from_easy(easy, request))
@@ -1,6 +1,6 @@
module Typhoeus
class Request
- attr_accessor :method, :params, :body, :headers, :timeout, :user_agent, :response, :cache_timeout
+ attr_accessor :method, :params, :body, :headers, :timeout, :user_agent, :response, :cache_timeout, :follow_location
attr_reader :url
def initialize(url, options = {})
@@ -11,6 +11,7 @@ def initialize(url, options = {})
@headers = options[:headers] || {}
@user_agent = options[:user_agent] || Typhoeus::USER_AGENT
@cache_timeout = options[:cache_timeout]
+ @follow_location = options[:follow_location]
if @method == :post
@url = url
else
@@ -112,4 +113,4 @@ def self.delete(url, params = {})
run(url, params.merge(:method => :delete))
end
end
-end
+end
View
@@ -17,6 +17,10 @@
error 500, "oh noes!"
end
+get '/redirect' do
+ redirect '/'
+end
+
get '/**' do
sleep params["delay"].to_i if params.has_key?("delay")
request.env.merge!(:body => request.body.read).to_json
@@ -2,8 +2,24 @@
describe Typhoeus::Easy do
describe "options" do
- it "should allow for following redirects"
- it "should allow you to set the user agent"
+ it "should not follow redirects if not instructed to" do
+ e = Typhoeus::Easy.new
+ e.url = "http://localhost:3001/redirect"
+ e.method = :get
+ e.perform
+ e.response_code.should == 302
+ end
+
+ it "should allow for following redirects" do
+ e = Typhoeus::Easy.new
+ e.url = "http://localhost:3001/redirect"
+ e.method = :get
+ e.follow_location = true
+ e.perform
+ e.response_code.should == 200
+ JSON.parse(e.response_body)["REQUEST_METHOD"].should == "GET"
+ end
+
it "should provide a timeout in milliseconds" do
e = Typhoeus::Easy.new
e.url = "http://localhost:3001"
@@ -174,7 +174,7 @@ def set(key, object, timeout = 0)
foo.should == :called
end
- it "has a global on_omplete setter" do
+ it "has a global on_complete setter" do
foo = nil
hydra = Typhoeus::Hydra.new
proc = Proc.new {|response| foo = :called}
@@ -238,7 +238,7 @@ def set(key, object, timeout = 0)
sleep 3 # have to do this or future tests may break.
end
- it "should take the maximum number of concurrent reqeusts as an argument" do
+ it "should take the maximum number of concurrent requests as an argument" do
hydra = Typhoeus::Hydra.new(:max_concurrency => 2)
first = Typhoeus::Request.new("http://localhost:3000/first?delay=1")
second = Typhoeus::Request.new("http://localhost:3001/second?delay=1")
@@ -256,4 +256,13 @@ def set(key, object, timeout = 0)
third.response.code.should == 200
(finish_time - start_time).should > 2.0
end
+
+ it "should respect the follow_location option when set on a request" do
+ hydra = Typhoeus::Hydra.new
+ request = Typhoeus::Request.new("http://localhost:3000/redirect", :follow_location => true)
+ hydra.queue request
+ hydra.run
+
+ request.response.code.should == 200
+ end
end
@@ -65,6 +65,10 @@
Typhoeus::Request.new("http://localhost:3000", :cache_timeout => 60).cache_timeout.should == 60
end
+ it "takes follow_location as an option" do
+ Typhoeus::Request.new("http://localhost:3000", :follow_location => true).follow_location.should == true
+ end
+
it "has the associated response object" do
request = Typhoeus::Request.new("http://localhost:3000")
request.response = :foo
@@ -133,4 +137,4 @@
it "should count the number of times a request has failed"
end
-end
+end

0 comments on commit 7aebf71

Please sign in to comment.