From d55dea5ef1cde3172d83f12a7ded96a9603dcaa9 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 29 Jul 2019 12:58:36 +0900 Subject: Add Mime::Type#match? that doesn't create MatchData --- actionpack/lib/action_dispatch/http/mime_type.rb | 8 +++++++- actionpack/test/dispatch/mime_type_test.rb | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb index ed1d50f3b9..4bee8ed785 100644 --- a/actionpack/lib/action_dispatch/http/mime_type.rb +++ b/actionpack/lib/action_dispatch/http/mime_type.rb @@ -202,7 +202,7 @@ module Mime # For an input of 'application', returns [Mime[:html], Mime[:js], # Mime[:xml], Mime[:yaml], Mime[:atom], Mime[:json], Mime[:rss], Mime[:url_encoded_form]. def parse_data_with_trailing_star(type) - Mime::SET.select { |m| m =~ type } + Mime::SET.select { |m| m.match?(type) } end # This method is opposite of register method. @@ -283,6 +283,12 @@ module Mime @synonyms.any? { |synonym| synonym.to_s =~ regexp } || @string =~ regexp end + def match?(mime_type) + return false unless mime_type + regexp = Regexp.new(Regexp.quote(mime_type.to_s)) + @synonyms.any? { |synonym| synonym.to_s.match?(regexp) } || @string.match?(regexp) + end + def html? symbol == :html || @string =~ /html/ end diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb index da417a1604..a8bc2f0e64 100644 --- a/actionpack/test/dispatch/mime_type_test.rb +++ b/actionpack/test/dispatch/mime_type_test.rb @@ -175,6 +175,12 @@ class MimeTypeTest < ActiveSupport::TestCase assert_match Mime[:html], "application/xhtml+xml" end + test "match?" do + assert Mime[:js].match?("text/javascript") + assert Mime[:js].match?("application/javascript") + assert_not Mime[:js].match?("text/html") + end + test "can be initialized with wildcards" do assert_equal "*/*", Mime::Type.new("*/*").to_s assert_equal "text/*", Mime::Type.new("text/*").to_s -- cgit v1.2.3