Skip to content

Commit 2afc1a1

Browse files
authored
Regression fix: binary declared type should fall back to filename extension type (#99)
The application/octet-stream content type is treated as a default rather than a specific declaration. We should check the filename extension for the more specific type in this case. If there's no extension, the type falls back to binary anyway. Fixes regression in 1.0.2 -> 1.0.3 introduced by #94.
1 parent 59d23fd commit 2afc1a1

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/marcel/mime_type.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ def for_extension(extension)
6060
end
6161

6262
def for_declared_type(declared_type)
63-
parse_media_type(declared_type)
63+
type = parse_media_type(declared_type)
64+
65+
# application/octet-stream is treated as an undeclared/missing type,
66+
# allowing the type to be inferred from the filename. If there's no
67+
# filename extension, then the type falls back to binary anyway.
68+
type unless type == BINARY
6469
end
6570

6671
def with_io(pathname_or_io, &block)

test/declared_type_test.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
require 'rack'
33

44
class Marcel::MimeType::DeclaredTypeTest < Marcel::TestCase
5-
test "returns declared type as last resort" do
6-
assert_equal "text/html", Marcel::MimeType.for(name: "unrecognisable", declared_type: "text/html")
5+
test "prefers declared type over filename extension" do
6+
assert_equal "text/html", Marcel::MimeType.for(name: "file.txt", declared_type: "text/html")
77
end
88

9-
test "returns application/octet-stream if declared type empty or unrecognised" do
9+
test "prefers filename extension over binary type" do
10+
assert_equal "text/plain", Marcel::MimeType.for(name: "file.txt", declared_type: "application/octet-stream")
11+
end
12+
13+
test "defaults to binary if declared type is unrecognized" do
14+
assert_equal "application/octet-stream", Marcel::MimeType.for(declared_type: nil)
1015
assert_equal "application/octet-stream", Marcel::MimeType.for(declared_type: "")
1116
assert_equal "application/octet-stream", Marcel::MimeType.for(declared_type: "unrecognised")
1217
end

0 commit comments

Comments
 (0)