// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Web.UnitTestUtil;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace Microsoft.Web.Mvc.Test
{
public class MailToExtensionsTest
{
[Fact]
public void MailToWithoutEmailThrowsArgumentNullException()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
Assert.ThrowsArgumentNull(() => html.Mailto("link text", null), "emailAddress");
}
[Fact]
public void MailToWithoutLinkTextThrowsArgumentNullException()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
Assert.ThrowsArgumentNull(() => html.Mailto(null, "somebody@example.com"), "linkText");
}
[Fact]
public void MailToWithLinkTextAndEmailRendersProperElement()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
MvcHtmlString result = html.Mailto("This is a test", "test@example.com");
Assert.Equal("This is a test", result.ToHtmlString());
}
[Fact]
public void MailToWithLinkTextEmailAndHtmlAttributesRendersAttributes()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
MvcHtmlString result = html.Mailto("This is a test", "test@example.com", new { title = "this is a test" });
Assert.Equal("This is a test", result.ToHtmlString());
}
[Fact]
public void MailToWithLinkTextEmailAndHtmlAttributesDictionaryRendersAttributes()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
MvcHtmlString result = html.Mailto("This is a test", "test@example.com", new RouteValueDictionary(new { title = "this is a test" }));
Assert.Equal("This is a test", result.ToHtmlString());
}
[Fact]
public void MailToWithSubjectAndHtmlAttributesRendersAttributes()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
MvcHtmlString result = html.Mailto("This is a test", "test@example.com", "The subject", new { title = "this is a test" });
Assert.Equal("This is a test", result.ToHtmlString());
}
[Fact]
public void MailToWithSubjectAndHtmlAttributesDictionaryRendersAttributes()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
MvcHtmlString result = html.Mailto("This is a test", "test@example.com", "The subject", new RouteValueDictionary(new { title = "this is a test" }));
Assert.Equal("This is a test", result.ToHtmlString());
}
[Fact]
public void MailToAttributeEncodesEmail()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
MvcHtmlString result = html.Mailto("This is a test", "te\">st@example.com");
Assert.Equal("st@example.com\">This is a test", result.ToHtmlString());
}
[Fact]
public void MailToWithMultipleRecipientsRendersWithCommas()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
MvcHtmlString result = html.Mailto("This is a test", "te\">st@example.com,test2@example.com");
Assert.Equal("st@example.com,test2@example.com\">This is a test", result.ToHtmlString());
}
[Fact]
public void MailToWithSubjectAppendsSubjectQuery()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
MvcHtmlString result = html.Mailto("This is a test", "test@example.com", "This is the subject");
Assert.Equal("This is a test", result.ToHtmlString());
}
[Fact]
public void MailToWithCopyOnlyAppendsCopyQuery()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
MvcHtmlString result = html.Mailto("This is a test", "test@example.com", null, null, "cctest@example.com", null, null);
Assert.Equal("This is a test", result.ToHtmlString());
}
[Fact]
public void MailToWithMultipartBodyRendersProperMailtoEncoding()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
string body = @"Line one
Line two
Line three";
MvcHtmlString result = html.Mailto("email me", "test@example.com", null, body, null, null, null);
Assert.Equal("email me", result.ToHtmlString());
}
[Fact]
public void MailToWithAllValuesProvidedRendersCorrectTag()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
string body = @"Line one
Line two
Line three";
MvcHtmlString result = html.Mailto("email me", "test@example.com", "the subject", body, "cc@example.com", "bcc@example.com", new { title = "email test" });
string expected = @"email me";
Assert.Equal(expected, result.ToHtmlString());
}
[Fact]
public void MailToWithAttributesWithUnderscores()
{
HtmlHelper html = MvcHelper.GetHtmlHelperWithPath(new ViewDataDictionary());
string body = @"Line one
Line two
Line three";
MvcHtmlString result = html.Mailto("email me", "test@example.com", "the subject", body, "cc@example.com", "bcc@example.com", new { foo_bar = "baz" });
string expected = @"email me";
Assert.Equal(expected, result.ToHtmlString());
}
}
}