2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-10-05 02:09:26 -04:00
|
|
|
require 'test/unit'
|
2003-10-04 00:02:16 -04:00
|
|
|
require 'uri/mailto'
|
2003-10-05 02:09:26 -04:00
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
module URI
|
|
|
|
|
2003-10-05 02:09:26 -04:00
|
|
|
|
|
|
|
class TestMailTo < Test::Unit::TestCase
|
2003-10-04 00:02:16 -04:00
|
|
|
def setup
|
|
|
|
@u = URI::MailTo
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
end
|
|
|
|
|
2003-10-05 02:09:26 -04:00
|
|
|
def uri_to_ary(uri)
|
|
|
|
uri.class.component.collect {|c| uri.send(c)}
|
|
|
|
end
|
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
def test_build
|
|
|
|
ok = []
|
|
|
|
bad = []
|
|
|
|
|
|
|
|
# RFC2368, 6. Examples
|
|
|
|
# mailto:chris@example.com
|
|
|
|
ok << ["mailto:chris@example.com"]
|
|
|
|
ok[-1] << ["chris@example.com", nil]
|
|
|
|
ok[-1] << {:to => "chris@example.com"}
|
|
|
|
|
2014-06-27 15:26:43 -04:00
|
|
|
ok << ["mailto:foo+@example.com,bar@example.com"]
|
|
|
|
ok[-1] << [["foo+@example.com", "bar@example.com"], nil]
|
|
|
|
ok[-1] << {:to => "foo+@example.com,bar@example.com"}
|
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
# mailto:infobot@example.com?subject=current-issue
|
|
|
|
ok << ["mailto:infobot@example.com?subject=current-issue"]
|
|
|
|
ok[-1] << ["infobot@example.com", ["subject=current-issue"]]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << {:to => "infobot@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
:headers => ["subject=current-issue"]}
|
|
|
|
|
|
|
|
# mailto:infobot@example.com?body=send%20current-issue
|
|
|
|
ok << ["mailto:infobot@example.com?body=send%20current-issue"]
|
|
|
|
ok[-1] << ["infobot@example.com", ["body=send%20current-issue"]]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << {:to => "infobot@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
:headers => ["body=send%20current-issue"]}
|
|
|
|
|
|
|
|
# mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index
|
|
|
|
ok << ["mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index"]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << ["infobot@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
["body=send%20current-issue%0D%0Asend%20index"]]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << {:to => "infobot@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
:headers => ["body=send%20current-issue%0D%0Asend%20index"]}
|
|
|
|
|
|
|
|
# mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com
|
|
|
|
ok << ["mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com"]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << ["foobar@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
["In-Reply-To=%3c3469A91.D10AF4C@example.com"]]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << {:to => "foobar@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
:headers => ["In-Reply-To=%3c3469A91.D10AF4C@example.com"]}
|
|
|
|
|
|
|
|
# mailto:majordomo@example.com?body=subscribe%20bamboo-l
|
|
|
|
ok << ["mailto:majordomo@example.com?body=subscribe%20bamboo-l"]
|
|
|
|
ok[-1] << ["majordomo@example.com", ["body=subscribe%20bamboo-l"]]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << {:to => "majordomo@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
:headers => ["body=subscribe%20bamboo-l"]}
|
|
|
|
|
|
|
|
# mailto:joe@example.com?cc=bob@example.com&body=hello
|
|
|
|
ok << ["mailto:joe@example.com?cc=bob@example.com&body=hello"]
|
|
|
|
ok[-1] << ["joe@example.com", ["cc=bob@example.com", "body=hello"]]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << {:to => "joe@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
:headers => ["cc=bob@example.com", "body=hello"]}
|
|
|
|
|
|
|
|
# mailto:?to=joe@example.com&cc=bob@example.com&body=hello
|
|
|
|
ok << ["mailto:?to=joe@example.com&cc=bob@example.com&body=hello"]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << [nil,
|
2003-10-04 00:02:16 -04:00
|
|
|
["to=joe@example.com", "cc=bob@example.com", "body=hello"]]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << {:headers => ["to=joe@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
"cc=bob@example.com", "body=hello"]}
|
|
|
|
|
|
|
|
# mailto:gorby%25kremvax@example.com
|
|
|
|
ok << ["mailto:gorby%25kremvax@example.com"]
|
|
|
|
ok[-1] << ["gorby%25kremvax@example.com", nil]
|
|
|
|
ok[-1] << {:to => "gorby%25kremvax@example.com"}
|
|
|
|
|
|
|
|
# mailto:unlikely%3Faddress@example.com?blat=foop
|
|
|
|
ok << ["mailto:unlikely%3Faddress@example.com?blat=foop"]
|
|
|
|
ok[-1] << ["unlikely%3Faddress@example.com", ["blat=foop"]]
|
2009-03-05 22:56:38 -05:00
|
|
|
ok[-1] << {:to => "unlikely%3Faddress@example.com",
|
2003-10-04 00:02:16 -04:00
|
|
|
:headers => ["blat=foop"]}
|
|
|
|
|
2012-01-23 03:12:52 -05:00
|
|
|
# mailto:john@example.com?Subject=Ruby&Cc=jack@example.com
|
|
|
|
ok << ["mailto:john@example.com?Subject=Ruby&Cc=jack@example.com"]
|
|
|
|
ok[-1] << ['john@example.com', [['Subject', 'Ruby'], ['Cc', 'jack@example.com']]]
|
|
|
|
ok[-1] << {:to=>"john@example.com", :headers=>[["Subject", "Ruby"], ["Cc", "jack@example.com"]]}
|
|
|
|
|
|
|
|
# mailto:listman@example.com?subject=subscribe
|
|
|
|
ok << ["mailto:listman@example.com?subject=subscribe"]
|
|
|
|
ok[-1] << {:to => 'listman@example.com', :headers => [['subject', 'subscribe']]}
|
|
|
|
ok[-1] << {:to => 'listman@example.com', :headers => [['subject', 'subscribe']]}
|
|
|
|
|
2016-09-12 00:46:27 -04:00
|
|
|
# mailto:listman@example.com?subject=subscribe
|
|
|
|
ok << ["mailto:listman@example.com?subject=subscribe"]
|
|
|
|
ok[-1] << {:to => 'listman@example.com', :headers => { 'subject' => 'subscribe' }}
|
|
|
|
ok[-1] << {:to => 'listman@example.com', :headers => 'subject=subscribe' }
|
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
ok_all = ok.flatten.join("\0")
|
|
|
|
|
|
|
|
# mailto:joe@example.com?cc=bob@example.com?body=hello ; WRONG!
|
|
|
|
bad << ["joe@example.com", ["cc=bob@example.com?body=hello"]]
|
|
|
|
|
|
|
|
# mailto:javascript:alert()
|
|
|
|
bad << ["javascript:alert()", []]
|
|
|
|
|
2015-01-14 04:06:13 -05:00
|
|
|
# mailto:/example.com/ ; WRONG, not a mail address
|
|
|
|
bad << ["/example.com/", []]
|
|
|
|
|
2014-06-23 22:58:45 -04:00
|
|
|
# '=' which is in hname or hvalue is wrong.
|
|
|
|
bad << ["foo@example.jp?subject=1+1=2", []]
|
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
ok.each do |x|
|
2016-07-02 05:05:05 -04:00
|
|
|
assert_equal(x[0], URI.parse(x[0]).to_s)
|
|
|
|
assert_equal(x[0], @u.build(x[1]).to_s)
|
|
|
|
assert_equal(x[0], @u.build(x[2]).to_s)
|
2003-10-04 00:02:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
bad.each do |x|
|
2016-07-02 05:05:05 -04:00
|
|
|
assert_raise(URI::InvalidURIError) {
|
|
|
|
URI.parse(x)
|
|
|
|
}
|
2014-06-23 22:58:45 -04:00
|
|
|
assert_raise(URI::InvalidComponentError) {
|
2003-10-04 00:02:16 -04:00
|
|
|
@u.build(x)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(ok_all, ok.flatten.join("\0"))
|
|
|
|
end
|
|
|
|
|
2016-09-12 00:46:27 -04:00
|
|
|
def test_initializer
|
|
|
|
assert_raise(InvalidComponentError) do
|
|
|
|
URI::MailTo.new('mailto', 'sdmitry:bla', 'localhost', '2000', nil,
|
|
|
|
'joe@example.com', nil, nil, 'subject=Ruby')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_check_to
|
|
|
|
u = URI::MailTo.build(['joe@example.com', 'subject=Ruby'])
|
|
|
|
|
|
|
|
assert_raise(InvalidComponentError) do
|
|
|
|
u.to = '#1@mail.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(InvalidComponentError) do
|
|
|
|
u.to = '@invalid.email'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
|
|
|
u = URI::MailTo.build([nil, 'subject=Ruby'])
|
|
|
|
|
|
|
|
u.send(:set_to, nil)
|
|
|
|
assert_equal('mailto:?subject=Ruby', u.to_s)
|
|
|
|
|
|
|
|
u.fragment = 'test'
|
|
|
|
assert_equal('mailto:?subject=Ruby#test', u.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_mailtext
|
|
|
|
results = []
|
|
|
|
results << ["To: ruby-list@ruby-lang.org\nSubject: subscribe\n\n\n"]
|
|
|
|
results[-1] << { to: 'ruby-list@ruby-lang.org', headers: { 'subject' => 'subscribe' } }
|
|
|
|
|
|
|
|
results << ["To: ruby-list@ruby-lang.org\n\nBody\n"]
|
|
|
|
results[-1] << { to: 'ruby-list@ruby-lang.org', headers: { 'body' => 'Body' } }
|
|
|
|
|
|
|
|
results << ["To: ruby-list@ruby-lang.org, cc@ruby-lang.org\n\n\n"]
|
|
|
|
results[-1] << { to: 'ruby-list@ruby-lang.org', headers: { 'to' => 'cc@ruby-lang.org' } }
|
|
|
|
|
|
|
|
results.each do |expected, params|
|
|
|
|
u = URI::MailTo.build(params)
|
|
|
|
assert_equal(expected, u.to_mailtext)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
def test_select
|
|
|
|
u = URI.parse('mailto:joe@example.com?cc=bob@example.com&body=hello')
|
2003-10-05 02:09:26 -04:00
|
|
|
assert_equal(uri_to_ary(u), u.select(*u.component))
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ArgumentError) do
|
2003-10-04 00:02:16 -04:00
|
|
|
u.select(:scheme, :host, :not_exist, :port)
|
|
|
|
end
|
|
|
|
end
|
2016-12-12 10:24:28 -05:00
|
|
|
|
|
|
|
def test_to_mailtext
|
|
|
|
u = URI.parse('mailto:ruby-list@ruby-lang.org?Subject=subscribe&cc=myaddr')
|
|
|
|
assert_equal "To: ruby-list@ruby-lang.org\nSubject: subscribe\nCc: myaddr\n\n\n",
|
|
|
|
u.to_mailtext
|
|
|
|
end
|
2003-10-04 00:02:16 -04:00
|
|
|
end
|
|
|
|
|
2003-10-05 02:09:26 -04:00
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
end
|