1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

return Mime::NullType if format is unknown

If a request has an unknown format, the methods html?, xml?, json? ...etc
not raise an Exception.

This patch add a class Mime::NullType, that is returned when  request.format is unknown
and it responds false to the methods that ends with '?' and true to 'nil?'.

It refers to #7837, this issue is considered a improvement not a bug.
This commit is contained in:
Angelo Capilleri 2012-12-22 09:18:01 +01:00
parent f9da785d0b
commit c2267db383
4 changed files with 28 additions and 3 deletions

View file

@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
* Added `Mime::NullType` class. This allows to use html?, xml?, json?..etc when
the `format` of `request` is unknown, without raise an exception.
*Angelo Capilleri*
* Integrate the Journey gem into Action Dispatch so that the global namespace
is not polluted with names that may be used as models.

View file

@ -27,7 +27,7 @@ module Mime
class << self
def [](type)
return type if type.is_a?(Type)
Type.lookup_by_extension(type)
Type.lookup_by_extension(type) || NullType.new
end
def fetch(type)
@ -306,6 +306,17 @@ module Mime
method.to_s.ends_with? '?'
end
end
class NullType
def nil?
true
end
private
def method_missing(method, *args)
false if method.to_s.ends_with? '?'
end
end
end
require 'action_dispatch/http/mime_types'

View file

@ -144,7 +144,7 @@ class SendFileTest < ActionController::TestCase
}
@controller.headers = {}
assert_raise(ArgumentError){ @controller.send(:send_file_headers!, options) }
assert !@controller.send(:send_file_headers!, options)
end
def test_send_file_headers_guess_type_from_extension

View file

@ -591,7 +591,16 @@ class RequestTest < ActiveSupport::TestCase
request = stub_request
request.expects(:parameters).at_least_once.returns({ :format => :unknown })
assert request.formats.empty?
assert_instance_of Mime::NullType, request.format
end
test "format is not nil with unknown format" do
request = stub_request
request.expects(:parameters).at_least_once.returns({ format: :hello })
assert_equal request.format.nil?, true
assert_equal request.format.html?, false
assert_equal request.format.xml?, false
assert_equal request.format.json?, false
end
test "formats with xhr request" do