diff --git a/lib/propshaft/asset.rb b/lib/propshaft/asset.rb index 759523f..4de31a5 100644 --- a/lib/propshaft/asset.rb +++ b/lib/propshaft/asset.rb @@ -8,8 +8,8 @@ def initialize(path, logical_path:, version: nil) @path, @logical_path, @version = path, Pathname.new(logical_path), version end - def content - File.binread(path) + def content(encoding: "ASCII-8BIT") + File.read(path, encoding: encoding) end def content_type diff --git a/lib/propshaft/resolver/dynamic.rb b/lib/propshaft/resolver/dynamic.rb index af5ce63..df82534 100644 --- a/lib/propshaft/resolver/dynamic.rb +++ b/lib/propshaft/resolver/dynamic.rb @@ -12,9 +12,9 @@ def resolve(logical_path) end end - def read(logical_path) + def read(logical_path, options = {}) if asset = load_path.find(logical_path) - asset.content + asset.content(**options) end end end diff --git a/lib/propshaft/resolver/static.rb b/lib/propshaft/resolver/static.rb index 896edc9..e46d755 100644 --- a/lib/propshaft/resolver/static.rb +++ b/lib/propshaft/resolver/static.rb @@ -12,9 +12,9 @@ def resolve(logical_path) end end - def read(logical_path) + def read(logical_path, encoding: "ASCII-8BIT") if asset_path = parsed_manifest[logical_path] - manifest_path.dirname.join(asset_path).read + File.read(manifest_path.dirname.join(asset_path), encoding: encoding) end end diff --git a/test/propshaft/asset_test.rb b/test/propshaft/asset_test.rb index 6d27ec1..afc2f7a 100644 --- a/test/propshaft/asset_test.rb +++ b/test/propshaft/asset_test.rb @@ -4,9 +4,15 @@ class Propshaft::AssetTest < ActiveSupport::TestCase test "content" do + assert_equal "ASCII-8BIT", find_asset("one.txt").content.encoding.to_s assert_equal "One from first path", find_asset("one.txt").content end + test "content with encoding" do + assert_equal "UTF-8", find_asset("one.txt").content(encoding: "UTF-8").encoding.to_s + assert_equal "One from first path", find_asset("one.txt").content(encoding: "UTF-8") + end + test "content type" do assert_equal "text/plain", find_asset("one.txt").content_type.to_s assert_equal "text/javascript", find_asset("again.js").content_type.to_s diff --git a/test/propshaft/resolver/dynamic_test.rb b/test/propshaft/resolver/dynamic_test.rb index 7cb60f1..1881f6c 100644 --- a/test/propshaft/resolver/dynamic_test.rb +++ b/test/propshaft/resolver/dynamic_test.rb @@ -13,9 +13,15 @@ class Propshaft::Resolver::DynamicTest < ActiveSupport::TestCase end test "reading static asset" do + assert_equal "ASCII-8BIT", @resolver.read("one.txt").encoding.to_s assert_equal "One from first path", @resolver.read("one.txt") end + test "reading static asset with encoding option" do + assert_equal "UTF-8", @resolver.read("one.txt", encoding: "UTF-8").encoding.to_s + assert_equal "One from first path", @resolver.read("one.txt", encoding: "UTF-8") + end + test "resolving missing asset returns nil" do assert_nil @resolver.resolve("nowhere.txt") end diff --git a/test/propshaft/resolver/static_test.rb b/test/propshaft/resolver/static_test.rb index 34e4a06..b643379 100644 --- a/test/propshaft/resolver/static_test.rb +++ b/test/propshaft/resolver/static_test.rb @@ -17,9 +17,15 @@ class Propshaft::Resolver::StaticTest < ActiveSupport::TestCase end test "reading static asset" do + assert_equal "ASCII-8BIT", @resolver.read("one.txt").encoding.to_s assert_equal "One from first path", @resolver.read("one.txt") end + test "reading static asset with encoding option" do + assert_equal "UTF-8", @resolver.read("one.txt", encoding: "UTF-8").encoding.to_s + assert_equal "One from first path", @resolver.read("one.txt", encoding: "UTF-8") + end + test "resolving missing asset returns nil" do assert_nil @resolver.resolve("nowhere.txt") end