1require 'test/unit'
2require 'uri/mailto'
3
4module URI
5
6
7class TestMailTo < Test::Unit::TestCase
8  def setup
9    @u = URI::MailTo
10  end
11
12  def teardown
13  end
14
15  def uri_to_ary(uri)
16    uri.class.component.collect {|c| uri.send(c)}
17  end
18
19  def test_build
20    ok = []
21    bad = []
22
23    # RFC2368, 6. Examples
24    # mailto:chris@example.com
25    ok << ["mailto:chris@example.com"]
26    ok[-1] << ["chris@example.com", nil]
27    ok[-1] << {:to => "chris@example.com"}
28
29    # mailto:infobot@example.com?subject=current-issue
30    ok << ["mailto:infobot@example.com?subject=current-issue"]
31    ok[-1] << ["infobot@example.com", ["subject=current-issue"]]
32    ok[-1] << {:to => "infobot@example.com",
33      :headers => ["subject=current-issue"]}
34
35    # mailto:infobot@example.com?body=send%20current-issue
36    ok << ["mailto:infobot@example.com?body=send%20current-issue"]
37    ok[-1] << ["infobot@example.com", ["body=send%20current-issue"]]
38    ok[-1] << {:to => "infobot@example.com",
39      :headers => ["body=send%20current-issue"]}
40
41    # mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index
42    ok << ["mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index"]
43    ok[-1] << ["infobot@example.com",
44      ["body=send%20current-issue%0D%0Asend%20index"]]
45    ok[-1] << {:to => "infobot@example.com",
46      :headers => ["body=send%20current-issue%0D%0Asend%20index"]}
47
48    # mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com
49    ok << ["mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com"]
50    ok[-1] << ["foobar@example.com",
51      ["In-Reply-To=%3c3469A91.D10AF4C@example.com"]]
52    ok[-1] << {:to => "foobar@example.com",
53      :headers => ["In-Reply-To=%3c3469A91.D10AF4C@example.com"]}
54
55    # mailto:majordomo@example.com?body=subscribe%20bamboo-l
56    ok << ["mailto:majordomo@example.com?body=subscribe%20bamboo-l"]
57    ok[-1] << ["majordomo@example.com", ["body=subscribe%20bamboo-l"]]
58    ok[-1] << {:to => "majordomo@example.com",
59      :headers => ["body=subscribe%20bamboo-l"]}
60
61    # mailto:joe@example.com?cc=bob@example.com&body=hello
62    ok << ["mailto:joe@example.com?cc=bob@example.com&body=hello"]
63    ok[-1] << ["joe@example.com", ["cc=bob@example.com", "body=hello"]]
64    ok[-1] << {:to => "joe@example.com",
65      :headers => ["cc=bob@example.com", "body=hello"]}
66
67    # mailto:?to=joe@example.com&cc=bob@example.com&body=hello
68    ok << ["mailto:?to=joe@example.com&cc=bob@example.com&body=hello"]
69    ok[-1] << [nil,
70      ["to=joe@example.com", "cc=bob@example.com", "body=hello"]]
71    ok[-1] << {:headers => ["to=joe@example.com",
72	"cc=bob@example.com", "body=hello"]}
73
74    # mailto:gorby%25kremvax@example.com
75    ok << ["mailto:gorby%25kremvax@example.com"]
76    ok[-1] << ["gorby%25kremvax@example.com", nil]
77    ok[-1] << {:to => "gorby%25kremvax@example.com"}
78
79    # mailto:unlikely%3Faddress@example.com?blat=foop
80    ok << ["mailto:unlikely%3Faddress@example.com?blat=foop"]
81    ok[-1] << ["unlikely%3Faddress@example.com", ["blat=foop"]]
82    ok[-1] << {:to => "unlikely%3Faddress@example.com",
83      :headers => ["blat=foop"]}
84
85    # mailto:john@example.com?Subject=Ruby&Cc=jack@example.com
86    ok << ["mailto:john@example.com?Subject=Ruby&Cc=jack@example.com"]
87    ok[-1] << ['john@example.com', [['Subject', 'Ruby'], ['Cc', 'jack@example.com']]]
88    ok[-1] << {:to=>"john@example.com", :headers=>[["Subject", "Ruby"], ["Cc", "jack@example.com"]]}
89
90    # mailto:listman@example.com?subject=subscribe
91    ok << ["mailto:listman@example.com?subject=subscribe"]
92    ok[-1] << {:to => 'listman@example.com', :headers => [['subject', 'subscribe']]}
93    ok[-1] << {:to => 'listman@example.com', :headers => [['subject', 'subscribe']]}
94
95    ok_all = ok.flatten.join("\0")
96
97    # mailto:joe@example.com?cc=bob@example.com?body=hello   ; WRONG!
98    bad << ["joe@example.com", ["cc=bob@example.com?body=hello"]]
99
100    # mailto:javascript:alert()
101    bad << ["javascript:alert()", []]
102
103    # '=' which is in hname or hvalue is wrong.
104    bad << ["foo@example.jp?subject=1+1=2", []]
105
106    ok.each do |x|
107      assert_equal(x[0],
108		   @u.build(x[1]).to_s)
109      assert_equal(x[0],
110		   @u.build(x[2]).to_s)
111    end
112
113    bad.each do |x|
114      assert_raise(URI::InvalidComponentError) {
115	@u.build(x)
116      }
117    end
118
119    assert_equal(ok_all, ok.flatten.join("\0"))
120  end
121
122  def test_select
123    u = URI.parse('mailto:joe@example.com?cc=bob@example.com&body=hello')
124    assert_equal(uri_to_ary(u), u.select(*u.component))
125    assert_raise(ArgumentError) do
126      u.select(:scheme, :host, :not_exist, :port)
127    end
128  end
129end
130
131
132end
133