aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2019-07-29 12:58:36 +0900
committerAkira Matsuda <ronnie@dio.jp>2019-07-29 14:17:36 +0900
commitd55dea5ef1cde3172d83f12a7ded96a9603dcaa9 (patch)
tree00ca4cad113a8530fb02719578a30978e117d23f
parent62d089a4ad0170320b851addf76f7f48a49d68d8 (diff)
downloadrails-d55dea5ef1cde3172d83f12a7ded96a9603dcaa9.tar.gz
rails-d55dea5ef1cde3172d83f12a7ded96a9603dcaa9.tar.bz2
rails-d55dea5ef1cde3172d83f12a7ded96a9603dcaa9.zip
Add Mime::Type#match? that doesn't create MatchData
-rw-r--r--actionpack/lib/action_dispatch/http/mime_type.rb8
-rw-r--r--actionpack/test/dispatch/mime_type_test.rb6
2 files changed, 13 insertions, 1 deletions
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 <tt>'application'</tt>, returns <tt>[Mime[:html], Mime[:js],
# Mime[:xml], Mime[:yaml], Mime[:atom], Mime[:json], Mime[:rss], Mime[:url_encoded_form]</tt>.
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