Skip to content

Commit 4f0f344

Browse files
committed
Marcel 1.0.4
Update tests to clarify content type detection heuristic after exposing a regression in Marcel 1.0.3 that wasn't caught by its test suite: 1. magic bytes 2. declared content type, unless it's binary 3. filename extension 4. binary: application/octet-stream
1 parent 1c0982d commit 4f0f344

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ GEM
317317
net-imap
318318
net-pop
319319
net-smtp
320-
marcel (1.0.2)
320+
marcel (1.0.4)
321321
matrix (0.4.2)
322322
mdl (0.12.0)
323323
kramdown (~> 2.3)

activestorage/test/models/blob_test.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,33 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
4242
end
4343

4444
test "create_and_upload extracts content type from data" do
45-
blob = create_file_blob content_type: "application/octet-stream"
45+
blob = create_file_blob fixture: "racecar.jpg", content_type: "application/octet-stream", filename: "spoofed.txt"
4646
assert_equal "image/jpeg", blob.content_type
4747
end
4848

49-
test "create_and_upload extracts content type from filename" do
50-
blob = create_blob content_type: "application/octet-stream"
49+
test "create_and_upload prefers given content type over filename" do
50+
blob = create_blob content_type: "specific/type", filename: "file.txt"
51+
assert_equal "specific/type", blob.content_type
52+
end
53+
54+
test "create_and_upload prefers filename over binary content type" do
55+
blob = create_blob content_type: "application/octet-stream", filename: "file.txt"
5156
assert_equal "text/plain", blob.content_type
5257
end
5358

54-
test "create_and_upload extracts content_type from io when no content_type given and identify: false" do
55-
blob = create_blob content_type: nil, identify: false
59+
test "create_and_upload extracts content type from filename" do
60+
blob = create_blob content_type: nil, filename: "hello.txt"
5661
assert_equal "text/plain", blob.content_type
5762
end
5863

59-
test "create_and_upload uses content_type when identify: false" do
60-
blob = create_blob data: "Article,dates,analysis\n1, 2, 3", filename: "table.csv", content_type: "text/csv", identify: false
61-
assert_equal "text/csv", blob.content_type
64+
test "create_and_upload extracts content_type from io when missing and identify: false" do
65+
blob = create_file_blob fixture: "racecar.jpg", content_type: nil, filename: "unknown", identify: false
66+
assert_equal "image/jpeg", blob.content_type
67+
end
68+
69+
test "create_and_upload uses given content_type when identify: false" do
70+
blob = create_file_blob fixture: "racecar.jpg", content_type: "given/type", filename: "unknown", identify: false
71+
assert_equal "given/type", blob.content_type
6272
end
6373

6474
test "create_and_upload generates a 28-character base36 key" do

activestorage/test/test_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def create_blob(key: nil, data: "Hello world!", filename: "hello.txt", content_t
4343
ActiveStorage::Blob.create_and_upload! key: key, io: StringIO.new(data), filename: filename, content_type: content_type, identify: identify, service_name: service_name, record: record
4444
end
4545

46-
def create_file_blob(key: nil, filename: "racecar.jpg", content_type: "image/jpeg", metadata: nil, service_name: nil, record: nil)
47-
ActiveStorage::Blob.create_and_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata, service_name: service_name, record: record
46+
def create_file_blob(key: nil, filename: "racecar.jpg", fixture: filename, content_type: "image/jpeg", identify: true, metadata: nil, service_name: nil, record: nil)
47+
ActiveStorage::Blob.create_and_upload! io: file_fixture(fixture).open, filename: filename, content_type: content_type, identify: identify, metadata: metadata, service_name: service_name, record: record
4848
end
4949

5050
def create_blob_before_direct_upload(key: nil, filename: "hello.txt", byte_size:, checksum:, content_type: "text/plain", record: nil)

0 commit comments

Comments
 (0)