diff options
| -rw-r--r-- | CHANGELOG.adoc | 1 | ||||
| -rw-r--r-- | Gemfile | 1 | ||||
| -rw-r--r-- | test/blocks_test.rb | 67 | ||||
| -rw-r--r-- | test/test_helper.rb | 4 |
4 files changed, 73 insertions, 0 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 0bcb1d72..d1dc8a5f 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -67,6 +67,7 @@ Improvements:: * Allow `--failure-level` to be set to default value, `FATAL` * Sort levels in help for `--failure-level` option in ascending order * Invert FR translations for caution & warning admonition labels (#4212) (*cyChop*) + * Add tests for open-uri/cached integration that is activated by the `cache-uri` attribute Documentation:: @@ -11,6 +11,7 @@ group :development do # coderay is needed for testing source highlighting gem 'coderay', '~> 1.1.0' gem 'haml', '~> 4.0' if RUBY_ENGINE == 'truffleruby' + gem 'open-uri-cached', '0.0.5' # pygments.rb is needed for testing source highlighting; Asciidoctor supports pygments.rb >= 1.2.0 gem 'pygments.rb', ENV['PYGMENTS_VERSION'] if ENV.key? 'PYGMENTS_VERSION' # rouge is needed for testing source highlighting; Asciidoctor supports rouge >= 2 diff --git a/test/blocks_test.rb b/test/blocks_test.rb index d238d202..d9afe4c7 100644 --- a/test/blocks_test.rb +++ b/test/blocks_test.rb @@ -2486,6 +2486,39 @@ context 'Blocks' do assert_css 'svg circle', output, 1 end + test 'should cache remote SVG when allow-uri-read, cache-uri, and inline option are set', unless: ruby_3_1_up? do + begin + if OpenURI.respond_to? :cache_open_uri + OpenURI.singleton_class.send :remove_method, :open_uri + OpenURI.singleton_class.send :alias_method, :open_uri, :cache_open_uri + end + image_url = %(http://#{resolve_localhost}:9876/fixtures/circle.svg) + attributes = { 'allow-uri-read' => '', 'cache-uri' => '' } + input = %(image::#{image_url}[Circle,100,100,opts=inline]) + output = using_test_webserver { convert_string_to_embedded input, safe: :safe, attributes: attributes } + assert defined? OpenURI::Cache + assert_css 'svg circle', output, 1 + Dir.mktmpdir do |cache_path| + original_cache_path = OpenURI::Cache.cache_path + begin + OpenURI::Cache.cache_path = cache_path + assert_nil OpenURI::Cache.get image_url + 2.times do + output = using_test_webserver { convert_string_to_embedded input, safe: :safe, attributes: attributes } + refute_nil OpenURI::Cache.get image_url + assert_css 'svg circle', output, 1 + end + ensure + OpenURI::Cache.cache_path = original_cache_path + end + end + ensure + OpenURI.singleton_class.send :alias_method, :cache_open_uri, :open_uri + OpenURI.singleton_class.send :remove_method, :open_uri + OpenURI.singleton_class.send :alias_method, :open_uri, :original_open_uri + end + end + test 'converts to alt text for SVG with inline option set if SVG cannot be read' do input = <<~'EOS' [%inline] @@ -2922,6 +2955,40 @@ context 'Blocks' do assert_xpath '//img[@src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="][@alt="Dot"]', output, 1 end + test 'should cache remote image when allow-uri-read, cache-uri, and data-uri are set', unless: ruby_3_1_up? do + begin + if OpenURI.respond_to? :cache_open_uri + OpenURI.singleton_class.send :remove_method, :open_uri + OpenURI.singleton_class.send :alias_method, :open_uri, :cache_open_uri + end + image_url = %(http://#{resolve_localhost}:9876/fixtures/dot.gif) + image_data_uri = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=' + attributes = { 'allow-uri-read' => '', 'cache-uri' => '', 'data-uri' => '' } + input = %(image::#{image_url}[Dot]) + output = using_test_webserver { convert_string_to_embedded input, safe: :safe, attributes: attributes } + assert defined? OpenURI::Cache + assert_xpath %(//img[@src="#{image_data_uri}"][@alt="Dot"]), output, 1 + Dir.mktmpdir do |cache_path| + original_cache_path = OpenURI::Cache.cache_path + begin + OpenURI::Cache.cache_path = cache_path + assert_nil OpenURI::Cache.get image_url + 2.times do + output = using_test_webserver { convert_string_to_embedded input, safe: :safe, attributes: attributes } + refute_nil OpenURI::Cache.get image_url + assert_xpath %(//img[@src="#{image_data_uri}"][@alt="Dot"]), output, 1 + end + ensure + OpenURI::Cache.cache_path = original_cache_path + end + end + ensure + OpenURI.singleton_class.send :alias_method, :cache_open_uri, :open_uri + OpenURI.singleton_class.send :remove_method, :open_uri + OpenURI.singleton_class.send :alias_method, :open_uri, :original_open_uri + end + end + test 'uses remote image uri when data-uri attribute is set and image cannot be retrieved' do image_uri = "http://#{resolve_localhost}:9876/fixtures/missing-image.gif" input = <<~EOS diff --git a/test/test_helper.rb b/test/test_helper.rb index b8d9d0ef..f948694d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -51,6 +51,10 @@ class Minitest::Test Minitest::Test.windows? end + def self.ruby_3_1_up? + (Gem::Version.new RUBY_VERSION) >= (Gem::Version.new '3.1.0') + end + def disk_root %(#{windows? ? (Asciidoctor::ROOT_DIR.partition '/')[0] : ''}/) end |
