diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2015-12-29 14:58:23 -0300 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2015-12-29 14:58:23 -0300 |
commit | 98927586a8caf2824557aa2ff462ddf22d48008b (patch) | |
tree | 96f984dec9fbe8942dfc8a50349f255e00e7a3c7 | |
parent | 929c61573e289e432a4e571ae157248745ae2eae (diff) | |
parent | 44ca804229cfdf7a149eed63a2f61f387f6f9fc8 (diff) | |
download | rails-98927586a8caf2824557aa2ff462ddf22d48008b.tar.gz rails-98927586a8caf2824557aa2ff462ddf22d48008b.tar.bz2 rails-98927586a8caf2824557aa2ff462ddf22d48008b.zip |
Merge pull request #22831 from jmbejar/mime-negotiation-format-from-extension-fix
Mime negotiation format from extension improvements
-rw-r--r-- | actionpack/lib/action_dispatch/http/mime_negotiation.rb | 6 | ||||
-rw-r--r-- | actionpack/test/dispatch/request_test.rb | 21 |
2 files changed, 24 insertions, 3 deletions
diff --git a/actionpack/lib/action_dispatch/http/mime_negotiation.rb b/actionpack/lib/action_dispatch/http/mime_negotiation.rb index 0152c17ed4..e9b25339dc 100644 --- a/actionpack/lib/action_dispatch/http/mime_negotiation.rb +++ b/actionpack/lib/action_dispatch/http/mime_negotiation.rb @@ -67,10 +67,10 @@ module ActionDispatch v = if params_readable Array(Mime[parameters[:format]]) - elsif format = format_from_path_extension - Array(Mime[format]) elsif use_accept_header && valid_accept_header accepts + elsif extension_format = format_from_path_extension + [extension_format] elsif xhr? [Mime[:js]] else @@ -166,7 +166,7 @@ module ActionDispatch def format_from_path_extension path = @env['action_dispatch.original_path'] || @env['PATH_INFO'] if match = path && path.match(/\.(\w+)\z/) - match.captures.first + Mime[match.captures.first] end end end diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index 7dd9d05e62..0edad72fd9 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -897,6 +897,27 @@ class RequestFormat < BaseRequestTest ActionDispatch::Request.ignore_accept_header = old_ignore_accept_header end end + + test "format taken from the path extension" do + request = stub_request 'PATH_INFO' => '/foo.xml' + assert_called(request, :parameters, times: 1, returns: {}) do + assert_equal [Mime[:xml]], request.formats + end + + request = stub_request 'PATH_INFO' => '/foo.123' + assert_called(request, :parameters, times: 1, returns: {}) do + assert_equal [Mime[:html]], request.formats + end + end + + test "formats from accept headers have higher precedence than path extension" do + request = stub_request 'HTTP_ACCEPT' => 'application/json', + 'PATH_INFO' => '/foo.xml' + + assert_called(request, :parameters, times: 1, returns: {}) do + assert_equal [Mime[:json]], request.formats + end + end end class RequestMimeType < BaseRequestTest |