1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/test/blob_test.rb

42 lines
1.2 KiB
Ruby
Raw Normal View History

2017-07-01 06:06:08 -04:00
require "test_helper"
2017-07-01 06:47:13 -04:00
require "database/setup"
require "active_storage/blob"
2017-07-01 06:47:13 -04:00
class ActiveStorage::BlobTest < ActiveSupport::TestCase
test "create after upload sets byte size and checksum" do
data = "Hello world!"
2017-07-03 14:14:28 -04:00
blob = create_blob data: data
assert_equal data, blob.download
assert_equal data.length, blob.byte_size
assert_equal Digest::MD5.base64digest(data), blob.checksum
2017-06-30 13:12:58 -04:00
end
2017-07-03 14:14:28 -04:00
2017-07-10 16:17:48 -04:00
test "download yields chunks" do
blob = create_blob data: "a" * 75.kilobytes
2017-07-10 16:17:48 -04:00
chunks = []
blob.download do |chunk|
chunks << chunk
end
assert_equal 2, chunks.size
assert_equal "a" * 64.kilobytes, chunks.first
assert_equal "a" * 11.kilobytes, chunks.second
2017-07-10 16:17:48 -04:00
end
test "urls expiring in 5 minutes" do
2017-07-03 14:14:28 -04:00
blob = create_blob
2017-07-03 15:06:09 -04:00
2017-07-19 13:58:23 -04:00
freeze_time do
assert_equal expected_url_for(blob), blob.url
assert_equal expected_url_for(blob, disposition: :attachment), blob.url(disposition: :attachment)
2017-07-03 15:06:09 -04:00
end
2017-07-03 14:14:28 -04:00
end
private
def expected_url_for(blob, disposition: :inline)
2017-07-09 12:03:13 -04:00
"/rails/active_storage/disk/#{ActiveStorage::VerifiedKeyWithExpiration.encode(blob.key, expires_in: 5.minutes)}/#{blob.filename}?disposition=#{disposition}"
end
2017-06-30 13:12:58 -04:00
end