aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-01-23 15:59:11 -0700
committerSean Griffin <sean@thoughtbot.com>2015-01-23 16:03:38 -0700
commit6cf671b61e35a94ec8e6acad0aaef85822c93eb0 (patch)
treeb2e7cb8f142dbf083d812c38c544d7908515a355
parent8c83bd0732fc526e69cd5c348cb9d9842ae60c99 (diff)
downloadrails-6cf671b61e35a94ec8e6acad0aaef85822c93eb0.tar.gz
rails-6cf671b61e35a94ec8e6acad0aaef85822c93eb0.tar.bz2
rails-6cf671b61e35a94ec8e6acad0aaef85822c93eb0.zip
Allow `method: "all"` as a valid routing test option
This allows the test to mirror the production code, since `via: :all` is a valid option. The behavior in 4.1 did not actually test that it matched all verbs, but instead defaulted to testing for "GET". This implementation aims to better handle the intention of passing "all". What will actually be asserted doesn't quite match up with the generated route, since it appears to just not create a constraint on the method. However, I don't think that we can easily test the lack of that constraint. Testing each of the main 4 HTTP verbs seems to be a reasonably close approximation, which should be sufficient for our needs. Fixes #18511.
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/routing.rb22
-rw-r--r--actionpack/test/controller/resources_test.rb22
2 files changed, 36 insertions, 8 deletions
diff --git a/actionpack/lib/action_dispatch/testing/assertions/routing.rb b/actionpack/lib/action_dispatch/testing/assertions/routing.rb
index 28dc88d317..c3e998a864 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/routing.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/routing.rb
@@ -38,18 +38,24 @@ module ActionDispatch
# # Test a custom route
# assert_recognizes({controller: 'items', action: 'show', id: '1'}, 'view/item1')
def assert_recognizes(expected_options, path, extras={}, msg=nil)
- request = recognized_request_for(path, extras, msg)
+ if path.is_a?(Hash) && path[:method].to_s == "all"
+ [:get, :post, :put, :delete].each do |method|
+ assert_recognizes(expected_options, path.merge(method: method), extras, msg)
+ end
+ else
+ request = recognized_request_for(path, extras, msg)
- expected_options = expected_options.clone
+ expected_options = expected_options.clone
- expected_options.stringify_keys!
+ expected_options.stringify_keys!
- msg = message(msg, "") {
- sprintf("The recognized options <%s> did not match <%s>, difference:",
- request.path_parameters, expected_options)
- }
+ msg = message(msg, "") {
+ sprintf("The recognized options <%s> did not match <%s>, difference:",
+ request.path_parameters, expected_options)
+ }
- assert_equal(expected_options, request.path_parameters, msg)
+ assert_equal(expected_options, request.path_parameters, msg)
+ end
end
# Asserts that the provided options can be used to generate the provided path. This is the inverse of +assert_recognizes+.
diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb
index 0e15883f43..ce05765ebf 100644
--- a/actionpack/test/controller/resources_test.rb
+++ b/actionpack/test/controller/resources_test.rb
@@ -1047,6 +1047,28 @@ class ResourcesTest < ActionController::TestCase
end
end
+ def test_assert_routing_accepts_all_as_a_valid_method
+ with_routing do |set|
+ set.draw do
+ match "/products", to: "products#show", via: :all
+ end
+
+ assert_routing({ method: "all", path: "/products" }, { controller: "products", action: "show" })
+ end
+ end
+
+ def test_assert_routing_fails_when_not_all_http_methods_are_recognized
+ with_routing do |set|
+ set.draw do
+ match "/products", to: "products#show", via: [:get, :post, :put]
+ end
+
+ assert_raises(Minitest::Assertion) do
+ assert_routing({ method: "all", path: "/products" }, { controller: "products", action: "show" })
+ end
+ end
+ end
+
def test_singleton_resource_name_is_not_singularized
with_singleton_resources(:preferences) do
assert_singleton_restful_for :preferences