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
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace Microsoft.Web.Mvc.Test
{
public class CopyAsyncParametersAttributeTest
{
[Fact]
public void OnActionExecuting_CopiesParametersIfControllerIsAsync()
{
// Arrange
CopyAsyncParametersAttribute attr = new CopyAsyncParametersAttribute();
SampleAsyncController controller = new SampleAsyncController();
ActionExecutingContext filterContext = new ActionExecutingContext
{
ActionParameters = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase),
Controller = controller
};
filterContext.ActionParameters["foo"] = "fooAction";
filterContext.ActionParameters["bar"] = "barAction";
controller.AsyncManager.Parameters["bar"] = "barAsync";
controller.AsyncManager.Parameters["baz"] = "bazAsync";
// Act
attr.OnActionExecuting(filterContext);
// Assert
Assert.Equal("fooAction", controller.AsyncManager.Parameters["foo"]);
Assert.Equal("barAction", controller.AsyncManager.Parameters["bar"]);
Assert.Equal("bazAsync", controller.AsyncManager.Parameters["baz"]);
}
[Fact]
public void OnActionExecuting_DoesNothingIfControllerNotAsync()
{
// Arrange
CopyAsyncParametersAttribute attr = new CopyAsyncParametersAttribute();
SampleSyncController controller = new SampleSyncController();
ActionExecutingContext filterContext = new ActionExecutingContext
{
ActionParameters = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase),
Controller = controller
};
filterContext.ActionParameters["foo"] = "originalFoo";
filterContext.ActionParameters["bar"] = "originalBar";
// Act
attr.OnActionExecuting(filterContext);
// Assert
// If we got this far without crashing, life is good :)
}
[Fact]
public void OnActionExecuting_ThrowsIfFilterContextIsNull()
{
// Arrange
CopyAsyncParametersAttribute attr = new CopyAsyncParametersAttribute();
// Act & assert
Assert.ThrowsArgumentNull(
delegate { attr.OnActionExecuting(null); }, "filterContext");
}
private class SampleSyncController : Controller
{
}
private class SampleAsyncController : AsyncController
{
}
}
}
|