Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows assert_redirected_to to accept a regular expression #6135

Merged
merged 1 commit into from
May 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 20 additions & 13 deletions actionpack/lib/action_dispatch/testing/assertions/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ def assert_response(type, message = nil)
# # 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
Expand All @@ -71,17 +74,21 @@ def parameterize(value)
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
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/controller/action_pack_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def test_assert_redirect_to_named_route_failure
assert_raise(ActiveSupport::TestCase::Assertion) do
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
Expand Down Expand Up @@ -212,6 +215,7 @@ def test_assert_redirected_to_top_level_named_route_from_nested_controller
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

Expand Down