diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2021-12-26 23:41:32 -0700 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2021-12-27 03:41:48 -0700 |
| commit | 317823d41780e0e79cbfacfeabb72fb8002c6ab4 (patch) | |
| tree | 13062866aeab11c0dc60d393f0ab2e5fe87badac /test | |
| parent | df24db273a9a84b52bec5f008a8e5e26ddf272dd (diff) | |
add tests for open-uri/cached integration that is activated by the cache-uri attribute
Diffstat (limited to 'test')
| -rw-r--r-- | test/blocks_test.rb | 67 | ||||
| -rw-r--r-- | test/test_helper.rb | 4 |
2 files changed, 71 insertions, 0 deletions
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 |
