From a4633725d2c459015b2a1796e991ff04b01b749d Mon Sep 17 00:00:00 2001 From: Bryan Traywick <42215+bryantraywick@users.noreply.github.com> Date: Fri, 4 Aug 2023 12:36:11 -0400 Subject: [PATCH] Fix NoMethodError when request Content-Type is blank. --- actionpack/lib/action_dispatch/http/mime_type.rb | 2 +- actionpack/test/controller/test_case_test.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb index 6ef576e19d024..a8c2fe9b85430 100644 --- a/actionpack/lib/action_dispatch/http/mime_type.rb +++ b/actionpack/lib/action_dispatch/http/mime_type.rb @@ -163,7 +163,7 @@ def register_callback(&block) def lookup(string) # fallback to the media-type without parameters if it was not found - LOOKUP[string] || LOOKUP[string.split(";", 2)[0].rstrip] || Type.new(string) + LOOKUP[string] || LOOKUP[string.split(";", 2)[0]&.rstrip] || Type.new(string) end def lookup_by_extension(extension) diff --git a/actionpack/test/controller/test_case_test.rb b/actionpack/test/controller/test_case_test.rb index d82694bf0acc1..1deb3889cd7ce 100644 --- a/actionpack/test/controller/test_case_test.rb +++ b/actionpack/test/controller/test_case_test.rb @@ -607,6 +607,13 @@ def test_params_passing_doesnt_modify_in_place assert_equal "application/json", parsed_env["CONTENT_TYPE"] end + test "blank Content-Type header" do + @request.headers["Content-Type"] = "" + assert_raises(ActionDispatch::Http::MimeNegotiation::InvalidType) do + get :test_headers + end + end + def test_using_as_json_sets_request_content_type_to_json post :render_body, params: { bool_value: true, str_value: "string", num_value: 2 }, as: :json