Skip to content

Commit

Permalink
Rails and Merb integration tests for following redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
joshknowles committed Dec 30, 2008
1 parent e77495b commit e19b1cc
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/webrat/core/session.rb
Expand Up @@ -110,7 +110,7 @@ def request_page(url, http_method, data) #:nodoc:
@http_method = http_method
@data = data

request_page(response.location, :get, data) if redirect?
request_page(response.headers["Location"], :get, data) if redirect?

return response
end
Expand Down
9 changes: 0 additions & 9 deletions lib/webrat/merb.rb
Expand Up @@ -42,11 +42,6 @@ def do_request(url, data, headers, method)
:params => (data && data.any?) ? data : nil,
:headers => headers,
:method => method)
follow_redirect
end

def follow_redirect
self.get(@response.headers['Location'], nil, @response.headers) if @response.status == 302
end

end
Expand All @@ -59,10 +54,6 @@ def request(uri, env = {})
@_webrat_session ||= Webrat::MerbSession.new
@_webrat_session.response = @_webrat_session.request(uri, env)
end

def follow_redirect
@_webrat_session.follow_redirect
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/integration/merb/app/controllers/testing.rb
Expand Up @@ -7,4 +7,8 @@ def show_form
def submit_form
end

def redirect_to_root
redirect "/"
end

end
1 change: 1 addition & 0 deletions spec/integration/merb/config/router.rb
Expand Up @@ -28,4 +28,5 @@
Merb.logger.info("Compiling routes...")
Merb::Router.prepare do
match("/").to(:controller => "testing", :action => "show_form")
match("/redirect").to(:controller => "testing", :action => "redirect_to_root")
end
8 changes: 6 additions & 2 deletions spec/integration/merb/spec/webrat_spec.rb
Expand Up @@ -5,13 +5,17 @@
response = visit "/"
response.should contain("Webrat Form")
end

it "should submit forms" do
visit "/"
fill_in "Text field", :with => "Hello"
check "TOS"
select "January"
click_button "Test"
end


it "should follow redirects" do
response = visit "/redirect"
response.should contain("Webrat Form")
end
end
10 changes: 7 additions & 3 deletions spec/integration/rails/app/controllers/webrat_controller.rb
@@ -1,10 +1,14 @@
class WebratController < ApplicationController

def form
end

def submit
render :text => "OK"
end


def redirect
redirect_to :submit
end

end
6 changes: 4 additions & 2 deletions spec/integration/rails/config/routes.rb
@@ -1,6 +1,8 @@
ActionController::Routing::Routes.draw do |map|
map.with_options :controller => "webrat" do |webrat|
webrat.submit "/submit", :action => "submit"
webrat.root :action => "form"
webrat.submit "/submit", :action => "submit"
webrat.redirect "/redirect", :action => "redirect"

webrat.root :action => "form"
end
end
7 changes: 6 additions & 1 deletion spec/integration/rails/test/integration/webrat_test.rb
Expand Up @@ -6,12 +6,17 @@ class WebratTest < ActionController::IntegrationTest
assert_tag "Webrat Form"
assert response.body.include?("Webrat Form")
end

test "should submit forms" do
visit root_path
fill_in "Text field", :with => "Hello"
check "TOS"
select "January"
click_button "Test"
end

test "should follow redirects" do
visit redirect_path
assert response.body.include?("OK")
end
end
2 changes: 1 addition & 1 deletion spec/private/core/session_spec.rb
Expand Up @@ -115,7 +115,7 @@ def session.response_body

it "should follow redirects" do
webrat_session.should_receive(:redirect?).twice.and_return(true, false)
webrat_session.response.should_receive(:location).once.and_return("/newurl")
webrat_session.response.should_receive(:headers).once.and_return({ "Location" => "/newurl" })

webrat_session.request_page("/oldurl", :get, {})

Expand Down
2 changes: 1 addition & 1 deletion spec/public/visit_spec.rb
Expand Up @@ -33,7 +33,7 @@

it "should follow redirects" do
webrat_session.should_receive(:redirect?).twice.and_return(true, false)
webrat_session.response.should_receive(:location).once.and_return("/newurl")
webrat_session.response.should_receive(:headers).once.and_return({ "Location" => "/newurl" })

visit("/oldurl")

Expand Down

0 comments on commit e19b1cc

Please sign in to comment.