2005-01-16 23:40:44 +01:00
|
|
|
#!/bin/env ruby
|
|
|
|
|
|
|
|
require File.dirname(__FILE__) + '/../test_helper'
|
2005-01-15 21:26:54 +01:00
|
|
|
require 'chunks/uri'
|
|
|
|
|
|
|
|
class URITest < Test::Unit::TestCase
|
|
|
|
include ChunkMatch
|
|
|
|
|
|
|
|
def test_non_matches
|
|
|
|
assert_no_match(URIChunk.pattern, 'There is no URI here')
|
|
|
|
assert_no_match(URIChunk.pattern, 'One gemstone is the garnet:reddish in colour, like ruby')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_simple_uri
|
2005-01-18 20:15:48 +01:00
|
|
|
# Simplest case
|
2005-01-15 21:26:54 +01:00
|
|
|
match(URIChunk, 'http://www.example.com',
|
|
|
|
:scheme =>'http', :host =>'www.example.com', :path => nil,
|
|
|
|
:link_text => 'http://www.example.com'
|
|
|
|
)
|
2005-01-18 20:15:48 +01:00
|
|
|
# With trailing slash
|
2005-01-15 21:26:54 +01:00
|
|
|
match(URIChunk, 'http://www.example.com/',
|
|
|
|
:scheme =>'http', :host =>'www.example.com', :path => '/',
|
|
|
|
:link_text => 'http://www.example.com/'
|
|
|
|
)
|
2005-01-18 20:15:48 +01:00
|
|
|
# Without http://
|
2005-01-15 21:26:54 +01:00
|
|
|
match(URIChunk, 'www.example.com',
|
|
|
|
:scheme =>'http', :host =>'www.example.com', :link_text => 'www.example.com'
|
|
|
|
)
|
2005-01-18 20:15:48 +01:00
|
|
|
# two parts
|
2005-01-15 21:26:54 +01:00
|
|
|
match(URIChunk, 'example.com',
|
|
|
|
:scheme =>'http',:host =>'example.com', :link_text => 'example.com'
|
|
|
|
)
|
2005-01-18 20:15:48 +01:00
|
|
|
# "unusual" base domain (was a bug in an early version)
|
2005-01-15 21:26:54 +01:00
|
|
|
match(URIChunk, 'http://example.com.au/',
|
|
|
|
:scheme =>'http', :host =>'example.com.au', :link_text => 'http://example.com.au/'
|
|
|
|
)
|
2005-01-18 20:15:48 +01:00
|
|
|
# "unusual" base domain without http://
|
2005-01-15 21:26:54 +01:00
|
|
|
match(URIChunk, 'example.com.au',
|
|
|
|
:scheme =>'http', :host =>'example.com.au', :link_text => 'example.com.au'
|
|
|
|
)
|
2005-01-18 20:15:48 +01:00
|
|
|
# Another "unusual" base domain
|
2005-01-15 21:26:54 +01:00
|
|
|
match(URIChunk, 'http://www.example.co.uk/',
|
|
|
|
:scheme =>'http', :host =>'www.example.co.uk',
|
|
|
|
:link_text => 'http://www.example.co.uk/'
|
|
|
|
)
|
|
|
|
match(URIChunk, 'example.co.uk',
|
|
|
|
:scheme =>'http', :host =>'example.co.uk', :link_text => 'example.co.uk'
|
|
|
|
)
|
2005-01-18 20:15:48 +01:00
|
|
|
# With some path at the end
|
2005-01-15 21:26:54 +01:00
|
|
|
match(URIChunk, 'http://moinmoin.wikiwikiweb.de/HelpOnNavigation',
|
|
|
|
:scheme => 'http', :host => 'moinmoin.wikiwikiweb.de', :path => '/HelpOnNavigation',
|
|
|
|
:link_text => 'http://moinmoin.wikiwikiweb.de/HelpOnNavigation'
|
|
|
|
)
|
2005-01-18 20:15:48 +01:00
|
|
|
# With some path at the end, and withot http:// prefix
|
2005-01-15 21:26:54 +01:00
|
|
|
match(URIChunk, 'moinmoin.wikiwikiweb.de/HelpOnNavigation',
|
|
|
|
:scheme => 'http', :host => 'moinmoin.wikiwikiweb.de', :path => '/HelpOnNavigation',
|
|
|
|
:link_text => 'moinmoin.wikiwikiweb.de/HelpOnNavigation'
|
|
|
|
)
|
2005-01-18 20:15:48 +01:00
|
|
|
# With a port number
|
|
|
|
match(URIChunk, 'http://www.example.com:80',
|
|
|
|
:scheme =>'http', :host =>'www.example.com', :port => '80', :path => nil,
|
|
|
|
:link_text => 'http://www.example.com:80')
|
|
|
|
# With a port number and a path
|
|
|
|
match(URIChunk, 'http://www.example.com.tw:80/HelpOnNavigation',
|
|
|
|
:scheme =>'http', :host =>'www.example.com.tw', :port => '80', :path => '/HelpOnNavigation',
|
|
|
|
:link_text => 'http://www.example.com.tw:80/HelpOnNavigation')
|
|
|
|
# With a query
|
|
|
|
match(URIChunk, 'http://www.example.com.tw:80/HelpOnNavigation?arg=val',
|
|
|
|
:scheme =>'http', :host =>'www.example.com.tw', :port => '80', :path => '/HelpOnNavigation',
|
|
|
|
:query => 'arg=val',
|
|
|
|
:link_text => 'http://www.example.com.tw:80/HelpOnNavigation?arg=val')
|
|
|
|
# Query with two arguments
|
|
|
|
match(URIChunk, 'http://www.example.com.tw:80/HelpOnNavigation?arg=val&arg2=val2',
|
|
|
|
:scheme =>'http', :host =>'www.example.com.tw', :port => '80', :path => '/HelpOnNavigation',
|
|
|
|
:query => 'arg=val&arg2=val2',
|
|
|
|
:link_text => 'http://www.example.com.tw:80/HelpOnNavigation?arg=val&arg2=val2')
|
|
|
|
# HTTPS
|
|
|
|
match(URIChunk, 'https://www.example.com',
|
|
|
|
:scheme =>'https', :host =>'www.example.com', :port => nil, :path => nil, :query => nil,
|
|
|
|
:link_text => 'https://www.example.com')
|
|
|
|
# FTP
|
|
|
|
match(URIChunk, 'ftp://www.example.com',
|
|
|
|
:scheme =>'ftp', :host =>'www.example.com', :port => nil, :path => nil, :query => nil,
|
|
|
|
:link_text => 'ftp://www.example.com')
|
|
|
|
# mailto
|
2005-01-18 21:29:10 +01:00
|
|
|
match(URIChunk, 'mailto:jdoe123@example.com',
|
2005-01-18 20:15:48 +01:00
|
|
|
:scheme =>'mailto', :host =>'example.com', :port => nil, :path => nil, :query => nil,
|
2005-01-18 21:29:10 +01:00
|
|
|
:user => 'jdoe123', :link_text => 'mailto:jdoe123@example.com')
|
2005-01-18 20:15:48 +01:00
|
|
|
# something nonexistant
|
|
|
|
match(URIChunk, 'foobar://www.example.com',
|
|
|
|
:scheme =>'foobar', :host =>'www.example.com', :port => nil, :path => nil, :query => nil,
|
|
|
|
:link_text => 'foobar://www.example.com')
|
|
|
|
|
|
|
|
# Soap opera (the most complex case imaginable... well, not really, there should be more evil)
|
|
|
|
match(URIChunk, 'http://www.example.com.tw:80/~jdoe123/Help%20Me%20?arg=val&arg2=val2',
|
|
|
|
:scheme =>'http', :host =>'www.example.com.tw', :port => '80',
|
|
|
|
:path => '/~jdoe123/Help%20Me%20', :query => 'arg=val&arg2=val2',
|
|
|
|
:link_text => 'http://www.example.com.tw:80/~jdoe123/Help%20Me%20?arg=val&arg2=val2')
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_email_uri
|
|
|
|
match(URIChunk, 'mail@example.com',
|
|
|
|
:user => 'mail', :host => 'example.com', :link_text => 'mail@example.com'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_non_email
|
|
|
|
# The @ is part of the normal text, but 'example.com' is marked up.
|
|
|
|
match(URIChunk, 'Not an email: @example.com', :user => nil, :uri => 'http://example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_non_uri
|
|
|
|
assert_no_match(URIChunk.pattern, 'httpd.conf')
|
|
|
|
assert_no_match(URIChunk.pattern, 'libproxy.so')
|
2005-01-18 20:15:48 +01:00
|
|
|
assert_no_match(URIChunk.pattern, 'ld.so.conf')
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_uri_in_text
|
|
|
|
match(URIChunk, 'Go to: http://www.example.com/', :host => 'www.example.com', :path =>'/')
|
|
|
|
match(URIChunk, 'http://www.example.com/ is a link.', :host => 'www.example.com')
|
|
|
|
match(URIChunk,
|
2005-01-18 20:15:48 +01:00
|
|
|
'Email david@loudthinking.com',
|
|
|
|
:scheme =>'mailto', :user =>'david', :host =>'loudthinking.com')
|
|
|
|
# check that trailing punctuation is not included in the hostname
|
|
|
|
match(URIChunk, '"link":http://fake.link.com.', :scheme => 'http', :host => 'fake.link.com')
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_uri_in_parentheses
|
|
|
|
match(URIChunk, 'URI (http://brackets.com.de) in brackets', :host => 'brackets.com.de')
|
|
|
|
match(URIChunk, 'because (as shown at research.net) the results', :host => 'research.net')
|
|
|
|
match(URIChunk,
|
|
|
|
'A wiki (http://wiki.org/wiki.cgi?WhatIsWiki) page',
|
|
|
|
:scheme => 'http', :host => 'wiki.org', :path => '/wiki.cgi', :query => 'WhatIsWiki'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_uri_list_item
|
|
|
|
match(
|
|
|
|
URIChunk,
|
|
|
|
'* http://www.btinternet.com/~mail2minh/SonyEricssonP80xPlatform.sis',
|
|
|
|
:path => '/~mail2minh/SonyEricssonP80xPlatform.sis'
|
|
|
|
)
|
|
|
|
end
|
2005-01-16 17:04:45 +01:00
|
|
|
|
2005-01-18 21:29:10 +01:00
|
|
|
def test_interesting_uri_with__comma
|
|
|
|
# Counter-intuitively, this URL matches, but the query part includes the trailing comma.
|
|
|
|
# It has no way to know that the query does not include the comma.
|
2005-01-16 17:04:45 +01:00
|
|
|
match(
|
2005-01-18 21:29:10 +01:00
|
|
|
URIChunk,
|
|
|
|
"This text contains a URL http://someplace.org:8080/~person/stuff.cgi?arg=val, doesn't it?",
|
|
|
|
:scheme => 'http', :host => 'someplace.org', :port => '8080', :path => '/~person/stuff.cgi',
|
|
|
|
:query => 'arg=val,')
|
2005-01-16 17:04:45 +01:00
|
|
|
end
|
2005-01-18 20:15:48 +01:00
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|