# encoding: utf-8 rootdir = File.dirname(File.dirname(__FILE__)) $LOAD_PATH.unshift "#{rootdir}/lib" require 'test/unit' require 'cgi' require 'uri' if RUBY_PLATFORM == 'java' require 'rinku-ffi' else require 'rinku' end class RedcarpetAutolinkTest < Test::Unit::TestCase SAFE_CHARS = "{}[]~'" def assert_linked(expected, url) assert_equal expected, Rinku.auto_link(url) end def test_segfault Rinku.auto_link("a+b@d.com+e@f.com", mode=:all) end def test_escapes_quotes assert_linked %(http://website.com/"onmouseover=document.body.style.backgroundColor="pink";//), %(http://website.com/"onmouseover=document.body.style.backgroundColor="pink";//) end def test_global_skip_tags assert_equal Rinku.skip_tags, nil Rinku.skip_tags = ['pre'] assert_equal Rinku.skip_tags, ['pre'] Rinku.skip_tags = ['pa'] url = 'This is just a http://www.pokemon.com test' assert_equal Rinku.auto_link(url), url Rinku.skip_tags = nil assert_not_equal Rinku.auto_link(url), url end def test_auto_link_with_single_trailing_punctuation_and_space url = "http://www.youtube.com" url_result = generate_result(url) assert_equal url_result, Rinku.auto_link(url) assert_equal "link: #{url_result}. foo?", Rinku.auto_link("link: #{url}. foo?") end def test_does_not_segfault assert_linked "< this is just a test", "< this is just a test" end def test_skips_tags html = <<-html This is just a test. http://www.pokemon.com
More test http://www.amd.com
  CODE www.less.es
html result = <<-result This is just a test. http://www.pokemon.com
More test http://www.amd.com
  CODE www.less.es
