From eddd979361b7d32f06d978b7a7626406c05576f6 Mon Sep 17 00:00:00 2001 From: Mutwin Kraus Date: Fri, 24 Apr 2009 23:49:11 -0700 Subject: [PATCH] Fixed following of internal redirects when using a custom Host header Webrat didn't properly follow internal redirects when using the Host header, this lets Webrat check the Host header before falling back to www.example.com as the current_host. --- lib/webrat/core/session.rb | 2 +- .../integration/rails/app/controllers/webrat_controller.rb | 4 ++++ spec/integration/rails/config/routes.rb | 1 + spec/integration/rails/test/integration/webrat_test.rb | 7 +++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index d006ce93..d59c6cab 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -264,7 +264,7 @@ def response_location end def current_host - URI.parse(current_url).host || "www.example.com" + URI.parse(current_url).host || @custom_headers["Host"] || "www.example.com" end def response_location_host diff --git a/spec/integration/rails/app/controllers/webrat_controller.rb b/spec/integration/rails/app/controllers/webrat_controller.rb index b117ae8a..10d9e824 100644 --- a/spec/integration/rails/app/controllers/webrat_controller.rb +++ b/spec/integration/rails/app/controllers/webrat_controller.rb @@ -25,6 +25,10 @@ def external_redirect redirect_to "http://google.com" end + def host_redirect + redirect_to submit_url + end + def before_redirect_form end diff --git a/spec/integration/rails/config/routes.rb b/spec/integration/rails/config/routes.rb index ced0b4f4..07223cec 100644 --- a/spec/integration/rails/config/routes.rb +++ b/spec/integration/rails/config/routes.rb @@ -4,6 +4,7 @@ webrat.internal_redirect "/internal_redirect", :action => "internal_redirect" webrat.external_redirect "/external_redirect", :action => "external_redirect" webrat.infinite_redirect "/infinite_redirect", :action => "infinite_redirect" + webrat.host_redirect "/host_redirect", :action => "host_redirect" webrat.before_redirect_form "/before_redirect_form", :action => "before_redirect_form" webrat.redirect_to_show_params "/redirect_to_show_params", :action => "redirect_to_show_params" diff --git a/spec/integration/rails/test/integration/webrat_test.rb b/spec/integration/rails/test/integration/webrat_test.rb index e2c2def6..5bf7875d 100644 --- a/spec/integration/rails/test/integration/webrat_test.rb +++ b/spec/integration/rails/test/integration/webrat_test.rb @@ -45,6 +45,13 @@ class WebratTest < ActionController::IntegrationTest assert response.redirect? end + test "should recognize the host header to follow redirects properly" do + header "Host", "foo.bar" + visit host_redirect_path + assert !response.redirect? + assert response.body.include?("OK") + end + test "should click link by text" do visit internal_redirect_path click_link "Test Link Text"