summaryrefslogtreecommitdiff
path: root/mcs/class/System/Test/System.Net.Mail/MailMessageTest.cs
blob: 62678501c84725bcce095c447cfcd1f9887f2ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// MailMessageTest.cs - NUnit Test Cases for System.Net.MailAddress.MailMessage
//
// Authors:
//   John Luke (john.luke@gmail.com)
//
// (C) 2005, 2006 John Luke
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.IO;
using System.Text;
using System.Net.Mail;

namespace MonoTests.System.Net.Mail
{
	[TestFixture]
	public class MailMessageTest
	{
		MailMessage msg;
		MailMessage msg2;
		
		[SetUp]
		public void GetReady ()
		{
			msg = new MailMessage ("from@example.com", "to@example.com");
			msg.Subject = "the subject";
			msg.Body = "hello";
			//msg.AlternateViews.Add (AlternateView.CreateAlternateViewFromString ("<html><body>hello</body></html>", "text/html"));
			//Attachment a = Attachment.CreateAttachmentFromString ("blah blah", "text/plain");
			//msg.Attachments.Add (a);

			msg2 = new MailMessage ("from@example.com", "r1@t1.com, r2@t1.com");
		}

		[Test]
		public void TestRecipients ()
		{
			Assert.AreEqual (msg2.To.Count,  2);
			Assert.AreEqual (msg2.To[0].Address, "r1@t1.com");
			Assert.AreEqual (msg2.To[1].Address, "r2@t1.com");
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ArgumentNullCtor1 ()
		{
			new MailMessage (null, "to@example.com");
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ArgumentNullCtor2 ()
		{
			new MailMessage (null, new MailAddress ("to@example.com"));
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ArgumentNullCtor3 ()
		{
			new MailMessage ("from@example.com", null);
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ArgumentNullCtor4 ()
		{
			new MailMessage (new MailAddress ("from@example.com"), null);
		}

		/*[Test]
		public void AlternateView ()
		{
			Assert.AreEqual (msg.AlternateViews.Count, 1);
			AlternateView av = msg.AlternateViews[0];
			// test that the type is ok, etc.
		}*/

		/*[Test]
		public void Attachment ()
		{
			Assert.AreEqual (msg.Attachments.Count, 1);
			Attachment at = msg.Attachments[0];
			Assert.AreEqual (at.ContentType.MediaType, "text/plain");
		}*/

		[Test]
		public void Body ()
		{
			Assert.AreEqual (msg.Body, "hello");
		}
		
		[Test]
		public void BodyEncoding ()
		{
			Assert.AreEqual (msg.BodyEncoding, Encoding.ASCII);
		}

		[Test]
		public void From ()
		{
			Assert.AreEqual (msg.From.Address, "from@example.com");
		}

		[Test]
		public void IsBodyHtml ()
		{
			Assert.IsFalse (msg.IsBodyHtml);
		}

		[Test]
		public void Priority ()
		{
			Assert.AreEqual (msg.Priority, MailPriority.Normal);
		}

		[Test]
		public void Subject ()
		{
			Assert.AreEqual (msg.Subject, "the subject");
		}

		[Test]
		public void To ()
		{
			Assert.AreEqual (msg.To.Count, 1, "#A1");
			Assert.AreEqual (msg.To[0].Address, "to@example.com", "#A2");

			msg = new MailMessage ();
			msg.To.Add ("to@example.com");
			msg.To.Add ("you@nowhere.com");
			Assert.AreEqual (msg.To.Count, 2, "#B1");
			Assert.AreEqual (msg.To[0].Address, "to@example.com", "#B2");
			Assert.AreEqual (msg.To[1].Address, "you@nowhere.com", "#B3");
		}

		[Test]
		public void BodyAndEncoding ()
		{
			MailMessage msg = new MailMessage ("from@example.com", "to@example.com");
			Assert.AreEqual (null, msg.BodyEncoding, "#1");
			msg.Body = "test";
			Assert.AreEqual (Encoding.ASCII, msg.BodyEncoding, "#2");
			msg.Body = "test\u3067\u3059";
			Assert.AreEqual (Encoding.ASCII, msg.BodyEncoding, "#3");
			msg.BodyEncoding = null;
			msg.Body = "test\u3067\u3059";
			Assert.AreEqual (Encoding.UTF8.CodePage, msg.BodyEncoding.CodePage, "#4");
		}

		[Test]
		public void SubjectAndEncoding ()
		{
			MailMessage msg = new MailMessage ("from@example.com", "to@example.com");
			Assert.AreEqual (null, msg.SubjectEncoding, "#1");
			msg.Subject = "test";
			Assert.AreEqual (null, msg.SubjectEncoding, "#2");
			msg.Subject = "test\u3067\u3059";
			Assert.AreEqual (Encoding.UTF8.CodePage, msg.SubjectEncoding.CodePage, "#3");
			msg.SubjectEncoding = null;
			msg.Subject = "test\u3067\u3059";
			Assert.AreEqual (Encoding.UTF8.CodePage, msg.SubjectEncoding.CodePage, "#4");
		}
	}
}
#endif