// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Microsoft.Web.UnitTestUtil;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Ajax.Test
{
public class AjaxExtensionsTest
{
// Guards
[Fact]
public void ActionLinkWithNullOrEmptyLinkTextThrows()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper();
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { MvcHtmlString actionLink = ajaxHelper.ActionLink(String.Empty, String.Empty, null, null, null, null); },
"linkText");
}
[Fact]
public void RouteLinkWithNullOrEmptyLinkTextThrows()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper();
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { MvcHtmlString actionLink = ajaxHelper.RouteLink(String.Empty, String.Empty, null, null, null); },
"linkText");
}
// Form context setup and cleanup
[Fact]
public void BeginFormSetsAndRestoresToDefault()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper();
ajaxHelper.ViewContext.FormContext = null;
FormContext defaultFormContext = ajaxHelper.ViewContext.FormContext;
// Act & assert - push
MvcForm theForm = ajaxHelper.BeginForm(new AjaxOptions());
Assert.NotNull(ajaxHelper.ViewContext.FormContext);
Assert.NotEqual(defaultFormContext, ajaxHelper.ViewContext.FormContext);
// Act & assert - pop
theForm.Dispose();
Assert.Equal(defaultFormContext, ajaxHelper.ViewContext.FormContext);
}
[Fact]
public void DisposeWritesClosingFormTag()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper();
AjaxOptions ajaxOptions = new AjaxOptions { UpdateTargetId = "some-id" };
StringWriter writer = new StringWriter();
ajaxHelper.ViewContext.Writer = writer;
// Act
IDisposable form = ajaxHelper.BeginForm("Action", "Controller", ajaxOptions);
form.Dispose();
// Assert
Assert.True(writer.ToString().EndsWith(""));
}
// GlobalizationScript
[Fact]
public void GlobalizationScriptWithNullCultureInfoThrows()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper();
// Act & Assert
Assert.ThrowsArgumentNull(
delegate { ajaxHelper.GlobalizationScript(null); },
"cultureInfo");
}
[Fact]
public void GlobalizationScriptUsesCurrentCultureAsDefault()
{
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
try
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper();
AjaxHelper.GlobalizationScriptPath = null;
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");
// Act
MvcHtmlString globalizationScript = ajaxHelper.GlobalizationScript();
// Assert
Assert.Equal(@"", globalizationScript.ToHtmlString());
}
finally
{
Thread.CurrentThread.CurrentCulture = currentCulture;
}
}
[Fact]
public void GlobalizationScriptWithCultureInfo()
{
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
try
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper();
AjaxHelper.GlobalizationScriptPath = null;
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");
// Act
MvcHtmlString globalizationScript = ajaxHelper.GlobalizationScript(CultureInfo.GetCultureInfo("en-CA"));
// Assert
Assert.Equal(@"", globalizationScript.ToHtmlString());
}
finally
{
Thread.CurrentThread.CurrentCulture = currentCulture;
}
}
[Fact]
public void GlobalizationScriptEncodesSource()
{
// Arrange
Mock xssCulture = new Mock("en-US");
xssCulture.Setup(culture => culture.Name).Returns("evil.example.com/");
string globalizationPath = "~/Scripts&Globalization";
string expectedScriptTag = @"";
// Act
MvcHtmlString globalizationScript = AjaxExtensions.GlobalizationScriptHelper(globalizationPath, xssCulture.Object);
// Assert
Assert.Equal(expectedScriptTag, globalizationScript.ToHtmlString());
}
[Fact]
public void GlobalizationScriptWithNullCultureName()
{
// Arrange
Mock xssCulture = new Mock("en-US");
xssCulture.Setup(culture => culture.Name).Returns((string)null);
AjaxHelper ajaxHelper = GetAjaxHelper();
AjaxHelper.GlobalizationScriptPath = null;
// Act
MvcHtmlString globalizationScript = ajaxHelper.GlobalizationScript(xssCulture.Object);
// Assert
Assert.Equal(@"", globalizationScript.ToHtmlString());
}
// ActionLink (traditional JavaScript)
[Fact]
public void ActionLinkWithNullActionName()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", null, new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkWithNullActionName_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", null, new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkWithNullActionNameAndNullOptions()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", null, null);
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkWithNullActionNameAndNullOptions_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", null, null);
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLink()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLink_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkAnonymousValues()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
object values = new { controller = "Controller" };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkAnonymousValues_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
object values = new { controller = "Controller" };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkAnonymousValuesAndAttributes()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
object htmlAttributes = new { foo = "bar", baz = "quux", foo_bar = "baz_quux" };
object values = new { controller = "Controller" };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkAnonymousValuesAndAttributes_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
object htmlAttributes = new { foo = "bar", baz = "quux", foo_bar = "baz_quux" };
object values = new { controller = "Controller" };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkTypedValues()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkTypedValues_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkTypedValuesAndAttributes()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" }
};
Dictionary htmlAttributes = new Dictionary
{
{ "foo", "bar" },
{ "baz", "quux" },
{ "foo_bar", "baz_quux" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkTypedValuesAndAttributes_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" }
};
Dictionary htmlAttributes = new Dictionary
{
{ "foo", "bar" },
{ "baz", "quux" },
{ "foo_bar", "baz_quux" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkController()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", "Controller", new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkController_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", "Controller", new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkControllerAnonymousValues()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
object values = new { id = 5 };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", "Controller", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkControllerAnonymousValues_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
object values = new { id = 5 };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", "Controller", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkControllerAnonymousValuesAndAttributes()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
object htmlAttributes = new { foo = "bar", baz = "quux", foo_bar = "baz_quux" };
object values = new { id = 5 };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", "Controller", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkControllerAnonymousValuesAndAttributes_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
object htmlAttributes = new { foo = "bar", baz = "quux", foo_bar = "baz_quux" };
object values = new { id = 5 };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", "Controller", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkControllerTypedValues()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
RouteValueDictionary values = new RouteValueDictionary
{
{ "id", 5 }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", "Controller", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkControllerTypedValues_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
RouteValueDictionary values = new RouteValueDictionary
{
{ "id", 5 }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", "Controller", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkControllerTypedValuesAndAttributes()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
RouteValueDictionary values = new RouteValueDictionary
{
{ "id", 5 }
};
Dictionary htmlAttributes = new Dictionary
{
{ "foo", "bar" },
{ "baz", "quux" },
{ "foo_bar", "baz_quux" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", "Controller", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkControllerTypedValuesAndAttributes_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
RouteValueDictionary values = new RouteValueDictionary
{
{ "id", 5 }
};
Dictionary htmlAttributes = new Dictionary
{
{ "foo", "bar" },
{ "baz", "quux" },
{ "foo_bar", "baz_quux" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.ActionLink("Some Text", "Action", "Controller", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkWithOptions()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", "Controller", new AjaxOptions { UpdateTargetId = "some-id" });
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkWithOptions_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
AjaxOptions options = new AjaxOptions { UpdateTargetId = "some-id" };
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", "Controller", options);
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkWithNullHostName()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", "Controller",
null, null, null, null, new AjaxOptions { UpdateTargetId = "some-id" }, null);
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkWithNullHostName_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", "Controller",
null, null, null, null, new AjaxOptions { UpdateTargetId = "some-id" }, null);
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkWithProtocol()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", "Controller", "https", null, null, null, new AjaxOptions { UpdateTargetId = "some-id" }, null);
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void ActionLinkWithProtocol_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
// Act
MvcHtmlString actionLink = ajaxHelper.ActionLink("linkText", "Action", "Controller", "https", null, null, null, new AjaxOptions { UpdateTargetId = "some-id" }, null);
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
// RouteLink
[Fact]
public void RouteLinkWithNullOptions()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
// Act
MvcHtmlString routeLink = ajaxHelper.RouteLink("Some Text", new RouteValueDictionary(), null);
// Assert
Assert.Equal(@"Some Text", routeLink.ToHtmlString());
}
[Fact]
public void RouteLinkWithNullOptions_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
// Act
MvcHtmlString routeLink = ajaxHelper.RouteLink("Some Text", new RouteValueDictionary(), null);
// Assert
Assert.Equal(@"Some Text", routeLink.ToHtmlString());
}
[Fact]
public void RouteLinkAnonymousValues()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
object values = new
{
action = "Action",
controller = "Controller"
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString routeLink = helper.RouteLink("Some Text", values, options);
// Assert
Assert.Equal(@"Some Text", routeLink.ToHtmlString());
}
[Fact]
public void RouteLinkAnonymousValues_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
object values = new
{
action = "Action",
controller = "Controller"
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString routeLink = helper.RouteLink("Some Text", values, options);
// Assert
Assert.Equal(@"Some Text", routeLink.ToHtmlString());
}
[Fact]
public void RouteLinkAnonymousValuesAndAttributes()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
object htmlAttributes = new
{
foo = "bar",
baz = "quux",
foo_bar = "baz_quux"
};
object values = new
{
action = "Action",
controller = "Controller"
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.RouteLink("Some Text", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkAnonymousValuesAndAttributes_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
object htmlAttributes = new
{
foo = "bar",
baz = "quux",
foo_bar = "baz_quux"
};
object values = new
{
action = "Action",
controller = "Controller"
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.RouteLink("Some Text", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkTypedValues()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" },
{ "action", "Action" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.RouteLink("Some Text", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkTypedValues_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" },
{ "action", "Action" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.RouteLink("Some Text", values, options);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkTypedValuesAndAttributes()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: false);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" },
{ "action", "Action" }
};
Dictionary htmlAttributes = new Dictionary
{
{ "foo", "bar" },
{ "baz", "quux" },
{ "foo_bar", "baz_quux" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.RouteLink("Some Text", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkTypedValuesAndAttributes_Unobtrusive()
{
// Arrange
AjaxHelper helper = GetAjaxHelper(unobtrusiveJavaScript: true);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" },
{ "action", "Action" }
};
Dictionary htmlAttributes = new Dictionary
{
{ "foo", "bar" },
{ "baz", "quux" },
{ "foo_bar", "baz_quux" }
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = helper.RouteLink("Some Text", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRoute()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("linkText", "namedroute", new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRoute_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("linkText", "namedroute", new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteAnonymousAttributes()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
object htmlAttributes = new
{
foo = "bar",
baz = "quux",
foo_bar = "baz_quux"
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteAnonymousAttributes_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
object htmlAttributes = new
{
foo = "bar",
baz = "quux",
foo_bar = "baz_quux"
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteTypedAttributes()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
Dictionary htmlAttributes = new Dictionary { { "foo", "bar" }, { "baz", "quux" }, { "foo_bar", "baz_quux" } };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteTypedAttributes_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
Dictionary htmlAttributes = new Dictionary { { "foo", "bar" }, { "baz", "quux" }, { "foo_bar", "baz_quux" } };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteWithAnonymousValues()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
object values = new
{
action = "Action",
controller = "Controller"
};
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("linkText", "namedroute", values, new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteWithAnonymousValues_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
object values = new
{
action = "Action",
controller = "Controller"
};
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("linkText", "namedroute", values, new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteAnonymousValuesAndAttributes()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
object values = new
{
action = "Action",
controller = "Controller"
};
object htmlAttributes = new
{
foo = "bar",
baz = "quux",
foo_bar = "baz_quux"
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteAnonymousValuesAndAttributes_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
object values = new
{
action = "Action",
controller = "Controller"
};
object htmlAttributes = new
{
foo = "bar",
baz = "quux",
foo_bar = "baz_quux"
};
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteWithTypedValues()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" },
{ "action", "Action" }
};
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("linkText", "namedroute", values, new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteWithTypedValues_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" },
{ "action", "Action" }
};
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("linkText", "namedroute", values, new AjaxOptions());
// Assert
Assert.Equal(@"linkText", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteTypedValuesAndAttributes()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" },
{ "action", "Action" }
};
Dictionary htmlAttributes = new Dictionary { { "foo", "bar" }, { "baz", "quux" }, { "foo_bar", "baz_quux" } };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteTypedValuesAndAttributes_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
RouteValueDictionary values = new RouteValueDictionary
{
{ "controller", "Controller" },
{ "action", "Action" }
};
Dictionary htmlAttributes = new Dictionary { { "foo", "bar" }, { "baz", "quux" }, { "foo_bar", "baz_quux" } };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", values, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteNullValuesAndAttributes()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
Dictionary htmlAttributes = new Dictionary { { "foo", "bar" }, { "baz", "quux" }, { "foo_bar", "baz_quux" } };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", null, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkNamedRouteNullValuesAndAttributes_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
Dictionary htmlAttributes = new Dictionary { { "foo", "bar" }, { "baz", "quux" }, { "foo_bar", "baz_quux" } };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", null, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkWithHostName()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
Dictionary htmlAttributes = new Dictionary { { "foo", "bar" }, { "baz", "quux" }, { "foo_bar", "baz_quux" } };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", null, "baz.bar.foo", null, null, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
[Fact]
public void RouteLinkWithHostName_Unobtrusive()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: true);
Dictionary htmlAttributes = new Dictionary { { "foo", "bar" }, { "baz", "quux" }, { "foo_bar", "baz_quux" } };
AjaxOptions options = new AjaxOptions { UpdateTargetId = "update-div" };
// Act
MvcHtmlString actionLink = ajaxHelper.RouteLink("Some Text", "namedroute", null, "baz.bar.foo", null, null, options, htmlAttributes);
// Assert
Assert.Equal(@"Some Text", actionLink.ToHtmlString());
}
// BeginForm
[Fact]
public void BeginFormOnlyWithNullOptions()
{
// Arrange
AjaxHelper ajaxHelper = GetAjaxHelper(unobtrusiveJavaScript: false);
StringWriter writer = new StringWriter();
ajaxHelper.ViewContext.Writer = writer;
// Act
IDisposable form = ajaxHelper.BeginForm(null);
// Assert
Assert.Equal(@"