* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / test / uri / test_mailto.rb
blobe7d314219824e1210ac410047039062c253451d2
1 # frozen_string_literal: false
2 require 'test/unit'
3 require 'uri/mailto'
5 class URI::TestMailTo < Test::Unit::TestCase
6   def setup
7     @u = URI::MailTo
8   end
10   def teardown
11   end
13   def uri_to_ary(uri)
14     uri.class.component.collect {|c| uri.send(c)}
15   end
17   def test_build
18     ok = []
19     bad = []
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"]
71     ok[-1] << [nil,
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", []]
116     ok.each do |x|
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)
120     end
122     bad.each do |x|
123       assert_raise(URI::InvalidURIError) {
124         URI.parse(x)
125       }
126       assert_raise(URI::InvalidComponentError) {
127         @u.build(x)
128       }
129     end
131     assert_equal(ok_all, ok.flatten.join("\0"))
132   end
134   def test_initializer
135     assert_raise(URI::InvalidComponentError) do
136       URI::MailTo.new('mailto', 'sdmitry:bla', 'localhost', '2000', nil,
137                       'joe@example.com', nil, nil, 'subject=Ruby')
138     end
139   end
141   def test_check_to
142     u = URI::MailTo.build(['joe@example.com', 'subject=Ruby'])
144     assert_raise(URI::InvalidComponentError) do
145       u.to = '#1@mail.com'
146     end
148     assert_raise(URI::InvalidComponentError) do
149       u.to = '@invalid.email'
150     end
151   end
153   def test_to_s
154     u = URI::MailTo.build([nil, 'subject=Ruby'])
156     u.send(:set_to, nil)
157     assert_equal('mailto:?subject=Ruby', u.to_s)
159     u.fragment = 'test'
160     assert_equal('mailto:?subject=Ruby#test', u.to_s)
161   end
163   def test_to_mailtext
164     results = []
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)
177     end
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",
181       u.to_mailtext
182   end
184   def test_select
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)
189     end
190   end