Sha256: 4e17b9c3e0116d67b774d263b44cd71ffa6b6734e2271891164014a5bca117fe

Contents?: true

Size: 1.72 KB

Versions: 13

Compression:

Stored size: 1.72 KB

Contents

require "test_helper"

class HTML::Pipeline::AbsoluteSourceFilterTest < Minitest::Test
  AbsoluteSourceFilter = HTML::Pipeline::AbsoluteSourceFilter

  def setup
    @image_base_url = 'http://assets.example.com'
    @image_subpage_url = 'http://blog.example.com/a/post'
    @options = {
      :image_base_url    => @image_base_url,
      :image_subpage_url => @image_subpage_url
    }
  end

  def test_rewrites_root_urls
    orig = %(<p><img src="/img.png"></p>)
    assert_equal "<p><img src=\"#{@image_base_url}/img.png\"></p>",
      AbsoluteSourceFilter.call(orig, @options).to_s
  end

  def test_rewrites_relative_urls
    orig = %(<p><img src="post/img.png"></p>)
    assert_equal "<p><img src=\"#{@image_subpage_url}/img.png\"></p>",
      AbsoluteSourceFilter.call(orig, @options).to_s
  end

  def test_does_not_rewrite_absolute_urls
    orig = %(<p><img src="http://other.example.com/img.png"></p>)
    result = AbsoluteSourceFilter.call(orig, @options).to_s
    refute_match /@image_base_url/, result
    refute_match /@image_subpage_url/, result
  end

  def test_fails_when_context_is_missing
    assert_raises RuntimeError do
      AbsoluteSourceFilter.call("<img src=\"img.png\">", {})
    end
    assert_raises RuntimeError do
      AbsoluteSourceFilter.call("<img src=\"/img.png\">", {})
    end
  end
  
  def test_tells_you_where_context_is_required
    exception = assert_raises(RuntimeError) { 
      AbsoluteSourceFilter.call("<img src=\"img.png\">", {}) 
    }
    assert_match 'HTML::Pipeline::AbsoluteSourceFilter', exception.message

    exception = assert_raises(RuntimeError) { 
      AbsoluteSourceFilter.call("<img src=\"/img.png\">", {}) 
    }
    assert_match 'HTML::Pipeline::AbsoluteSourceFilter', exception.message
  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
html-pipeline-2.4.1 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-2.4.0 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-2.3.0 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-2.2.4 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-2.2.3 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-2.2.2 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-2.2.1 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-2.2.0 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-2.1.0 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-2.0 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-1.11.0 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-1.10.0 test/html/pipeline/absolute_source_filter_test.rb
html-pipeline-1.9.0 test/html/pipeline/absolute_source_filter_test.rb