From c2267db383fb42e2e3c2abdbd58be6638908fb0f Mon Sep 17 00:00:00 2001 From: Angelo Capilleri Date: Sat, 22 Dec 2012 09:18:01 +0100 Subject: 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. --- actionpack/test/controller/send_file_test.rb | 2 +- actionpack/test/dispatch/request_test.rb | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb index 5e9053da5b..8ecc1c7d73 100644 --- a/actionpack/test/controller/send_file_test.rb +++ b/actionpack/test/controller/send_file_test.rb @@ -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 diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index 263853fb6c..4e59e214c6 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -591,9 +591,18 @@ 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 request = stub_request 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" request.expects(:parameters).at_least_once.returns({}) -- cgit v1.2.3