Skip to content

Commit 0b1b512

Browse files
authored
Merge pull request rails#46057 from jdufresne/assert-redirect-status
Allow specifying the HTTP status code in assert_redirected_to
2 parents dd163f8 + 0211139 commit 0b1b512

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

actionpack/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Added the `:status` option to `assert_redirected_to` to specify the precise
2+
HTTP status of the redirect. Defaults to `:redirect` for backwards
3+
compatibility.
4+
5+
*Jon Dufresne*
6+
17
* Rescue `JSON::ParserError` in Cookies JSON deserializer to discards marshal dumps:
28

39
Without this change, if `action_dispatch.cookies_serializer` is set to `:json` and

actionpack/lib/action_dispatch/testing/assertions/response.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,19 @@ def assert_response(type, message = nil)
5050
#
5151
# # Asserts that the redirection matches the regular expression
5252
# assert_redirected_to %r(\Ahttp://example.org)
53-
def assert_redirected_to(options = {}, message = nil)
54-
assert_response(:redirect, message)
55-
return true if options === @response.location
53+
#
54+
# # Asserts that the redirection has the HTTP status code 301 (Moved
55+
# # Permanently).
56+
# assert_redirected_to "/some/path", status: :moved_permanently
57+
def assert_redirected_to(url_options = {}, options = {}, message = nil)
58+
options, message = message, nil if message.is_a?(Hash) && options.empty?
59+
60+
status = options[:status] || :redirect
61+
assert_response(status, message)
62+
return true if url_options === @response.location
5663

5764
redirect_is = normalize_argument_to_redirection(@response.location)
58-
redirect_expected = normalize_argument_to_redirection(options)
65+
redirect_expected = normalize_argument_to_redirection(url_options)
5966

6067
message ||= "Expected response to be a redirect to <#{redirect_expected}> but was a redirect to <#{redirect_is}>"
6168
assert_operator redirect_expected, :===, redirect_is, message

actionpack/test/controller/action_pack_assertions_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def redirect_external() redirect_to "http://www.rubyonrails.org"; end
3636

3737
def redirect_external_protocol_relative() redirect_to "//www.rubyonrails.org"; end
3838

39+
def redirect_permanently() redirect_to "/some/path", status: :moved_permanently end
40+
3941
def response404() head "404 AWOL" end
4042

4143
def response500() head "500 Sorry" end
@@ -440,6 +442,26 @@ def test_assert_redirection_with_symbol
440442
}
441443
end
442444

445+
def test_assert_redirection_with_status
446+
process :redirect_to_path
447+
assert_redirected_to "http://test.host/some/path", status: :found
448+
assert_raise ActiveSupport::TestCase::Assertion do
449+
assert_redirected_to "http://test.host/some/path", status: :moved_permanently
450+
end
451+
assert_raise ActiveSupport::TestCase::Assertion, "Custom message" do
452+
assert_redirected_to "http://test.host/some/path", { status: :moved_permanently }, "Custom message"
453+
end
454+
455+
process :redirect_permanently
456+
assert_redirected_to "http://test.host/some/path", status: :moved_permanently
457+
assert_raise ActiveSupport::TestCase::Assertion do
458+
assert_redirected_to "http://test.host/some/path", status: :found
459+
end
460+
assert_raise ActiveSupport::TestCase::Assertion, "Custom message" do
461+
assert_redirected_to "http://test.host/some/path", { status: :found }, "Custom message"
462+
end
463+
end
464+
443465
def test_redirected_to_with_nested_controller
444466
@controller = Admin::InnerModuleController.new
445467
get :redirect_to_absolute_controller

0 commit comments

Comments
 (0)