diff --git a/lib/uri/mailto.rb b/lib/uri/mailto.rb index cb8024f..a20e0ef 100644 --- a/lib/uri/mailto.rb +++ b/lib/uri/mailto.rb @@ -222,7 +222,8 @@ def set_headers(v) @headers = [] if v v.split('&').each do |x| - @headers << x.split(/=/, 2) + name, value = x.split(/=/, 2) + @headers << [name, value || ''] end end end @@ -266,18 +267,20 @@ def to_s # # => "To: ruby-list@ruby-lang.org\nSubject: subscribe\nCc: myaddr\n\n\n" # def to_mailtext - to = URI.decode_www_form_component(@to) + to = URI.decode_www_form_component(@to.to_s) head = '' body = '' @headers.each do |x| - case x[0] + name = x[0].to_s + value = x[1].to_s + case name when 'body' - body = URI.decode_www_form_component(x[1]) + body = URI.decode_www_form_component(value) when 'to' - to << ', ' + URI.decode_www_form_component(x[1]) + to << ', ' + URI.decode_www_form_component(value) else - head << URI.decode_www_form_component(x[0]).capitalize + ': ' + - URI.decode_www_form_component(x[1]) + "\n" + head << URI.decode_www_form_component(name).capitalize + ': ' + + URI.decode_www_form_component(value) + "\n" end end diff --git a/test/uri/test_mailto.rb b/test/uri/test_mailto.rb index 6cd3352..fd5c7e6 100644 --- a/test/uri/test_mailto.rb +++ b/test/uri/test_mailto.rb @@ -253,6 +253,16 @@ def test_to_mailtext u.to_mailtext end + def test_to_mailtext_with_header_name_only + u = URI.parse('mailto:ruby-list@ruby-lang.org?Subject') + assert_equal [['Subject', '']], u.headers + assert_equal "To: ruby-list@ruby-lang.org\nSubject: \n\n\n", u.to_mailtext + + u = URI.parse('mailto:ruby-list@ruby-lang.org?body&cc=myaddr') + assert_equal [['body', ''], ['cc', 'myaddr']], u.headers + assert_equal "To: ruby-list@ruby-lang.org\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))