result assert_equal result, Rinku.auto_link(html, :all, nil, ["div", "a"]) end def test_auto_link_with_brackets link1_raw = 'http://en.wikipedia.org/wiki/Sprite_(computer_graphics)' link1_result = generate_result(link1_raw) assert_equal link1_result, Rinku.auto_link(link1_raw) assert_equal "(link: #{link1_result})", Rinku.auto_link("(link: #{link1_raw})") link2_raw = 'http://en.wikipedia.org/wiki/Sprite_[computer_graphics]' link2_result = generate_result(link2_raw) assert_equal link2_result, Rinku.auto_link(link2_raw) assert_equal "[link: #{link2_result}]", Rinku.auto_link("[link: #{link2_raw}]") link3_raw = 'http://en.wikipedia.org/wiki/Sprite_{computer_graphics}' link3_result = generate_result(link3_raw) assert_equal link3_result, Rinku.auto_link(link3_raw) assert_equal "{link: #{link3_result}}", Rinku.auto_link("{link: #{link3_raw}}") end def test_auto_link_with_multiple_trailing_punctuations url = "http://youtube.com" url_result = generate_result(url) assert_equal url_result, Rinku.auto_link(url) assert_equal "(link: #{url_result}).", Rinku.auto_link("(link: #{url}).") end def test_auto_link_with_block url = "http://api.rubyonrails.com/Foo.html" email = "fantabulous@shiznadel.ic" assert_equal %(

#{url[0...7]}...
#{email[0...7]}...

), Rinku.auto_link("

#{url}
#{email}

") { |_url| _url[0...7] + '...' } end def test_auto_link_with_block_with_html pic = "http://example.com/pic.png" url = "http://example.com/album?a&b=c" expect = %(My pic: -- full album here #{generate_result(url)}) text = "My pic: #{pic} -- full album here #{CGI.escapeHTML url}" assert_equal expect, Rinku.auto_link(text) { |link| if link =~ /\.(jpg|gif|png|bmp|tif)$/i %() else link end } end def test_auto_link_already_linked linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com') linked2 = %('www.example.com') linked3 = %('www.example.com') linked4 = %('www.example.com') linked5 = %('close www.example.com') assert_equal linked1, Rinku.auto_link(linked1) assert_equal linked2, Rinku.auto_link(linked2) assert_equal linked3, Rinku.auto_link(linked3) assert_equal linked4, Rinku.auto_link(linked4) assert_equal linked5, Rinku.auto_link(linked5) linked_email = %Q(Mail me) assert_equal linked_email, Rinku.auto_link(linked_email) end def test_auto_link_at_eol url1 = "http://api.rubyonrails.com/Foo.html" url2 = "http://www.ruby-doc.org/core/Bar.html" assert_equal %(

#{url1}
#{url2}

), Rinku.auto_link("

#{url1}
#{url2}

") end def test_block link = Rinku.auto_link("Find ur favorite pokeman @ http://www.pokemon.com") do |url| assert_equal url, "http://www.pokemon.com" "POKEMAN WEBSITE" end assert_equal link, "Find ur favorite pokeman @ POKEMAN WEBSITE" end def test_autolink_works url = "http://example.com/" assert_linked "#{url}", url end def test_autolink_options_for_short_domains url = "http://google" linked_url = "#{url}" flags = Rinku::AUTOLINK_SHORT_DOMAINS # Specifying use short_domains in the args url = "http://google" linked_url = "#{url}" assert_equal Rinku.auto_link(url, nil, nil, nil, flags), linked_url # Specifying no short_domains in the args url = "http://google" linked_url = "#{url}" assert_equal Rinku.auto_link(url, nil, nil, nil, 0), url end def test_not_autolink_www assert_linked "Awww... man", "Awww... man" end def test_does_not_terminate_on_dash url = "http://example.com/Notification_Center-GitHub-20101108-140050.jpg" assert_linked "#{url}", url end def test_does_not_include_trailing_gt url = "http://example.com" assert_linked "<#{url}>", "<#{url}>" end def test_links_with_anchors url = "https://github.com/github/hubot/blob/master/scripts/cream.js#L20-20" assert_linked "#{url}", url end def test_links_like_rails urls = %w(http://www.rubyonrails.com http://www.rubyonrails.com:80 http://www.rubyonrails.com/~minam https://www.rubyonrails.com/~minam http://www.rubyonrails.com/~minam/url%20with%20spaces http://www.rubyonrails.com/foo.cgi?something=here http://www.rubyonrails.com/foo.cgi?something=here&and=here http://www.rubyonrails.com/contact;new http://www.rubyonrails.com/contact;new%20with%20spaces http://www.rubyonrails.com/contact;new?with=query&string=params http://www.rubyonrails.com/~minam/contact;new?with=query&string=params http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007 http://www.mail-archive.com/rails@lists.rubyonrails.org/ http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1 http://en.wikipedia.org/wiki/Sprite_(computer_graphics) http://en.wikipedia.org/wiki/Texas_hold%27em https://www.google.com/doku.php?id=gps:resource:scs:start ) urls.each do |url| assert_linked %(#{CGI.escapeHTML url}), CGI.escapeHTML(url) end end def test_links_like_autolink_rails email_raw = 'david@loudthinking.com' email_result = %{#{email_raw}} email2_raw = '+david@loudthinking.com' email2_result = %{#{email2_raw}} link_raw = 'http://www.rubyonrails.com' link_result = %{#{link_raw}} link_result_with_options = %{#{link_raw}} link2_raw = 'www.rubyonrails.com' link2_result = %{#{link2_raw}} link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281' link3_result = %{#{link3_raw}} link4_raw = CGI.escapeHTML 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123' link4_result = %{#{link4_raw}} link5_raw = 'http://foo.example.com:3000/controller/action' link5_result = %{#{link5_raw}} link6_raw = 'http://foo.example.com:3000/controller/action+pack' link6_result = %{#{link6_raw}} link7_raw = CGI.escapeHTML 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123' link7_result = %{#{link7_raw}} link8_raw = 'http://foo.example.com:3000/controller/action.html' link8_result = %{#{link8_raw}} link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html' link9_result = %{#{link9_raw}} link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/' link10_result = %{#{link10_raw}} assert_linked %(Go to #{link_result} and say hello to #{email_result}), "Go to #{link_raw} and say hello to #{email_raw}" assert_linked %(

Link #{link_result}

), "

