MailMessageTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // MailMessageTest.cs - NUnit Test Cases for System.Net.MailAddress.MailMessage
  3. //
  4. // Authors:
  5. // John Luke ([email protected])
  6. //
  7. // (C) 2005, 2006 John Luke
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.IO;
  12. using System.Text;
  13. using System.Net.Mail;
  14. namespace MonoTests.System.Net.Mail
  15. {
  16. [TestFixture]
  17. public class MailMessageTest
  18. {
  19. MailMessage msg;
  20. MailMessage msg2;
  21. [SetUp]
  22. public void GetReady ()
  23. {
  24. msg = new MailMessage ("[email protected]", "[email protected]");
  25. msg.Subject = "the subject";
  26. msg.Body = "hello";
  27. //msg.AlternateViews.Add (AlternateView.CreateAlternateViewFromString ("<html><body>hello</body></html>", "text/html"));
  28. //Attachment a = Attachment.CreateAttachmentFromString ("blah blah", "text/plain");
  29. //msg.Attachments.Add (a);
  30. msg2 = new MailMessage ("[email protected]", "[email protected], [email protected]");
  31. }
  32. [Test]
  33. public void TestRecipients ()
  34. {
  35. Assert.AreEqual (msg2.To.Count, 2);
  36. Assert.AreEqual (msg2.To[0].Address, "[email protected]");
  37. Assert.AreEqual (msg2.To[1].Address, "[email protected]");
  38. }
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void ArgumentNullCtor1 ()
  42. {
  43. new MailMessage (null, "[email protected]");
  44. }
  45. [Test]
  46. [ExpectedException (typeof (ArgumentNullException))]
  47. public void ArgumentNullCtor2 ()
  48. {
  49. new MailMessage (null, new MailAddress ("[email protected]"));
  50. }
  51. [Test]
  52. [ExpectedException (typeof (ArgumentNullException))]
  53. public void ArgumentNullCtor3 ()
  54. {
  55. new MailMessage ("[email protected]", null);
  56. }
  57. [Test]
  58. [ExpectedException (typeof (ArgumentNullException))]
  59. public void ArgumentNullCtor4 ()
  60. {
  61. new MailMessage (new MailAddress ("[email protected]"), null);
  62. }
  63. /*[Test]
  64. public void AlternateView ()
  65. {
  66. Assert.AreEqual (msg.AlternateViews.Count, 1);
  67. AlternateView av = msg.AlternateViews[0];
  68. // test that the type is ok, etc.
  69. }*/
  70. /*[Test]
  71. public void Attachment ()
  72. {
  73. Assert.AreEqual (msg.Attachments.Count, 1);
  74. Attachment at = msg.Attachments[0];
  75. Assert.AreEqual (at.ContentType.MediaType, "text/plain");
  76. }*/
  77. [Test]
  78. public void Body ()
  79. {
  80. Assert.AreEqual (msg.Body, "hello");
  81. }
  82. [Test]
  83. public void BodyEncoding ()
  84. {
  85. Assert.AreEqual (msg.BodyEncoding, Encoding.ASCII);
  86. }
  87. [Test]
  88. public void From ()
  89. {
  90. Assert.AreEqual (msg.From.Address, "[email protected]");
  91. }
  92. [Test]
  93. public void IsBodyHtml ()
  94. {
  95. Assert.IsFalse (msg.IsBodyHtml);
  96. }
  97. [Test]
  98. public void Priority ()
  99. {
  100. Assert.AreEqual (msg.Priority, MailPriority.Normal);
  101. }
  102. [Test]
  103. public void Subject ()
  104. {
  105. Assert.AreEqual (msg.Subject, "the subject");
  106. }
  107. [Test]
  108. public void To ()
  109. {
  110. Assert.AreEqual (msg.To.Count, 1, "#A1");
  111. Assert.AreEqual (msg.To[0].Address, "[email protected]", "#A2");
  112. msg = new MailMessage ();
  113. msg.To.Add ("[email protected]");
  114. msg.To.Add ("[email protected]");
  115. Assert.AreEqual (msg.To.Count, 2, "#B1");
  116. Assert.AreEqual (msg.To[0].Address, "[email protected]", "#B2");
  117. Assert.AreEqual (msg.To[1].Address, "[email protected]", "#B3");
  118. }
  119. [Test]
  120. public void BodyAndEncoding ()
  121. {
  122. MailMessage msg = new MailMessage ("[email protected]", "[email protected]");
  123. Assert.AreEqual (null, msg.BodyEncoding, "#1");
  124. msg.Body = "test";
  125. Assert.AreEqual (Encoding.ASCII, msg.BodyEncoding, "#2");
  126. msg.Body = "test\u3067\u3059";
  127. Assert.AreEqual (Encoding.ASCII, msg.BodyEncoding, "#3");
  128. msg.BodyEncoding = null;
  129. msg.Body = "test\u3067\u3059";
  130. Assert.AreEqual (Encoding.UTF8.CodePage, msg.BodyEncoding.CodePage, "#4");
  131. }
  132. [Test]
  133. public void SubjectAndEncoding ()
  134. {
  135. MailMessage msg = new MailMessage ("[email protected]", "[email protected]");
  136. Assert.AreEqual (null, msg.SubjectEncoding, "#1");
  137. msg.Subject = "test";
  138. Assert.AreEqual (null, msg.SubjectEncoding, "#2");
  139. msg.Subject = "test\u3067\u3059";
  140. Assert.AreEqual (Encoding.UTF8.CodePage, msg.SubjectEncoding.CodePage, "#3");
  141. msg.SubjectEncoding = null;
  142. msg.Subject = "test\u3067\u3059";
  143. Assert.AreEqual (Encoding.UTF8.CodePage, msg.SubjectEncoding.CodePage, "#4");
  144. }
  145. }
  146. }