From e19b1cc323e8df657a357bfeefe3f68745f80938 Mon Sep 17 00:00:00 2001 From: Josh Knowles Date: Mon, 29 Dec 2008 23:14:26 -0500 Subject: [PATCH] Rails and Merb integration tests for following redirects --- lib/webrat/core/session.rb | 2 +- lib/webrat/merb.rb | 9 --------- spec/integration/merb/app/controllers/testing.rb | 4 ++++ spec/integration/merb/config/router.rb | 1 + spec/integration/merb/spec/webrat_spec.rb | 8 ++++++-- .../rails/app/controllers/webrat_controller.rb | 10 +++++++--- spec/integration/rails/config/routes.rb | 6 ++++-- spec/integration/rails/test/integration/webrat_test.rb | 7 ++++++- spec/private/core/session_spec.rb | 2 +- spec/public/visit_spec.rb | 2 +- 10 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 3cff73c..d5f83f1 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -110,7 +110,7 @@ For example: @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 diff --git a/lib/webrat/merb.rb b/lib/webrat/merb.rb index 5ace8b5..6fa3e46 100644 --- a/lib/webrat/merb.rb +++ b/lib/webrat/merb.rb @@ -42,11 +42,6 @@ module Webrat :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 @@ -59,10 +54,6 @@ module Merb #:nodoc: @_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 diff --git a/spec/integration/merb/app/controllers/testing.rb b/spec/integration/merb/app/controllers/testing.rb index 40a56da..937500a 100644 --- a/spec/integration/merb/app/controllers/testing.rb +++ b/spec/integration/merb/app/controllers/testing.rb @@ -7,4 +7,8 @@ class Testing < Application def submit_form end + def redirect_to_root + redirect "/" + end + end \ No newline at end of file diff --git a/spec/integration/merb/config/router.rb b/spec/integration/merb/config/router.rb index 6df8fb2..3b06c55 100644 --- a/spec/integration/merb/config/router.rb +++ b/spec/integration/merb/config/router.rb @@ -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 \ No newline at end of file diff --git a/spec/integration/merb/spec/webrat_spec.rb b/spec/integration/merb/spec/webrat_spec.rb index cc9fc1b..1675a2c 100644 --- a/spec/integration/merb/spec/webrat_spec.rb +++ b/spec/integration/merb/spec/webrat_spec.rb @@ -5,7 +5,7 @@ describe "Webrat" do response = visit "/" response.should contain("Webrat Form") end - + it "should submit forms" do visit "/" fill_in "Text field", :with => "Hello" @@ -13,5 +13,9 @@ describe "Webrat" do select "January" click_button "Test" end - + + it "should follow redirects" do + response = visit "/redirect" + response.should contain("Webrat Form") + end end \ No newline at end of file diff --git a/spec/integration/rails/app/controllers/webrat_controller.rb b/spec/integration/rails/app/controllers/webrat_controller.rb index 5a2db67..23f0e82 100644 --- a/spec/integration/rails/app/controllers/webrat_controller.rb +++ b/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 \ No newline at end of file diff --git a/spec/integration/rails/config/routes.rb b/spec/integration/rails/config/routes.rb index 7b63461..41d6ff5 100644 --- a/spec/integration/rails/config/routes.rb +++ b/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 diff --git a/spec/integration/rails/test/integration/webrat_test.rb b/spec/integration/rails/test/integration/webrat_test.rb index 4ecbe27..ec83c1c 100644 --- a/spec/integration/rails/test/integration/webrat_test.rb +++ b/spec/integration/rails/test/integration/webrat_test.rb @@ -6,7 +6,7 @@ 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" @@ -14,4 +14,9 @@ class WebratTest < ActionController::IntegrationTest select "January" click_button "Test" end + + test "should follow redirects" do + visit redirect_path + assert response.body.include?("OK") + end end diff --git a/spec/private/core/session_spec.rb b/spec/private/core/session_spec.rb index 96cf281..d4f9b2a 100644 --- a/spec/private/core/session_spec.rb +++ b/spec/private/core/session_spec.rb @@ -115,7 +115,7 @@ describe Webrat::Session do 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, {}) diff --git a/spec/public/visit_spec.rb b/spec/public/visit_spec.rb index cecd460..28aa2ba 100644 --- a/spec/public/visit_spec.rb +++ b/spec/public/visit_spec.rb @@ -33,7 +33,7 @@ describe "visit" do 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")