Link #{link_raw}

" assert_linked %(

#{link_result} Link

), "

#{link_raw} Link

" assert_linked %(Go to #{link_result}.), %(Go to #{link_raw}.) assert_linked %(

Go to #{link_result}, then say hello to #{email_result}.

), %(

Go to #{link_raw}, then say hello to #{email_raw}.

) assert_linked %(

Link #{link2_result}

), "

Link #{link2_raw}

" assert_linked %(

#{link2_result} Link

), "

#{link2_raw} Link

" assert_linked %(Go to #{link2_result}.), %(Go to #{link2_raw}.) assert_linked %(

Say hello to #{email_result}, then go to #{link2_result},

), %(

Say hello to #{email_raw}, then go to #{link2_raw},

) assert_linked %(

Link #{link3_result}

), "

Link #{link3_raw}

" assert_linked %(

#{link3_result} Link

), "

#{link3_raw} Link

" assert_linked %(Go to #{link3_result}.), %(Go to #{link3_raw}.) assert_linked %(

Go to #{link3_result}. seriously, #{link3_result}? i think I'll say hello to #{email_result}. instead.

), %(

Go to #{link3_raw}. seriously, #{link3_raw}? i think I'll say hello to #{email_raw}. instead.

) assert_linked %(

Link #{link4_result}

), "

Link #{link4_raw}

" assert_linked %(

#{link4_result} Link

), "

#{link4_raw} Link

" assert_linked %(

#{link5_result} Link

), "

#{link5_raw} Link

" assert_linked %(

#{link6_result} Link

), "

#{link6_raw} Link

" assert_linked %(

#{link7_result} Link

), "

#{link7_raw} Link

" assert_linked %(

Link #{link8_result}

), "

Link #{link8_raw}

" assert_linked %(

#{link8_result} Link

), "

#{link8_raw} Link

" assert_linked %(Go to #{link8_result}.), %(Go to #{link8_raw}.) assert_linked %(

Go to #{link8_result}. seriously, #{link8_result}? i think I'll say hello to #{email_result}. instead.

), %(

Go to #{link8_raw}. seriously, #{link8_raw}? i think I'll say hello to #{email_raw}. instead.

) assert_linked %(

Link #{link9_result}

), "

Link #{link9_raw}

" assert_linked %(

#{link9_result} Link

), "

#{link9_raw} Link

" assert_linked %(Go to #{link9_result}.), %(Go to #{link9_raw}.) assert_linked %(

Go to #{link9_result}. seriously, #{link9_result}? i think I'll say hello to #{email_result}. instead.

), %(

Go to #{link9_raw}. seriously, #{link9_raw}? i think I'll say hello to #{email_raw}. instead.

) assert_linked %(

#{link10_result} Link

), "

#{link10_raw} Link

" assert_linked email2_result, email2_raw assert_linked "#{link_result} #{link_result} #{link_result}", "#{link_raw} #{link_raw} #{link_raw}" assert_linked 'Ruby On Rails', 'Ruby On Rails' end if "".respond_to?(:force_encoding) def test_copies_source_encoding str = "http://www.bash.org" ret = Rinku.auto_link str assert_equal str.encoding, ret.encoding str.encode! 'binary' ret = Rinku.auto_link str assert_equal str.encoding, ret.encoding end end def generate_result(link_text, href = nil) href ||= link_text %{#{CGI.escapeHTML link_text}} end end