1 # frozen_string_literal: false
5 class URI::TestMailTo < Test::Unit::TestCase
14 uri.class.component.collect {|c| uri.send(c)}
21 # RFC2368, 6. Examples
22 # mailto:chris@example.com
23 ok << ["mailto:chris@example.com"]
24 ok[-1] << ["chris@example.com", nil]
25 ok[-1] << {:to => "chris@example.com"}
27 ok << ["mailto:foo+@example.com,bar@example.com"]
28 ok[-1] << [["foo+@example.com", "bar@example.com"], nil]
29 ok[-1] << {:to => "foo+@example.com,bar@example.com"}
31 # mailto:infobot@example.com?subject=current-issue
32 ok << ["mailto:infobot@example.com?subject=current-issue"]
33 ok[-1] << ["infobot@example.com", ["subject=current-issue"]]
34 ok[-1] << {:to => "infobot@example.com",
35 :headers => ["subject=current-issue"]}
37 # mailto:infobot@example.com?body=send%20current-issue
38 ok << ["mailto:infobot@example.com?body=send%20current-issue"]
39 ok[-1] << ["infobot@example.com", ["body=send%20current-issue"]]
40 ok[-1] << {:to => "infobot@example.com",
41 :headers => ["body=send%20current-issue"]}
43 # mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index
44 ok << ["mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index"]
45 ok[-1] << ["infobot@example.com",
46 ["body=send%20current-issue%0D%0Asend%20index"]]
47 ok[-1] << {:to => "infobot@example.com",
48 :headers => ["body=send%20current-issue%0D%0Asend%20index"]}
50 # mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com
51 ok << ["mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com"]
52 ok[-1] << ["foobar@example.com",
53 ["In-Reply-To=%3c3469A91.D10AF4C@example.com"]]
54 ok[-1] << {:to => "foobar@example.com",
55 :headers => ["In-Reply-To=%3c3469A91.D10AF4C@example.com"]}
57 # mailto:majordomo@example.com?body=subscribe%20bamboo-l
58 ok << ["mailto:majordomo@example.com?body=subscribe%20bamboo-l"]
59 ok[-1] << ["majordomo@example.com", ["body=subscribe%20bamboo-l"]]
60 ok[-1] << {:to => "majordomo@example.com",
61 :headers => ["body=subscribe%20bamboo-l"]}
63 # mailto:joe@example.com?cc=bob@example.com&body=hello
64 ok << ["mailto:joe@example.com?cc=bob@example.com&body=hello"]
65 ok[-1] << ["joe@example.com", ["cc=bob@example.com", "body=hello"]]
66 ok[-1] << {:to => "joe@example.com",
67 :headers => ["cc=bob@example.com", "body=hello"]}
69 # mailto:?to=joe@example.com&cc=bob@example.com&body=hello
70 ok << ["mailto:?to=joe@example.com&cc=bob@example.com&body=hello"]
72 ["to=joe@example.com", "cc=bob@example.com", "body=hello"]]
73 ok[-1] << {:headers => ["to=joe@example.com",
74 "cc=bob@example.com", "body=hello"]}
76 # mailto:gorby%25kremvax@example.com
77 ok << ["mailto:gorby%25kremvax@example.com"]
78 ok[-1] << ["gorby%25kremvax@example.com", nil]
79 ok[-1] << {:to => "gorby%25kremvax@example.com"}
81 # mailto:unlikely%3Faddress@example.com?blat=foop
82 ok << ["mailto:unlikely%3Faddress@example.com?blat=foop"]
83 ok[-1] << ["unlikely%3Faddress@example.com", ["blat=foop"]]
84 ok[-1] << {:to => "unlikely%3Faddress@example.com",
85 :headers => ["blat=foop"]}
87 # mailto:john@example.com?Subject=Ruby&Cc=jack@example.com
88 ok << ["mailto:john@example.com?Subject=Ruby&Cc=jack@example.com"]
89 ok[-1] << ['john@example.com', [['Subject', 'Ruby'], ['Cc', 'jack@example.com']]]
90 ok[-1] << {:to=>"john@example.com", :headers=>[["Subject", "Ruby"], ["Cc", "jack@example.com"]]}
92 # mailto:listman@example.com?subject=subscribe
93 ok << ["mailto:listman@example.com?subject=subscribe"]
94 ok[-1] << {:to => 'listman@example.com', :headers => [['subject', 'subscribe']]}
95 ok[-1] << {:to => 'listman@example.com', :headers => [['subject', 'subscribe']]}
97 # mailto:listman@example.com?subject=subscribe
98 ok << ["mailto:listman@example.com?subject=subscribe"]
99 ok[-1] << {:to => 'listman@example.com', :headers => { 'subject' => 'subscribe' }}
100 ok[-1] << {:to => 'listman@example.com', :headers => 'subject=subscribe' }
102 ok_all = ok.flatten.join("\0")
104 # mailto:joe@example.com?cc=bob@example.com?body=hello ; WRONG!
105 bad << ["joe@example.com", ["cc=bob@example.com?body=hello"]]
107 # mailto:javascript:alert()
108 bad << ["javascript:alert()", []]
110 # mailto:/example.com/ ; WRONG, not a mail address
111 bad << ["/example.com/", []]
113 # '=' which is in hname or hvalue is wrong.
114 bad << ["foo@example.jp?subject=1+1=2", []]
117 assert_equal(x[0], URI.parse(x[0]).to_s)
118 assert_equal(x[0], @u.build(x[1]).to_s)
119 assert_equal(x[0], @u.build(x[2]).to_s)
123 assert_raise(URI::InvalidURIError) {
126 assert_raise(URI::InvalidComponentError) {
131 assert_equal(ok_all, ok.flatten.join("\0"))
135 assert_raise(URI::InvalidComponentError) do
136 URI::MailTo.new('mailto', 'sdmitry:bla', 'localhost', '2000', nil,
137 'joe@example.com', nil, nil, 'subject=Ruby')
142 u = URI::MailTo.build(['joe@example.com', 'subject=Ruby'])
144 assert_raise(URI::InvalidComponentError) do
148 assert_raise(URI::InvalidComponentError) do
149 u.to = '@invalid.email'
154 u = URI::MailTo.build([nil, 'subject=Ruby'])
157 assert_equal('mailto:?subject=Ruby', u.to_s)
160 assert_equal('mailto:?subject=Ruby#test', u.to_s)
165 results << ["To: ruby-list@ruby-lang.org\nSubject: subscribe\n\n\n"]
166 results[-1] << { to: 'ruby-list@ruby-lang.org', headers: { 'subject' => 'subscribe' } }
168 results << ["To: ruby-list@ruby-lang.org\n\nBody\n"]
169 results[-1] << { to: 'ruby-list@ruby-lang.org', headers: { 'body' => 'Body' } }
171 results << ["To: ruby-list@ruby-lang.org, cc@ruby-lang.org\n\n\n"]
172 results[-1] << { to: 'ruby-list@ruby-lang.org', headers: { 'to' => 'cc@ruby-lang.org' } }
174 results.each do |expected, params|
175 u = URI::MailTo.build(params)
176 assert_equal(expected, u.to_mailtext)
179 u = URI.parse('mailto:ruby-list@ruby-lang.org?Subject=subscribe&cc=myaddr')
180 assert_equal "To: ruby-list@ruby-lang.org\nSubject: subscribe\nCc: myaddr\n\n\n",
185 u = URI.parse('mailto:joe@example.com?cc=bob@example.com&body=hello')
186 assert_equal(uri_to_ary(u), u.select(*u.component))
187 assert_raise(ArgumentError) do
188 u.select(:scheme, :host, :not_exist, :port)