Permalink
Browse files

unregister method implementation and test

  • Loading branch information...
1 parent c6030e8 commit e8708836252852d8bae87c80472b865812fa9776 Neeraj Singh committed with josevalim Nov 23, 2010
Showing with 29 additions and 0 deletions.
  1. +12 −0 actionpack/lib/action_dispatch/http/mime_type.rb
  2. +17 −0 actionpack/test/dispatch/mime_type_test.rb
@@ -177,6 +177,18 @@ def parse_data_with_trailing_star(input)
keys = Mime::LOOKUP.keys.select{|k| k.include?(input)}
Mime::LOOKUP.values_at(*keys).uniq
end
+
+ # This method is opposite of register method.
+ #
+ # Usage:
+ #
+ # Mime::Type.unregister("text/x-mobile", :mobile)
+ def unregister(string, symbol)
+ EXTENSION_LOOKUP.delete(symbol.to_s)
+ LOOKUP.delete(string)
+ symbol = symbol.to_s.upcase.intern
+ Mime.module_eval { remove_const(symbol) if const_defined?(symbol) }
+ end
end
def initialize(string, symbol = nil, synonyms = [])
@@ -12,6 +12,23 @@ class MimeTypeTest < ActiveSupport::TestCase
end
end
+ test "unregister" do
+ begin
+ Mime::Type.register("text/x-mobile", :mobile)
+ assert defined?(Mime::MOBILE)
+ assert_equal Mime::MOBILE, Mime::LOOKUP['text/x-mobile']
+ assert_equal Mime::MOBILE, Mime::EXTENSION_LOOKUP['mobile']
+
+ Mime::Type.unregister("text/x-mobile", :mobile)
+ assert !defined?(Mime::MOBILE), "Mime::MOBILE should not be defined"
+ assert !Mime::LOOKUP.has_key?('text/x-mobile'), "Mime::LOOKUP should not have key ['text/x-mobile]"
+ assert !Mime::EXTENSION_LOOKUP.has_key?('mobile'), "Mime::EXTENSION_LOOKUP should not have key ['mobile]"
+ ensure
+ Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
+ Mime::LOOKUP.reject!{|key,_| key == 'text/x-mobile'}
+ end
+ end
+
test "parse text with trailing star" do
accept = "text/*"
expect = [Mime::JSON, Mime::XML, Mime::ICS, Mime::HTML, Mime::CSS, Mime::CSV, Mime::JS, Mime::YAML, Mime::TEXT].sort_by(&:to_s)

0 comments on commit e870883

Please sign in to comment.