Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add follow_redirect! to the API. Useful in Rails integration tests fo…

…r post/put/delete actions.
  • Loading branch information...
commit 5f1364259a4b31f52b2765e057c54d4779339f40 1 parent dd0c942
@josevalim josevalim authored indirect committed
View
2  lib/webrat/core/methods.rb
@@ -68,6 +68,6 @@ def response
:field_with_id,
:selenium,
:simulate, :automate,
- :field_named
+ :field_named, :follow_redirect!
end
end
View
11 lib/webrat/core/session.rb
@@ -219,6 +219,17 @@ def visit(url = nil, http_method = :get, data = {})
webrat_deprecate :visits, :visit
+ # Redirects to the address given at headers["Location"].
+ #
+ # Example:
+ # click_button "Save"
+ # follow_redirect!
+ def follow_redirect!
+ raise "not a redirect! Got #{response_code}" unless redirect?
+ visit(response_location)
+ response_code
+ end
+
# Subclasses can override this to show error messages without html
def formatted_error #:nodoc:
response_body
View
18 spec/public/follow_redirect_spec.rb
@@ -0,0 +1,18 @@
+require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
+
+describe "follow_redirect!" do
+ it "should request the address returned in the Location header" do
+ webrat_session.response_code = 302
+ webrat_session.stub!(:response_location).and_return("http://example.com/")
+ webrat_session.stub!(:internal_redirect?).and_return(false)
+ webrat_session.should_receive(:get).with("http://example.com/", {})
+ follow_redirect!.should == 302
+ end
+
+ it "should raise an error if not redirect?" do
+ lambda {
+ webrat_session.response_code = 200
+ follow_redirect!
+ }.should raise_error("not a redirect! Got 200")
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.