Skip to content

Commit

Permalink
Generalize https filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rymohr committed May 15, 2014
1 parent d45627b commit 5e7fd65
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/html/pipeline/https_filter.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
module HTML
class Pipeline
# HTML Filter for replacing http github urls with https versions.
# HTML Filter for replacing http references to :base_url with https versions.
# Subdomain references are not rewritten.
#
# Context options:
# :base_url - The url to force https
class HttpsFilter < Filter
def call
doc.css('a[href^="http://github.com"]').each do |element|
doc.css(%Q(a[href^="#{context[:base_url]}"])).each do |element|
element['href'] = element['href'].sub(/^http:/,'https:')
end
doc
end

# Raise error if :base_url undefined
def validate
needs :base_url
end
end
end
end
end
34 changes: 34 additions & 0 deletions test/html/pipeline/https_filter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "test_helper"

HttpsFilter = HTML::Pipeline::HttpsFilter

class HTML::Pipeline::AutolinkFilterTest < Test::Unit::TestCase
def filter(html, base_url="http://github.com")
HttpsFilter.to_html(html, :base_url => base_url)
end

def test_http
assert_equal %(<a href="https://github.com">github.com</a>),
filter(%(<a href="http://github.com">github.com</a>))
end

def test_https
assert_equal %(<a href="https://github.com">github.com</a>),
filter(%(<a href="https://github.com">github.com</a>))
end

def test_subdomain
assert_equal %(<a href="http://help.github.com">github.com</a>),
filter(%(<a href="http://help.github.com">github.com</a>))
end

def test_other
assert_equal %(<a href="http://github.io">github.io</a>),
filter(%(<a href="http://github.io">github.io</a>))
end

def test_validation
exception = assert_raise(ArgumentError) { HttpsFilter.call(nil, {}) }
assert_match "HTML::Pipeline::HttpsFilter: :base_url", exception.message
end
end

0 comments on commit 5e7fd65

Please sign in to comment.