diff options
author | Andy Lindeman <alindeman@gmail.com> | 2012-05-03 09:16:38 -0400 |
---|---|---|
committer | Andy Lindeman <alindeman@gmail.com> | 2012-05-03 09:16:38 -0400 |
commit | a544e006814a1ef82db6af50637eee3d35740c39 (patch) | |
tree | 73302f4af9393bfae515f7401a16c907146cbebd | |
parent | fb7abea99aeccb07962e4e82abbd110ab51eae47 (diff) | |
download | rails-a544e006814a1ef82db6af50637eee3d35740c39.tar.gz rails-a544e006814a1ef82db6af50637eee3d35740c39.tar.bz2 rails-a544e006814a1ef82db6af50637eee3d35740c39.zip |
Allows assert_redirected_to to accept a regular expression
-rw-r--r-- | actionpack/CHANGELOG.md | 2 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/testing/assertions/response.rb | 33 | ||||
-rw-r--r-- | actionpack/test/controller/action_pack_assertions_test.rb | 4 |
3 files changed, 26 insertions, 13 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 6bad313aef..96dee33f7b 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* Allows `assert_redirected_to` to match against a regular expression. *Andy Lindeman* + * Add backtrace to development routing error page. *Richard Schneeman* * Replace `include_seconds` boolean argument with `:include_seconds => true` option diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb index 40d38c59d6..8f6fff5d32 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/response.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb @@ -53,15 +53,18 @@ module ActionDispatch # # assert that the redirection was to the url for @customer # assert_redirected_to @customer # + # # asserts that the redirection matches the regular expression + # assert_redirected_to %r(\Ahttp://example.org) + # def assert_redirected_to(options = {}, message=nil) assert_response(:redirect, message) - return true if options == @response.location + return true if options === @response.location redirect_is = normalize_argument_to_redirection(@response.location) redirect_expected = normalize_argument_to_redirection(options) message ||= "Expected response to be a redirect to <#{redirect_expected}> but was a redirect to <#{redirect_is}>" - assert_equal redirect_expected, redirect_is, message + assert_operator redirect_expected, :===, redirect_is, message end private @@ -71,17 +74,21 @@ module ActionDispatch end def normalize_argument_to_redirection(fragment) - case fragment - when %r{^\w[A-Za-z\d+.-]*:.*} - fragment - when String - @request.protocol + @request.host_with_port + fragment - when :back - raise RedirectBackError unless refer = @request.headers["Referer"] - refer - else - @controller.url_for(fragment) - end.delete("\0\r\n") + normalized = case fragment + when Regexp + fragment + when %r{^\w[A-Za-z\d+.-]*:.*} + fragment + when String + @request.protocol + @request.host_with_port + fragment + when :back + raise RedirectBackError unless refer = @request.headers["Referer"] + refer + else + @controller.url_for(fragment) + end + + normalized.respond_to?(:delete) ? normalized.delete("\0\r\n") : normalized end end end diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index b121ca9481..f5f397c9c0 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -179,6 +179,9 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase assert_redirected_to 'http://test.host/route_two' end assert_raise(ActiveSupport::TestCase::Assertion) do + assert_redirected_to %r(^http://test.host/route_two) + end + assert_raise(ActiveSupport::TestCase::Assertion) do assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two' end assert_raise(ActiveSupport::TestCase::Assertion) do @@ -212,6 +215,7 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase process :redirect_to_top_level_named_route # assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return assert_redirected_to "/action_pack_assertions/foo" + assert_redirected_to %r(/action_pack_assertions/foo) end end |