Skip to content

Commit

Permalink
Ensure assets without extensions are handled correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rky-pl committed May 4, 2024
1 parent 26bc414 commit 6088b6b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/wicked_pdf/wicked_pdf_helper/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,26 @@ def find_asset(path)
elsif defined?(Propshaft::Assembly) && Rails.application.assets.is_a?(Propshaft::Assembly)
PropshaftAsset.new(Rails.application.assets.load_path.find(path))
elsif Rails.application.respond_to?(:assets_manifest)
asset_path = File.join(Rails.application.assets_manifest.dir, Rails.application.assets_manifest.assets[path])
relative_asset_path = get_asset_path_from_manifest(path)
return unless relative_asset_path

asset_path = File.join(Rails.application.assets_manifest.dir, relative_asset_path)
LocalAsset.new(asset_path) if File.file?(asset_path)
else
SprocketsEnvironment.find_asset(path, :base_path => Rails.application.root.to_s)
end
end

def get_asset_path_from_manifest(path)
assets = Rails.application.assets_manifest.assets

if File.extname(path).empty?
assets.find { |asset, _v| File.basename(asset, File.extname(asset)) == path }&.last
else
assets[path]
end
end

# will prepend a http or default_protocol to a protocol relative URL
# or when no protcol is set.
def prepend_protocol(source)
Expand Down
53 changes: 53 additions & 0 deletions test/functional/wicked_pdf_helper_assets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,57 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase

teardown do
WickedPdf.config = @saved_config

# @see freerange/mocha#331
Rails.application.unstub(:assets)
Rails.application.unstub(:assets_manifest)
end

if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
end

test 'wicked_pdf_asset_base64 works without file extension when using sprockets' do
assert_match %r{data:application\/javascript;base64,.+}, wicked_pdf_asset_base64('wicked')
end

test 'wicked_pdf_asset_base64 works without file extension when using asset manifest' do
stub_manifest = OpenStruct.new(
:dir => Rails.root.join('app/assets'),
:assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
)
Rails.application.stubs(:assets).returns(nil)
Rails.application.stubs(:assets_manifest).returns(stub_manifest)

assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked')
end

test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
Rails.configuration.assets.expects(:compile => true)
assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
wicked_pdf_stylesheet_link_tag('wicked')
end

test 'wicked_pdf_stylesheet_link_tag should work without file extension when using sprockets' do
Rails.configuration.assets.expects(:compile => true)
assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
wicked_pdf_stylesheet_link_tag('wicked')
end

test 'wicked_pdf_stylesheet_link_tag should work without file extension when using asset manifest' do
stub_manifest = OpenStruct.new(
:dir => Rails.root.join('app/assets'),
:assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
)

Rails.application.stubs(:assets).returns(nil)
Rails.application.stubs(:assets_manifest).returns(stub_manifest)

assert_equal "<style type='text/css'>/* Wicked styles */\n</style>",
wicked_pdf_stylesheet_link_tag('wicked')
end

test 'wicked_pdf_stylesheet_link_tag should raise if the stylesheet is not available and config is set' do
Rails.configuration.assets.expects(:compile => true)
WickedPdf.config[:raise_on_missing_assets] = true
Expand Down Expand Up @@ -60,6 +98,21 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
end

test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in when assets are remote and using asset manifest' do
stub_manifest = OpenStruct.new(
:dir => Rails.root.join('app/assets'),
:assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
)

Rails.application.stubs(:assets).returns(nil)
Rails.application.stubs(:assets_manifest).returns(stub_manifest)

stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 200, :body => '/* Wicked styles */')
expects(:precompiled_or_absolute_asset? => true).twice
assert_equal "<style type='text/css'>/* Wicked styles */</style>",
wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
end

test 'wicked_pdf_stylesheet_link_tag should raise if remote assets are not available and config is set' do
WickedPdf.config[:raise_on_missing_assets] = true
stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 404, :body => 'File not found')
Expand Down

0 comments on commit 6088b6b

Please sign in to comment.