summaryrefslogtreecommitdiffstats
path: root/SendGrid/Tests/Transport/TestWebApi.cs
blob: 6a4fa0c01f53f95c530ebd1568ec57d89c13e6af (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using Moq;
using NUnit.Framework;
using SendGrid;

namespace Tests.Transport
{
	[TestFixture]
	internal class TestWebApi
	{
		private const string TestUsername = "username";
		private const string TestPassword = "password";

		[Test]
		public void TestFetchFileBodies()
		{
			var webApi = new Web(new NetworkCredential(TestUsername, TestPassword));
			var message = new Mock<ISendGrid>();
			var attachments = new[] {"foo", "bar", "foobar"};
			message.SetupProperty(foo => foo.Attachments, null);
			var result = webApi.FetchFileBodies(message.Object);
			Assert.AreEqual(0, result.Count);

			message.SetupProperty(foo => foo.Attachments, attachments);
			result = webApi.FetchFileBodies(message.Object);
			Assert.AreEqual(attachments.Count(), result.Count);
			for (var index = 0; index < attachments.Length; index++)
				Assert.AreEqual(result[index].Value.Name, attachments[index]);
		}

		[Test]
		public void TestFetchFormParams()
		{
			// Test Variables
			const string toAddress = "foobar@outlook.com";
		    const string ccAddress = "cc@outlook.com";
		    const string bcc1Address = "bcc1@outlook.com";
		    const string bcc2Address = "bcc2@outlook.com";
		    MailAddress[] bccAddresses = {new MailAddress(bcc1Address), new MailAddress(bcc2Address)};
			const string fromAddress = "test@outlook.com";
			const string subject = "Test Subject";
			const string textBody = "Test Text Body";
			const string htmlBody = "<p>Test HTML Body</p>";
			const string headerKey = "headerkey";
			var testHeader = new Dictionary<string, string> { { headerKey, "headervalue" } };
			const string categoryName = "Example Category";

			var message = new SendGridMessage();
			message.AddTo(toAddress);
            message.AddCc(ccAddress);
		    message.Bcc = bccAddresses;
			message.From = new MailAddress(fromAddress);
			message.Subject = subject;
			message.Text = textBody;
			message.Html = htmlBody;
			message.AddHeaders(testHeader);
			message.Header.SetCategory(categoryName);

			var webApi = new Web(new NetworkCredential(TestUsername, TestPassword));
			var result = webApi.FetchFormParams(message);
			Assert.True(result.Any(r => r.Key == "api_user" && r.Value == TestUsername));
			Assert.True(result.Any(r => r.Key == "api_key" && r.Value == TestPassword));
			Assert.True(result.Any(r => r.Key == "to[]" && r.Value == toAddress));
            Assert.True(result.Any(r => r.Key == "cc[]" && r.Value == ccAddress));
            Assert.True(result.Any(r => r.Key == "bcc[]" && r.Value == bcc1Address));
            Assert.True(result.Any(r => r.Key == "bcc[]" && r.Value == bcc2Address));
			Assert.True(result.Any(r => r.Key == "from" && r.Value == fromAddress));
			Assert.True(result.Any(r => r.Key == "subject" && r.Value == subject));
			Assert.True(result.Any(r => r.Key == "text" && r.Value == textBody));
			Assert.True(result.Any(r => r.Key == "html" && r.Value == htmlBody));
			Assert.True(
				result.Any(
					r => r.Key == "headers" && r.Value == String.Format("{{\"{0}\":\"{1}\"}}", headerKey, testHeader[headerKey])));
			Assert.True(
				result.Any(r => r.Key == "x-smtpapi" && r.Value == String.Format("{{\"category\" : \"{0}\"}}", categoryName)));
		}
	}
}