diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2013-02-27 19:17:31 -0800 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2013-02-27 19:17:31 -0800 |
commit | 17cff2c1b867ccf46d2c2dad4ae0ee9d506e9017 (patch) | |
tree | 977302ab48bee04d8096efc6aa5bbb54aa9c3409 | |
parent | 9126d53a07a6ae746491c7d86eecbdac4b3b5645 (diff) | |
parent | afddc0409e53731c8f21328e83921fd234b5bf54 (diff) | |
download | rails-17cff2c1b867ccf46d2c2dad4ae0ee9d506e9017.tar.gz rails-17cff2c1b867ccf46d2c2dad4ae0ee9d506e9017.tar.bz2 rails-17cff2c1b867ccf46d2c2dad4ae0ee9d506e9017.zip |
Merge pull request #9469 from senny/9466_format_enforcing_routes
`format: true` does not override existing format constraints.
-rw-r--r-- | actionpack/CHANGELOG.md | 10 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 2 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing_test.rb | 24 |
3 files changed, 35 insertions, 1 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 2a9afed35b..27ada73785 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,15 @@ ## Rails 4.0.0 (unreleased) ## +* `format: true` does not override existing format constraints. + Fixes #9466. + + Example: + + # this will force the .json extension + get '/json_only', to: ok, format: true, constraints: { format: /json/ } + + *Yves Senn* + * Skip valid encoding checks for non-String parameters that come from the matched route's defaults. Fixes #9435. diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index dba9ccbfa5..b0842e6354 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -129,7 +129,7 @@ module ActionDispatch end if options[:format] == true - @requirements[:format] = /.+/ + @requirements[:format] ||= /.+/ elsif Regexp === options[:format] @requirements[:format] = options[:format] elsif String === options[:format] diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 4775324b43..89f406a3d0 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -3407,6 +3407,8 @@ class TestFormatConstraints < ActionDispatch::IntegrationTest get '/string', to: ok, constraints: { format: 'json' } get '/regexp', to: ok, constraints: { format: /json/ } + get '/json_only', to: ok, format: true, constraints: { format: /json/ } + get '/xml_only', to: ok, format: 'xml' end end @@ -3434,6 +3436,28 @@ class TestFormatConstraints < ActionDispatch::IntegrationTest get 'http://www.example.com/regexp.html' assert_response :not_found end + + def test_enforce_with_format_true_with_constraint + get 'http://www.example.com/json_only.json' + assert_response :success + + get 'http://www.example.com/json_only.html' + assert_response :not_found + + get 'http://www.example.com/json_only' + assert_response :not_found + end + + def test_enforce_with_string + get 'http://www.example.com/xml_only.xml' + assert_response :success + + get 'http://www.example.com/xml_only' + assert_response :success + + get 'http://www.example.com/xml_only.json' + assert_response :not_found + end end class TestRouteDefaults < ActionDispatch::IntegrationTest |