From 7f7a19296b6ba3dbbef88c7d42e66cad5c3e9ac5 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Tue, 7 Oct 2014 11:54:44 -0400 Subject: Increasing coverage on MIME::Types::Loader. --- lib/mime/types/loader.rb | 33 ++++++++++++--------------------- test/bad-fixtures/malformed | 9 +++++++++ test/test_mime_types_loader.rb | 13 +++++++++++-- 3 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 test/bad-fixtures/malformed diff --git a/lib/mime/types/loader.rb b/lib/mime/types/loader.rb index c78ac0b..40ddd8d 100644 --- a/lib/mime/types/loader.rb +++ b/lib/mime/types/loader.rb @@ -11,10 +11,8 @@ require 'mime/types/loader_path' # 3. The value of MIME::Types::Loader::PATH. # # When #load is called, the +path+ will be searched recursively for all YAML -# (.yml or .yaml) files. By convention, there is one file for each media type -# (application.yml, audio.yml, etc.), but this is not required. -# -# +# (.yml or .yaml) files. By convention, there is one file for each media +# type (application.yml, audio.yml, etc.), but this is not required. class MIME::Types::Loader # The path that will be read for the MIME::Types files. attr_reader :path @@ -81,6 +79,8 @@ class MIME::Types::Loader container end + BadV1Format = Class.new(Exception) + class << self # Loads the default MIME::Type registry. def load @@ -124,36 +124,27 @@ class MIME::Types::Loader # more information that's available, though, the richer the values that can # be provided. def load_from_v1(filename) + MIME.deprecated(self.class, __method__) data = read_file(filename).split($/) mime = MIME::Types.new data.each_with_index { |line, index| item = line.chomp.strip next if item.empty? - begin - m = MIME::Types::Loader::V1_FORMAT.match(item).captures - rescue Exception + m = MIME::Types::Loader::V1_FORMAT.match(item) + + unless m warn <<-EOS -#{filename}:#{index}: Parsing error in v1 MIME type definition. +#{filename}:#{index + 1}: Parsing error in v1 MIME type definition. => #{line} EOS - raise + raise BadV1Format, line end unregistered, obsolete, platform, mediatype, subtype, extensions, - encoding, urls, docs, comment = *m + encoding, urls, docs, comment = *m.captures - if mediatype.nil? - if comment.nil? - warn <<-EOS -#{filename}:#{index}: Parsing error in v1 MIME type definition (no media type). -=> #{line} - EOS - raise - end - - next - end + next if mediatype.nil? extensions &&= extensions.split(/,/) urls &&= urls.split(/,/) diff --git a/test/bad-fixtures/malformed b/test/bad-fixtures/malformed new file mode 100644 index 0000000..2c716d4 --- /dev/null +++ b/test/bad-fixtures/malformed @@ -0,0 +1,9 @@ +!application.smil @smi,smil :8bit 'IANA,RFC4536 =use-instead:application/smil+xml +!audio/vnd.qcelp @qcp 'IANA,RFC3625 =use-instead:audio/QCELP +*!image/bmp @bmp =use-instead:image/x-bmp +*application/acad 'LTSW +*audio/webm @webm '{WebM=http://www.webmproject.org/code/specs/container/} +*image/pjpeg :base64 =Fixes a bug with IE6 and progressive JPEGs +application/1d-interleaved-parityfec 'IANA,RFC6015 +audio/1d-interleaved-parityfec 'IANA,RFC6015 +mac:application/x-apple-diskimage @dmg diff --git a/test/test_mime_types_loader.rb b/test/test_mime_types_loader.rb index 26ae584..61848ba 100644 --- a/test/test_mime_types_loader.rb +++ b/test/test_mime_types_loader.rb @@ -5,8 +5,9 @@ require 'minitest_helper' class TestMIMETypesLoader < Minitest::Test def setup - @path = File.expand_path('../fixture', __FILE__) - @loader = MIME::Types::Loader.new(@path) + @path = File.expand_path('../fixture', __FILE__) + @loader = MIME::Types::Loader.new(@path) + @bad_path = File.expand_path('../bad-fixtures', __FILE__) end def assert_correctly_loaded(types) @@ -39,4 +40,12 @@ class TestMIMETypesLoader < Minitest::Test def test_load_v1 assert_correctly_loaded(@loader.load_v1) end + + def test_malformed_v1 + assert_output(nil, /1: Parsing error in v1 MIME type definition/) { + assert_raises(MIME::Types::Loader::BadV1Format) { + MIME::Types::Loader.load_from_v1(File.join(@bad_path, 'malformed')) + } + } + end end -- cgit v1.2.1