1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
# frozen_string_literal: false
require 'test/unit'
require 'uri/mailto'
class URI::TestMailTo < Test::Unit::TestCase
def setup
@u = URI::MailTo
end
def teardown
end
def uri_to_ary(uri)
uri.class.component.collect {|c| uri.send(c)}
end
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"}
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"}
# mailto:infobot@example.com?subject=current-issue
ok << ["mailto:infobot@example.com?subject=current-issue"]
ok[-1] << ["infobot@example.com", ["subject=current-issue"]]
ok[-1] << {:to => "infobot@example.com",
: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"]]
ok[-1] << {:to => "infobot@example.com",
: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"]
ok[-1] << ["infobot@example.com",
["body=send%20current-issue%0D%0Asend%20index"]]
ok[-1] << {:to => "infobot@example.com",
: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"]
ok[-1] << ["foobar@example.com",
["In-Reply-To=%3c3469A91.D10AF4C@example.com"]]
ok[-1] << {:to => "foobar@example.com",
: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"]]
ok[-1] << {:to => "majordomo@example.com",
: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"]]
ok[-1] << {:to => "joe@example.com",
: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"]
ok[-1] << [nil,
["to=joe@example.com", "cc=bob@example.com", "body=hello"]]
ok[-1] << {:headers => ["to=joe@example.com",
"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"]]
ok[-1] << {:to => "unlikely%3Faddress@example.com",
:headers => ["blat=foop"]}
# 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']]}
# 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' }
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()", []]
# mailto:/example.com/ ; WRONG, not a mail address
bad << ["/example.com/", []]
# '=' which is in hname or hvalue is wrong.
bad << ["foo@example.jp?subject=1+1=2", []]
ok.each do |x|
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)
end
bad.each do |x|
assert_raise(URI::InvalidURIError) {
URI.parse(x)
}
assert_raise(URI::InvalidComponentError) {
@u.build(x)
}
end
assert_equal(ok_all, ok.flatten.join("\0"))
end
def test_initializer
assert_raise(URI::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(URI::InvalidComponentError) do
u.to = '#1@mail.com'
end
assert_raise(URI::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
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
def test_select
u = URI.parse('mailto:joe@example.com?cc=bob@example.com&body=hello')
assert_equal(uri_to_ary(u), u.select(*u.component))
assert_raise(ArgumentError) do
u.select(:scheme, :host, :not_exist, :port)
end
end
end
|