Skip to content

Commit

Permalink
Merge pull request #9469 from senny/9466_format_enforcing_routes
Browse files Browse the repository at this point in the history
`format: true` does not override existing format constraints.
  • Loading branch information
pixeltrix committed Feb 28, 2013
2 parents 9126d53 + afddc04 commit 17cff2c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
10 changes: 10 additions & 0 deletions 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.
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -129,7 +129,7 @@ def normalize_requirements!
end

if options[:format] == true
@requirements[:format] = /.+/
@requirements[:format] ||= /.+/
elsif Regexp === options[:format]
@requirements[:format] = options[:format]
elsif String === options[:format]
Expand Down
24 changes: 24 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Expand Up @@ -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

Expand Down Expand Up @@ -3434,6 +3436,28 @@ def test_regexp_format_constraints
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
Expand Down

0 comments on commit 17cff2c

Please sign in to comment.