PageRenderTime 69ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/gems/tmail-1.2.3.1/test/test_header.rb

https://github.com/maletor/PicPocket
Ruby | 963 lines | 822 code | 110 blank | 31 comment | 8 complexity | 275b40a779a638b89b0b8d8f1246188c MD5 | raw file
Possible License(s): MIT
  1. $:.unshift File.dirname(__FILE__)
  2. require 'test_helper'
  3. require 'tmail'
  4. require 'tmail/header'
  5. require 'kcode'
  6. require 'time'
  7. class UnstructuredHeaderTester < Test::Unit::TestCase
  8. def test_s_new
  9. %w( Subject SUBJECT sUbJeCt
  10. X-My-Header ).each do |name|
  11. h = TMail::HeaderField.new(name, 'This is test header.')
  12. assert_instance_of TMail::UnstructuredHeader, h,
  13. 'Header.new: name=' + name.dump
  14. end
  15. end
  16. def test_to_s
  17. # I must write more and more test.
  18. [
  19. 'This is test header.',
  20. # "This is \r\n\ttest header"
  21. # "JAPANESE STRING"
  22. ''
  23. ]\
  24. .each do |str|
  25. h = TMail::HeaderField.new('Subject', str)
  26. assert_equal str, h.decoded
  27. assert_equal str, h.to_s
  28. end
  29. end
  30. end
  31. class DateTimeHeaderTester < Test::Unit::TestCase
  32. def test_s_new
  33. %w( Date Resent-Date ).each do |name|
  34. h = TMail::HeaderField.new(name, 'Tue, 4 Dec 2001 10:49:32 +0900')
  35. assert_instance_of TMail::DateTimeHeader, h, name
  36. end
  37. end
  38. def test_date
  39. h = TMail::HeaderField.new('Date', 'Tue, 4 Dec 2001 10:49:32 +0900')
  40. assert_instance_of Time, h.date
  41. assert_equal false, h.date.gmt?
  42. assert_equal Time.parse('Tue, 4 Dec 2001 10:49:32 +0900'), h.date
  43. end
  44. def test_empty__illegal?
  45. [ [false, 'Tue, 4 Dec 2001 10:49:32 +0900'],
  46. [false, 'Sat, 15 Dec 2001 12:51:38 +0900'],
  47. [true, 'Sat, 15 Dec 2001 12:51:38'],
  48. [true, 'Sat, 15 Dec 2001 12:51'],
  49. [true, 'Sat,'] ].each do |wrong, str|
  50. h = TMail::HeaderField.new('Date', str)
  51. assert_equal wrong, h.empty?, str
  52. assert_equal wrong, h.illegal?, str
  53. end
  54. end
  55. def test_to_s
  56. h = TMail::HeaderField.new('Date', 'Tue, 4 Dec 2001 10:49:32 +0900')
  57. time = Time.parse('Tue, 4 Dec 2001 10:49:32 +0900').strftime("%a,%e %b %Y %H:%M:%S %z")
  58. assert_equal time, h.to_s
  59. assert_equal h.to_s, h.decoded
  60. ok = h.to_s
  61. h = TMail::HeaderField.new('Date', 'Tue, 4 Dec 2001 01:49:32 +0000')
  62. assert_equal ok, h.to_s
  63. h = TMail::HeaderField.new('Date', 'Tue, 4 Dec 2001 01:49:32 GMT')
  64. assert_equal ok, h.to_s
  65. end
  66. end
  67. class AddressHeaderTester < Test::Unit::TestCase
  68. def test_s_new
  69. %w( To Cc Bcc From Reply-To
  70. Resent-To Resent-Cc Resent-Bcc
  71. Resent-From Resent-Reply-To ).each do |name|
  72. h = TMail::HeaderField.new(name, 'aamine@loveruby.net')
  73. assert_instance_of TMail::AddressHeader, h, name
  74. end
  75. end
  76. def validate_case( str, isempty, to_s, comments, succ )
  77. h = TMail::HeaderField.new('To', str)
  78. assert_equal isempty, h.empty?, str.inspect + " (empty?)\n"
  79. assert_instance_of Array, h.addrs, str.inspect + " (is a)\n"
  80. assert_equal succ.size, h.addrs.size, str.inspect + " (size)\n"
  81. h.addrs.each do |a|
  82. ok = succ.shift
  83. assert_equal ok[:phrase], a.phrase, str.inspect + " (phrase)\n"
  84. assert_equal ok[:routes], a.routes, str.inspect + " (routes)\n"
  85. assert_equal ok[:spec], a.spec, str.inspect + " (spec)\n"
  86. end
  87. if comments.first.respond_to? :force_encoding
  88. encoding = h.comments.first.encoding
  89. comments.each { |c| c.force_encoding encoding }
  90. end
  91. assert_equal comments, h.comments, str.inspect + " (comments)\n"
  92. to_s.force_encoding(h.to_s.encoding) if to_s.respond_to? :force_encoding
  93. assert_equal to_s, h.to_s, str.inspect + " (to_s)\n" if to_s
  94. assert_equal to_s, h.decoded, str.inspect + " (decoded)\n" if to_s
  95. end
  96. def test_ATTRS
  97. validate_case 'aamine@loveruby.net',
  98. false,
  99. 'aamine@loveruby.net',
  100. [],
  101. [{ :phrase => nil,
  102. :routes => [],
  103. :spec => 'aamine@loveruby.net' }]
  104. validate_case 'Minero Aoki <aamine@loveruby.net> (comment)',
  105. false,
  106. 'Minero Aoki <aamine@loveruby.net> (comment)',
  107. ['comment'],
  108. [{ :phrase => 'Minero Aoki',
  109. :routes => [],
  110. :spec => 'aamine@loveruby.net' }]
  111. validate_case 'aamine@loveruby.net, , taro@softica.org',
  112. false,
  113. 'aamine@loveruby.net, taro@softica.org',
  114. [],
  115. [{ :phrase => nil,
  116. :routes => [],
  117. :spec => 'aamine@loveruby.net' },
  118. { :phrase => nil,
  119. :routes => [],
  120. :spec => 'taro@softica.org' }]
  121. validate_case '',
  122. true,
  123. nil,
  124. [],
  125. []
  126. validate_case '(comment only)',
  127. true,
  128. nil,
  129. ['comment only'],
  130. []
  131. kcode('EUC') {
  132. validate_case 'hoge@example.jp (=?ISO-2022-JP?B?GyRCJUYlOSVIGyhC?=)',
  133. false,
  134. "hoge@example.jp (\245\306\245\271\245\310)",
  135. ["\245\306\245\271\245\310"],
  136. [{ :phrase => nil,
  137. :routes => [],
  138. :spec => 'hoge@example.jp'}]
  139. }
  140. end
  141. end
  142. class SingleAddressHeaderTester < Test::Unit::TestCase
  143. def test_s_new
  144. h = TMail::HeaderField.new('Sender', 'aamine@loveruby.net')
  145. assert_instance_of TMail::SingleAddressHeader, h
  146. end
  147. def test_addr
  148. h = TMail::HeaderField.new('Sender', 'aamine@loveruby.net')
  149. assert_not_nil h.addr
  150. assert_instance_of TMail::Address, h.addr
  151. assert_equal 'aamine@loveruby.net', h.addr.spec
  152. assert_equal nil, h.addr.phrase
  153. assert_equal [], h.addr.routes
  154. end
  155. def test_to_s
  156. str = 'Minero Aoki <aamine@loveruby.net>, "AOKI, Minero" <aamine@softica.org>'
  157. h = TMail::HeaderField.new('Sender', str)
  158. assert_equal 'Minero Aoki <aamine@loveruby.net>', h.to_s
  159. end
  160. end
  161. class ReturnPathHeaderTester < Test::Unit::TestCase
  162. def test_s_new
  163. %w( Return-Path ).each do |name|
  164. h = TMail::HeaderField.new(name, '<aamine@loveruby.net>')
  165. assert_instance_of TMail::ReturnPathHeader, h, name
  166. assert_equal false, h.empty?
  167. assert_equal false, h.illegal?
  168. end
  169. end
  170. def test_ATTRS
  171. h = TMail::HeaderField.new('Return-Path', '<@a,@b,@c:aamine@loveruby.net>')
  172. assert_not_nil h.addr
  173. assert_instance_of TMail::Address, h.addr
  174. assert_equal 'aamine@loveruby.net', h.addr.spec
  175. assert_equal nil, h.addr.phrase
  176. assert_equal ['a', 'b', 'c'], h.addr.routes
  177. assert_not_nil h.routes
  178. assert_instance_of Array, h.routes
  179. assert_equal ['a', 'b', 'c'], h.routes
  180. assert_equal h.addr.routes, h.routes
  181. assert_not_nil h.spec
  182. assert_instance_of String, h.spec
  183. assert_equal 'aamine@loveruby.net', h.spec
  184. # missing '<' '>'
  185. h = TMail::HeaderField.new('Return-Path', 'xxxx@yyyy')
  186. assert_equal 'xxxx@yyyy', h.spec
  187. h = TMail::HeaderField.new('Return-Path', '<>')
  188. assert_instance_of TMail::Address, h.addr
  189. assert_nil h.addr.local
  190. assert_nil h.addr.domain
  191. assert_nil h.addr.spec
  192. assert_nil h.spec
  193. end
  194. def test_to_s
  195. body = 'Minero Aoki <@a,@b,@c:aamine@loveruby.net>'
  196. h = TMail::HeaderField.new('Return-Path', body)
  197. assert_equal '<@a,@b,@c:aamine@loveruby.net>', h.to_s
  198. assert_equal h.to_s, h.decoded
  199. body = 'aamine@loveruby.net'
  200. h = TMail::HeaderField.new('Return-Path', body)
  201. assert_equal '<aamine@loveruby.net>', h.to_s
  202. assert_equal h.to_s, h.decoded
  203. body = '<>'
  204. h = TMail::HeaderField.new('Return-Path', body)
  205. assert_equal '<>', h.to_s
  206. end
  207. end
  208. class MessageIdHeaderTester < Test::Unit::TestCase
  209. def test_s_new
  210. %w( Message-Id MESSAGE-ID Message-ID
  211. Resent-Message-Id Content-Id ).each do |name|
  212. h = TMail::HeaderField.new(name, '<20020103xg88.k0@mail.loveruby.net>')
  213. assert_instance_of TMail::MessageIdHeader, h
  214. end
  215. end
  216. def test_id
  217. str = '<20020103xg88.k0@mail.loveruby.net>'
  218. h = TMail::HeaderField.new('Message-Id', str)
  219. assert_not_nil h.id
  220. assert_equal str, h.id
  221. id = '<20020103xg88.k0@mail.loveruby.net>'
  222. str = id + ' (comm(ent))'
  223. h = TMail::HeaderField.new('Message-Id', str)
  224. assert_not_nil h.id
  225. assert_equal id, h.id
  226. end
  227. def test_id=
  228. h = TMail::HeaderField.new('Message-Id', '')
  229. h.id = str = '<20020103xg88.k0@mail.loveruby.net>'
  230. assert_not_nil h.id
  231. assert_equal str, h.id
  232. end
  233. end
  234. class ReferencesHeaderTester < Test::Unit::TestCase
  235. def test_s_new
  236. str = '<20020103xg88.k0@mail.loveruby.net>'
  237. %w( References REFERENCES ReFeReNcEs
  238. In-Reply-To ).each do |name|
  239. h = TMail::HeaderField.new(name, str)
  240. assert_instance_of TMail::ReferencesHeader, h, name
  241. end
  242. end
  243. def test_ATTRS
  244. id1 = '<20020103xg88.k0@mail.loveruby.net>'
  245. id2 = '<20011204103415.64DB.GGB03124@nifty.ne.jp>'
  246. phr = 'message of "Wed, 17 Mar 1999 18:42:07 +0900"'
  247. str = id1 + ' ' + phr + ' ' + id2
  248. h = TMail::HeaderField.new('References', str)
  249. ok = [id1, id2]
  250. h.each_id do |i|
  251. assert_equal ok.shift, i
  252. end
  253. ok = [id1, id2]
  254. assert_equal ok, h.ids
  255. h.each_id do |i|
  256. assert_equal ok.shift, i
  257. end
  258. ok = [phr]
  259. assert_equal ok, h.phrases
  260. h.each_phrase do |i|
  261. assert_equal ok.shift, i
  262. end
  263. ok = [phr]
  264. h.each_phrase do |i|
  265. assert_equal ok.shift, i
  266. end
  267. # test 2
  268. # 'In-Reply-To'
  269. # 'aamine@dp.u-netsurf.ne.jp's message of "Fri, 8 Jan 1999 03:49:37 +0900"'
  270. end
  271. def test_to_s
  272. id1 = '<20020103xg88.k0@mail.loveruby.net>'
  273. id2 = '<20011204103415.64DB.GGB03124@nifty.ne.jp>'
  274. phr = 'message of "Wed, 17 Mar 1999 18:42:07 +0900"'
  275. str = id1 + ' ' + phr + ' ' + id2
  276. h = TMail::HeaderField.new('References', str)
  277. assert_equal id1 + ' ' + id2, h.to_s
  278. end
  279. end
  280. class ReceivedHeaderTester < Test::Unit::TestCase
  281. HEADER1 = <<EOS
  282. from helium.ruby-lang.org (helium.ruby-lang.org [210.251.121.214])
  283. by doraemon.edit.ne.jp (8.12.1/8.12.0) via TCP with ESMTP
  284. id fB41nwEj007438 for <aamine@mx.edit.ne.jp>;
  285. Tue, 4 Dec 2001 10:49:58 +0900 (JST)
  286. EOS
  287. HEADER2 = <<EOS
  288. from helium.ruby-lang.org (localhost [127.0.0.1])
  289. by helium.ruby-lang.org (Postfix) with ESMTP
  290. id 8F8951AF3F; Tue, 4 Dec 2001 10:49:32 +0900 (JST)
  291. EOS
  292. HEADER3 = <<EOS
  293. from smtp1.dti.ne.jp (smtp1.dti.ne.jp [202.216.228.36])
  294. by helium.ruby-lang.org (Postfix) with ESMTP id CE3A1C3
  295. for <ruby-list@ruby-lang.org>; Tue, 4 Dec 2001 10:49:31 +0900 (JST)
  296. EOS
  297. =begin dangerous headers
  298. # 2-word WITH (this header is also wrong in semantic)
  299. # I cannot support this.
  300. Received: by mebius with Microsoft Mail
  301. id <01BE2B9D.9051EAA0@mebius>; Sat, 19 Dec 1998 22:18:54 -0800
  302. =end
  303. def test_s_new
  304. %w( Received ).each do |name|
  305. h = TMail::HeaderField.new(name, HEADER1)
  306. assert_instance_of TMail::ReceivedHeader, h, name
  307. end
  308. end
  309. def test_ATTRS
  310. h = TMail::HeaderField.new('Received', HEADER1)
  311. assert_instance_of String, h.from
  312. assert_equal 'helium.ruby-lang.org', h.from
  313. assert_instance_of String, h.by
  314. assert_equal 'doraemon.edit.ne.jp', h.by
  315. assert_instance_of String, h.via
  316. assert_equal 'TCP', h.via
  317. assert_instance_of Array, h.with
  318. assert_equal %w(ESMTP), h.with
  319. assert_instance_of String, h.id
  320. assert_equal 'fB41nwEj007438', h.id
  321. assert_instance_of String, h._for
  322. assert_equal 'aamine@mx.edit.ne.jp', h._for # must be <a> ?
  323. assert_instance_of Time, h.date
  324. time = Time.parse('Tue, 4 Dec 2001 10:49:58 +0900')
  325. assert_equal time, h.date
  326. h = TMail::HeaderField.new('Received', '; Tue, 4 Dec 2001 10:49:58 +0900')
  327. assert_nil h.from
  328. assert_nil h.by
  329. assert_nil h.via
  330. assert_equal [], h.with
  331. assert_nil h.id
  332. assert_nil h._for
  333. time = Time.parse('Tue, 4 Dec 2001 10:49:58 +0900')
  334. assert_equal time, h.date
  335. # without date
  336. h = TMail::HeaderField.new('Received', 'by NeXT.Mailer (1.144.2)')
  337. assert_nil h.from
  338. assert_equal 'NeXT.Mailer', h.by
  339. assert_nil h.via
  340. assert_equal [], h.with
  341. assert_nil h.id
  342. assert_nil h._for
  343. assert_nil h.date
  344. # FROM is not a domain
  345. h = TMail::HeaderField.new('Received',
  346. 'from someuser@example.com; Tue, 24 Nov 1998 07:59:39 -0500')
  347. assert_equal 'example.com', h.from
  348. assert_nil h.by
  349. assert_nil h.via
  350. assert_equal [], h.with
  351. assert_nil h.id
  352. assert_nil h._for
  353. time = Time.parse('Tue, 24 Nov 1998 07:59:39 -0500')
  354. assert_equal time, h.date
  355. =begin
  356. # FOR is not route-addr.
  357. # item order is wrong.
  358. h = TMail::HeaderField.new('Received',
  359. 'from aamine by mail.softica.org with local for list@softica.org id 12Vm3N-00044L-01; Fri, 17 Mar 2000 10:59:53 +0900')
  360. assert_equal 'aamine', h.from
  361. assert_equal 'mail.softica.org', h.by
  362. assert_nil h.via
  363. assert_equal ['local'], h.with
  364. assert_equal '12Vm3N-00044L-01', h.id
  365. assert_equal 'list@softica.org', h._for
  366. assert_equal Time.local(2000,4,17, 10,59,53), h.date
  367. =end
  368. # word + domain-literal in FROM
  369. h = TMail::HeaderField.new('Received',
  370. 'from localhost [192.168.1.1]; Sat, 19 Dec 1998 22:19:50 PST')
  371. assert_equal 'localhost', h.from
  372. assert_nil h.by
  373. assert_nil h.via
  374. assert_equal [], h.with
  375. assert_nil h.id
  376. assert_nil h._for
  377. time = Time.parse('Sat, 19 Dec 1998 22:19:50 PST')
  378. assert_equal time, h.date
  379. # addr-spec in BY (must be a domain)
  380. h = TMail::HeaderField.new('Received',
  381. 'by aamine@loveruby.net; Wed, 24 Feb 1999 14:34:20 +0900')
  382. assert_equal 'loveruby.net', h.by
  383. end
  384. def test_to_s
  385. h = TMail::HeaderField.new('Received', HEADER1)
  386. time = Time.parse('Tue, 4 Dec 2001 10:49:58 +0900').strftime("%a,%e %b %Y %H:%M:%S %z")
  387. assert_equal "from helium.ruby-lang.org by doraemon.edit.ne.jp via TCP with ESMTP id fB41nwEj007438 for <aamine@mx.edit.ne.jp>; #{time}", h.to_s
  388. [
  389. 'from harmony.loveruby.net',
  390. 'by mail.loveruby.net',
  391. 'via TCP',
  392. 'with ESMTP',
  393. 'id LKJHSDFG',
  394. 'for <aamine@loveruby.net>',
  395. "; #{time}"
  396. ]\
  397. .each do |str|
  398. h = TMail::HeaderField.new('Received', str)
  399. assert_equal str, h.to_s, 'ReceivedHeader#to_s: data=' + str.dump
  400. end
  401. end
  402. end
  403. class KeywordsHeaderTester < Test::Unit::TestCase
  404. def test_s_new
  405. %w( Keywords KEYWORDS KeYwOrDs ).each do |name|
  406. h = TMail::HeaderField.new(name, 'key, word, is, keyword')
  407. assert_instance_of TMail::KeywordsHeader, h
  408. end
  409. end
  410. def test_keys
  411. h = TMail::HeaderField.new('Keywords', 'key, word, is, keyword')
  412. assert_instance_of Array, h.keys
  413. assert_equal %w(key word is keyword), h.keys
  414. end
  415. end
  416. class EncryptedHeaderTester < Test::Unit::TestCase
  417. def test_s_new
  418. %w( Encrypted ).each do |name|
  419. h = TMail::HeaderField.new(name, 'lot17 solt')
  420. assert_instance_of TMail::EncryptedHeader, h
  421. end
  422. end
  423. def test_encrypter
  424. h = TMail::HeaderField.new('Encrypted', 'lot17 solt')
  425. assert_equal 'lot17', h.encrypter
  426. end
  427. def test_encrypter=
  428. h = TMail::HeaderField.new('Encrypted', 'lot17 solt')
  429. h.encrypter = 'newscheme'
  430. assert_equal 'newscheme', h.encrypter
  431. end
  432. def test_keyword
  433. h = TMail::HeaderField.new('Encrypted', 'lot17 solt')
  434. assert_equal 'solt', h.keyword
  435. h = TMail::HeaderField.new('Encrypted', 'lot17')
  436. assert_equal nil, h.keyword
  437. end
  438. def test_keyword=
  439. h = TMail::HeaderField.new('Encrypted', 'lot17 solt')
  440. h.keyword = 'newscheme'
  441. assert_equal 'newscheme', h.keyword
  442. end
  443. end
  444. class MimeVersionHeaderTester < Test::Unit::TestCase
  445. def test_s_new
  446. %w( Mime-Version MIME-VERSION MiMe-VeRsIoN ).each do |name|
  447. h = TMail::HeaderField.new(name, '1.0')
  448. assert_instance_of TMail::MimeVersionHeader, h
  449. end
  450. end
  451. def test_ATTRS
  452. h = TMail::HeaderField.new('Mime-Version', '1.0')
  453. assert_equal 1, h.major
  454. assert_equal 0, h.minor
  455. assert_equal '1.0', h.version
  456. h = TMail::HeaderField.new('Mime-Version', '99.77 (is ok)')
  457. assert_equal 99, h.major
  458. assert_equal 77, h.minor
  459. assert_equal '99.77', h.version
  460. end
  461. def test_major=
  462. h = TMail::HeaderField.new('Mime-Version', '1.1')
  463. h.major = 2
  464. assert_equal 2, h.major
  465. assert_equal 1, h.minor
  466. assert_equal 2, h.major
  467. h.major = 3
  468. assert_equal 3, h.major
  469. end
  470. def test_minor=
  471. h = TMail::HeaderField.new('Mime-Version', '2.3')
  472. assert_equal 3, h.minor
  473. h.minor = 5
  474. assert_equal 5, h.minor
  475. assert_equal 2, h.major
  476. end
  477. def test_to_s
  478. h = TMail::HeaderField.new('Mime-Version', '1.0 (first version)')
  479. assert_equal '1.0', h.to_s
  480. end
  481. def test_empty?
  482. h = TMail::HeaderField.new('Mime-Version', '')
  483. assert_equal true, h.empty?
  484. end
  485. end
  486. class ContentTypeHeaderTester < Test::Unit::TestCase
  487. def test_s_new
  488. %w( Content-Type CONTENT-TYPE CoNtEnT-TyPe ).each do |name|
  489. h = TMail::HeaderField.new(name, 'text/plain; charset=iso-2022-jp')
  490. assert_instance_of TMail::ContentTypeHeader, h, name
  491. end
  492. end
  493. def test_ATTRS
  494. h = TMail::HeaderField.new('Content-Type', 'text/plain; charset=iso-2022-jp')
  495. assert_equal 'text', h.main_type
  496. assert_equal 'plain', h.sub_type
  497. assert_equal 1, h.params.size
  498. assert_equal 'iso-2022-jp', h.params['charset']
  499. h = TMail::HeaderField.new('Content-Type', 'Text/Plain; Charset=shift_jis')
  500. assert_equal 'text', h.main_type
  501. assert_equal 'plain', h.sub_type
  502. assert_equal 1, h.params.size
  503. assert_equal 'shift_jis', h.params['charset']
  504. end
  505. def test_multipart_with_legal_unquoted_boundary
  506. h = TMail::HeaderField.new('Content-Type', 'multipart/mixed; boundary=dDRMvlgZJXvWKvBx')
  507. assert_equal 'multipart', h.main_type
  508. assert_equal 'mixed', h.sub_type
  509. assert_equal 1, h.params.size
  510. assert_equal 'dDRMvlgZJXvWKvBx', h.params['boundary']
  511. end
  512. def test_multipart_with_legal_quoted_boundary_should_retain_quotations
  513. h = TMail::HeaderField.new('Content-Type', 'multipart/mixed; boundary="dDRMvlgZJXvWKvBx"')
  514. assert_equal 'multipart', h.main_type
  515. assert_equal 'mixed', h.sub_type
  516. assert_equal 1, h.params.size
  517. assert_equal 'dDRMvlgZJXvWKvBx', h.params['boundary']
  518. end
  519. def test_multipart_with_illegal_unquoted_boundary_should_add_quotations
  520. h = TMail::HeaderField.new('Content-Type', 'multipart/alternative; boundary=----=_=NextPart_000_0093_01C81419.EB75E850')
  521. assert_equal 'multipart', h.main_type
  522. assert_equal 'alternative', h.sub_type
  523. assert_equal 1, h.params.size
  524. assert_equal '----=_=NextPart_000_0093_01C81419.EB75E850', h.params['boundary']
  525. end
  526. def test_multipart_with_illegal_quoted_boundary_should_retain_quotations
  527. h = TMail::HeaderField.new('Content-Type', 'multipart/alternative; boundary="----=_=NextPart_000_0093_01C81419.EB75E850"')
  528. assert_equal 'multipart', h.main_type
  529. assert_equal 'alternative', h.sub_type
  530. assert_equal 1, h.params.size
  531. assert_equal '----=_=NextPart_000_0093_01C81419.EB75E850', h.params['boundary']
  532. end
  533. def test_multipart_with_extra_with_multiple_params
  534. h = TMail::HeaderField.new('Content-Type', 'multipart/related;boundary=1_4626B816_9F1690;Type="application/smil";Start="<mms.smil.txt>"')
  535. assert_equal 'multipart', h.main_type
  536. assert_equal 'related', h.sub_type
  537. assert_equal 3, h.params.size
  538. assert_equal '1_4626B816_9F1690', h.params['boundary']
  539. end
  540. def test_main_type=
  541. h = TMail::HeaderField.new('Content-Type', 'text/plain; charset=iso-2022-jp')
  542. assert_equal 'text', h.main_type
  543. h.main_type = 'multipart'
  544. assert_equal 'multipart', h.main_type
  545. assert_equal 'multipart', h.main_type
  546. h.main_type = 'TEXT'
  547. assert_equal 'text', h.main_type
  548. end
  549. def test_sub_type=
  550. h = TMail::HeaderField.new('Content-Type', 'text/plain; charset=iso-2022-jp')
  551. assert_equal 'plain', h.sub_type
  552. h.sub_type = 'html'
  553. assert_equal 'html', h.sub_type
  554. h.sub_type = 'PLAIN'
  555. assert_equal 'plain', h.sub_type
  556. end
  557. end
  558. class ContentEncodingHeaderTester < Test::Unit::TestCase
  559. def test_s_new
  560. %w( Content-Transfer-Encoding CONTENT-TRANSFER-ENCODING
  561. COnteNT-TraNSFer-ENCodiNG ).each do |name|
  562. h = TMail::HeaderField.new(name, 'Base64')
  563. assert_instance_of TMail::ContentTransferEncodingHeader, h
  564. end
  565. end
  566. def test_encoding
  567. h = TMail::HeaderField.new('Content-Transfer-Encoding', 'Base64')
  568. assert_equal 'base64', h.encoding
  569. h = TMail::HeaderField.new('Content-Transfer-Encoding', '7bit')
  570. assert_equal '7bit', h.encoding
  571. end
  572. def test_encoding=
  573. h = TMail::HeaderField.new('Content-Transfer-Encoding', 'Base64')
  574. assert_equal 'base64', h.encoding
  575. h.encoding = '7bit'
  576. assert_equal '7bit', h.encoding
  577. end
  578. def test_to_s
  579. h = TMail::HeaderField.new('Content-Transfer-Encoding', 'Base64')
  580. assert_equal 'Base64', h.to_s
  581. assert_equal h.to_s, h.decoded
  582. assert_equal h.to_s, h.encoded
  583. end
  584. def test_insertion_of_headers_and_encoding_them_short
  585. mail = TMail::Mail.new
  586. mail['X-Mail-Header'] = "short bit of data"
  587. assert_equal("X-Mail-Header: short bit of data\r\n\r\n", mail.encoded)
  588. end
  589. def test_insertion_of_headers_and_encoding_them_more_than_78_char_total_with_whitespace
  590. mail = TMail::Mail.new
  591. mail['X-Ruby-Talk'] = "<11152772-AAFA-4614-95FD-9071A4BDF4A111152772-AAFA 4614-95FD-9071A4BDF4A1@grayproductions.net>"
  592. assert_equal("X-Ruby-Talk: <11152772-AAFA-4614-95FD-9071A4BDF4A111152772-AAFA\r\n\t4614-95FD-9071A4BDF4A1@grayproductions.net>\r\n\r\n", mail.encoded)
  593. result = TMail::Mail.parse(mail.encoded)
  594. assert_equal(mail['X-Ruby-Talk'].to_s, result['X-Ruby-Talk'].to_s)
  595. end
  596. def test_insertion_of_headers_and_encoding_them_more_than_78_char_total_with_whitespace
  597. mail = TMail::Mail.new
  598. mail['X-Ruby-Talk'] = "<11152772-AAFA-4614-95FD-9071A4BDF4A111152772-AAFA 4614-95FD-9071A4BDF4A1@grayproductions.net11152772-AAFA-4614-95FD-9071A4BDF4A111152772-AAFA 4614-95FD-9071A4BDF4A1@grayproductions.net>"
  599. assert_equal("X-Ruby-Talk: <11152772-AAFA-4614-95FD-9071A4BDF4A111152772-AAFA\r\n\t4614-95FD-9071A4BDF4A1@grayproductions.net11152772-AAFA-4614-95FD-9071A4BDF4A111152772-AAFA\r\n\t4614-95FD-9071A4BDF4A1@grayproductions.net>\r\n\r\n", mail.encoded)
  600. result = TMail::Mail.parse(mail.encoded)
  601. assert_equal(mail['X-Ruby-Talk'].to_s, result['X-Ruby-Talk'].to_s)
  602. end
  603. def test_insertion_of_headers_and_encoding_them_more_than_78_char_total_without_whitespace
  604. mail = TMail::Mail.new
  605. mail['X-Ruby-Talk'] = "<11152772-AAFA-4614-95FD-9071A4BDF4A111152772-AAFA-4614-95FD-9071A4BDF4A1@grayproductions.net>"
  606. assert_equal("X-Ruby-Talk: <11152772-AAFA-4614-95FD-9071A4BDF4A111152772-AAFA-4614-95FD-9071A4BDF4A1@grayproductions.net>\r\n\r\n", mail.encoded)
  607. result = TMail::Mail.parse(mail.encoded)
  608. assert_equal(mail['X-Ruby-Talk'].to_s, result['X-Ruby-Talk'].to_s)
  609. end
  610. def test_insertion_of_headers_and_encoding_them_less_than_998_char_total_without_whitespace
  611. mail = TMail::Mail.new
  612. text_with_whitespace = ""; 985.times{text_with_whitespace << "a"}
  613. mail['Reply-To'] = "#{text_with_whitespace}"
  614. assert_equal("Reply-To: #{text_with_whitespace}\r\n\r\n", mail.encoded)
  615. result = TMail::Mail.parse(mail.encoded)
  616. assert_equal(mail['Reply-To'].to_s, result['Reply-To'].to_s)
  617. end
  618. def test_insertion_of_headers_and_encoding_them_more_than_998_char_total_without_whitespace
  619. mail = TMail::Mail.new
  620. text_with_whitespace = ""; 1200.times{text_with_whitespace << "a"}
  621. before_text = ""; 985.times{before_text << "a"}
  622. after_text = ""; 215.times{after_text << "a"}
  623. mail['X-Ruby-Talk'] = "#{text_with_whitespace}"
  624. assert_equal("X-Ruby-Talk: #{before_text}\r\n\t#{after_text}\r\n\r\n", mail.encoded)
  625. end
  626. def test_insertion_of_headers_and_encoding_with_1_more_than_998_char_total_without_whitespace
  627. mail = TMail::Mail.new
  628. text_with_whitespace = ""; 996.times{text_with_whitespace << "a"}
  629. before_text = ""; 995.times{before_text << "a"}
  630. after_text = ""; 1.times{after_text << "a"}
  631. mail['X'] = "#{text_with_whitespace}"
  632. assert_equal("X: #{before_text}\r\n\t#{after_text}\r\n\r\n", mail.encoded)
  633. end
  634. def test_insertion_of_headers_and_encoding_with_exactly_998_char_total_without_whitespace
  635. mail = TMail::Mail.new
  636. text_with_whitespace = ""; 995.times{text_with_whitespace << "a"}
  637. before_text = ""; 995.times{before_text << "a"}
  638. mail['X'] = "#{text_with_whitespace}"
  639. assert_equal("X: #{before_text}\r\n\r\n", mail.encoded)
  640. end
  641. end
  642. class ContentDispositionHeaderTester < Test::Unit::TestCase
  643. def test_s_new
  644. %w( Content-Disposition CONTENT-DISPOSITION
  645. ConTENt-DIsPOsition ).each do |name|
  646. h = TMail::HeaderField.new(name, 'attachment; filename="README.txt.pif"')
  647. assert_instance_of TMail::ContentDispositionHeader, h
  648. end
  649. end
  650. def test_ATTRS
  651. begin
  652. _test_ATTRS
  653. _test_tspecials
  654. _test_rfc2231_decode
  655. #_test_rfc2231_encode
  656. _test_raw_iso2022jp
  657. _test_raw_eucjp
  658. _test_raw_sjis
  659. _test_code_conversion
  660. ensure
  661. TMail.KCODE = 'NONE'
  662. end
  663. end
  664. def _test_ATTRS
  665. TMail.KCODE = 'NONE'
  666. h = TMail::HeaderField.new('Content-Disposition',
  667. 'attachment; filename="README.txt.pif"')
  668. assert_equal 'attachment', h.disposition
  669. assert_equal 1, h.params.size
  670. assert_equal 'README.txt.pif', h.params['filename']
  671. h = TMail::HeaderField.new('Content-Disposition',
  672. 'attachment; Filename="README.txt.pif"')
  673. assert_equal 'attachment', h.disposition
  674. assert_equal 1, h.params.size
  675. assert_equal 'README.txt.pif', h.params['filename']
  676. h = TMail::HeaderField.new('Content-Disposition',
  677. 'attachment; filename=')
  678. assert_equal true, h.empty?
  679. assert_nil h.params
  680. assert_nil h['filename']
  681. end
  682. def _test_tspecials
  683. h = TMail::HeaderField.new('Content-Disposition', 'a; n=a')
  684. h['n'] = %q|()<>[];:@\\,"/?=|
  685. assert_equal 'a; n="()<>[];:@\\\\,\"/?="', h.encoded
  686. end
  687. def _test_rfc2231_decode
  688. TMail.KCODE = 'EUC'
  689. h = TMail::HeaderField.new('Content-Disposition',
  690. "attachment; filename*=iso-2022-jp'ja'%1b$B$Q$i$`%1b%28B")
  691. assert_equal 'attachment', h.disposition
  692. assert_equal 1, h.params.size
  693. expected = "\244\321\244\351\244\340"
  694. expected.force_encoding 'EUC-JP' if expected.respond_to? :force_encoding
  695. assert_equal expected, h.params['filename']
  696. end
  697. def _test_rfc2231_encode
  698. TMail.KCODE = 'EUC'
  699. h = TMail::HeaderField.new('Content-Disposition', 'a; n=a')
  700. h['n'] = "\245\265\245\363\245\327\245\353.txt"
  701. assert_equal "a; n*=iso-2022-jp'ja'%1B$B%255%25s%25W%25k%1B%28B.txt",
  702. h.encoded
  703. h = TMail::HeaderField.new('Content-Disposition', 'a; n=a')
  704. h['n'] = "\245\265()<>[];:@\\,\"/?=%*'"
  705. assert_equal "a;\r\n\tn*=iso-2022-jp'ja'%1B$B%255%1B%28B%28%29%3C%3E%5B%5D%3B%3A%40%5C%2C%22%2F%3F%3D%25%2A%27",
  706. h.encoded
  707. end
  708. def _test_raw_iso2022jp
  709. TMail.KCODE = 'EUC'
  710. # raw iso2022jp string in value (token)
  711. h = TMail::HeaderField.new('Content-Disposition',
  712. %Q<attachment; filename=\e$BF|K\\8l\e(B.doc>)
  713. assert_equal 'attachment', h.disposition
  714. assert_equal 1, h.params.size
  715. # assert_equal "\e$BF|K\\8l\e(B.doc", h.params['filename']
  716. expected = "\306\374\313\334\270\354.doc"
  717. expected.force_encoding 'EUC-JP' if expected.respond_to? :force_encoding
  718. assert_equal expected, h.params['filename']
  719. # raw iso2022jp string in value (quoted string)
  720. h = TMail::HeaderField.new('Content-Disposition',
  721. %Q<attachment; filename="\e$BF|K\\8l\e(B.doc">)
  722. assert_equal 'attachment', h.disposition
  723. assert_equal 1, h.params.size
  724. # assert_equal "\e$BF|K\\8l\e(B.doc", h.params['filename']
  725. assert_equal expected, h.params['filename']
  726. end
  727. def _test_raw_eucjp
  728. TMail.KCODE = 'EUC'
  729. # raw EUC-JP string in value (token)
  730. h = TMail::HeaderField.new('Content-Disposition',
  731. %Q<attachment; filename=\306\374\313\334\270\354.doc>)
  732. assert_equal 'attachment', h.disposition
  733. assert_equal 1, h.params.size
  734. expected = "\306\374\313\334\270\354.doc"
  735. expected.force_encoding 'EUC-JP' if expected.respond_to? :force_encoding
  736. assert_equal expected, h.params['filename']
  737. # raw EUC-JP string in value (quoted-string)
  738. h = TMail::HeaderField.new('Content-Disposition',
  739. %Q<attachment; filename="\306\374\313\334\270\354.doc">)
  740. assert_equal 'attachment', h.disposition
  741. assert_equal 1, h.params.size
  742. assert_equal expected, h.params['filename']
  743. end
  744. def _test_raw_sjis
  745. TMail.KCODE = 'SJIS'
  746. # raw SJIS string in value (token)
  747. h = TMail::HeaderField.new('Content-Disposition',
  748. %Q<attachment; filename=\223\372\226{\214\352.doc>)
  749. assert_equal 'attachment', h.disposition
  750. assert_equal 1, h.params.size
  751. expected = "\223\372\226{\214\352.doc"
  752. expected.force_encoding 'Windows-31J' if expected.respond_to? :force_encoding
  753. assert_equal expected, h.params['filename']
  754. # raw SJIS string in value (quoted-string)
  755. h = TMail::HeaderField.new('Content-Disposition',
  756. %Q<attachment; filename="\223\372\226{\214\352.doc">)
  757. assert_equal 'attachment', h.disposition
  758. assert_equal 1, h.params.size
  759. assert_equal expected, h.params['filename']
  760. end
  761. def _test_code_conversion
  762. # JIS -> TMail.KCODE auto conversion
  763. TMail.KCODE = 'EUC'
  764. h = TMail::HeaderField.new('Content-Disposition',
  765. %Q<attachment; filename=\e$BF|K\\8l\e(B.doc>)
  766. assert_equal 'attachment', h.disposition
  767. assert_equal 1, h.params.size
  768. expected = "\306\374\313\334\270\354.doc"
  769. expected.force_encoding 'EUC-JP' if expected.respond_to? :force_encoding
  770. assert_equal expected, h.params['filename']
  771. TMail.KCODE = 'SJIS'
  772. h = TMail::HeaderField.new('Content-Disposition',
  773. %Q<attachment; filename=\e$BF|K\\8l\e(B.doc>)
  774. assert_equal 'attachment', h.disposition
  775. assert_equal 1, h.params.size
  776. expected = "\223\372\226{\214\352.doc"
  777. expected.force_encoding 'Windows-31J' if expected.respond_to? :force_encoding
  778. assert_equal expected, h.params['filename']
  779. end
  780. def test_disposition=
  781. h = TMail::HeaderField.new('Content-Disposition',
  782. 'attachment; filename="README.txt.pif"')
  783. assert_equal 'attachment', h.disposition
  784. h.disposition = 'virus'
  785. assert_equal 'virus', h.disposition
  786. h.disposition = 'AtTaChMeNt'
  787. assert_equal 'attachment', h.disposition
  788. end
  789. def test_wrong_mail_header
  790. fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email9")
  791. assert_raise(TMail::SyntaxError) { TMail::Mail.parse(fixture) }
  792. end
  793. def test_decode_message_with_unknown_charset
  794. fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10")
  795. mail = TMail::Mail.parse(fixture)
  796. assert_nothing_raised { mail.body }
  797. end
  798. def test_decode_message_with_unquoted_atchar_in_header
  799. fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email11")
  800. mail = TMail::Mail.parse(fixture)
  801. assert_not_nil mail.from
  802. end
  803. def test_new_from_port_should_produce_a_header_object_of_the_correct_class
  804. p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
  805. h = TMail::HeaderField.new_from_port(p, 'Message-Id')
  806. assert_equal(TMail::MessageIdHeader, h.class)
  807. end
  808. def test_should_return_the_evelope_sender_when_given_from_without_a_colon
  809. p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
  810. h = TMail::HeaderField.new_from_port(p, 'EnvelopeSender')
  811. assert_equal("mike@envelope_sender.com.au", h.addrs.join)
  812. end
  813. def test_new_from_port_should_produce_a_header_object_that_contains_the_right_data
  814. p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
  815. h = TMail::HeaderField.new_from_port(p, 'From')
  816. assert_equal("Mikel Lindsaar <mikel@from_address.com>", h.addrs.join)
  817. end
  818. def test_unwrapping_a_long_header_field_using_new_from_port
  819. p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
  820. h = TMail::HeaderField.new_from_port(p, 'Content-Type')
  821. line = 'multipart/signed; protocol="application/pkcs7-signature"; boundary=Apple-Mail-42-587703407; micalg=sha1'
  822. assert(line =~ /multipart\/signed/)
  823. assert(line =~ /protocol="application\/pkcs7-signature"/)
  824. assert(line =~ /boundary=Apple-Mail-42-587703407/)
  825. assert(line =~ /micalg=sha1/)
  826. assert_equal(line.length, 103)
  827. end
  828. def test_returning_nil_if_there_is_no_match
  829. p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
  830. h = TMail::HeaderField.new_from_port(p, 'Received-Long-Header')
  831. assert_equal(h, nil)
  832. end
  833. end