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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Web.Mvc.Async;
using System.Web.Mvc.Async.Test;
using System.Web.Routing;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Test
{
public class AsyncControllerTest
{
[Fact]
public void ActionInvokerProperty()
{
// Arrange
EmptyController controller = new EmptyController();
// Act
IActionInvoker invoker = controller.ActionInvoker;
// Assert
Assert.IsType<AsyncControllerActionInvoker>(invoker);
}
[Fact]
public void AsyncManagerProperty()
{
// Arrange
EmptyController controller = new EmptyController();
// Act
AsyncManager asyncManager = controller.AsyncManager;
// Assert
Assert.NotNull(asyncManager);
}
[Fact]
public void Execute_ThrowsIfCalledMoreThanOnce()
{
// Arrange
IAsyncController controller = new EmptyController();
RequestContext requestContext = GetRequestContext("SomeAction");
// Act & assert
controller.BeginExecute(requestContext, null, null);
Assert.Throws<InvalidOperationException>(
delegate { controller.BeginExecute(requestContext, null, null); },
@"A single instance of controller 'System.Web.Mvc.Test.AsyncControllerTest+EmptyController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.");
}
[Fact]
public void Execute_ThrowsIfRequestContextIsNull()
{
// Arrange
IAsyncController controller = new EmptyController();
// Act & assert
Assert.ThrowsArgumentNull(
delegate { controller.BeginExecute(null, null, null); }, "requestContext");
}
[Fact]
public void ExecuteCore_Asynchronous_ActionFound()
{
// Arrange
MockAsyncResult innerAsyncResult = new MockAsyncResult();
Mock<IAsyncActionInvoker> mockActionInvoker = new Mock<IAsyncActionInvoker>();
mockActionInvoker.Setup(o => o.BeginInvokeAction(It.IsAny<ControllerContext>(), "SomeAction", It.IsAny<AsyncCallback>(), It.IsAny<object>())).Returns(innerAsyncResult);
mockActionInvoker.Setup(o => o.EndInvokeAction(innerAsyncResult)).Returns(true);
RequestContext requestContext = GetRequestContext("SomeAction");
EmptyController controller = new EmptyController()
{
ActionInvoker = mockActionInvoker.Object
};
// Act & assert
IAsyncResult outerAsyncResult = ((IAsyncController)controller).BeginExecute(requestContext, null, null);
Assert.False(controller.TempDataSaved);
((IAsyncController)controller).EndExecute(outerAsyncResult);
Assert.True(controller.TempDataSaved);
Assert.False(controller.HandleUnknownActionCalled);
}
[Fact]
public void ExecuteCore_Asynchronous_ActionNotFound()
{
// Arrange
MockAsyncResult innerAsyncResult = new MockAsyncResult();
Mock<IAsyncActionInvoker> mockActionInvoker = new Mock<IAsyncActionInvoker>();
mockActionInvoker.Setup(o => o.BeginInvokeAction(It.IsAny<ControllerContext>(), "SomeAction", It.IsAny<AsyncCallback>(), It.IsAny<object>())).Returns(innerAsyncResult);
mockActionInvoker.Setup(o => o.EndInvokeAction(innerAsyncResult)).Returns(false);
RequestContext requestContext = GetRequestContext("SomeAction");
EmptyController controller = new EmptyController()
{
ActionInvoker = mockActionInvoker.Object
};
// Act & assert
IAsyncResult outerAsyncResult = ((IAsyncController)controller).BeginExecute(requestContext, null, null);
Assert.False(controller.TempDataSaved);
((IAsyncController)controller).EndExecute(outerAsyncResult);
Assert.True(controller.TempDataSaved);
Assert.True(controller.HandleUnknownActionCalled);
}
[Fact]
public void ExecuteCore_Synchronous_ActionFound()
{
// Arrange
MockAsyncResult innerAsyncResult = new MockAsyncResult();
Mock<IActionInvoker> mockActionInvoker = new Mock<IActionInvoker>();
mockActionInvoker.Setup(o => o.InvokeAction(It.IsAny<ControllerContext>(), "SomeAction")).Returns(true);
RequestContext requestContext = GetRequestContext("SomeAction");
EmptyController controller = new EmptyController()
{
ActionInvoker = mockActionInvoker.Object
};
// Act & assert
IAsyncResult outerAsyncResult = ((IAsyncController)controller).BeginExecute(requestContext, null, null);
Assert.False(controller.TempDataSaved);
((IAsyncController)controller).EndExecute(outerAsyncResult);
Assert.True(controller.TempDataSaved);
Assert.False(controller.HandleUnknownActionCalled);
}
[Fact]
public void ExecuteCore_Synchronous_ActionNotFound()
{
// Arrange
MockAsyncResult innerAsyncResult = new MockAsyncResult();
Mock<IActionInvoker> mockActionInvoker = new Mock<IActionInvoker>();
mockActionInvoker.Setup(o => o.InvokeAction(It.IsAny<ControllerContext>(), "SomeAction")).Returns(false);
RequestContext requestContext = GetRequestContext("SomeAction");
EmptyController controller = new EmptyController()
{
ActionInvoker = mockActionInvoker.Object
};
// Act & assert
IAsyncResult outerAsyncResult = ((IAsyncController)controller).BeginExecute(requestContext, null, null);
Assert.False(controller.TempDataSaved);
((IAsyncController)controller).EndExecute(outerAsyncResult);
Assert.True(controller.TempDataSaved);
Assert.True(controller.HandleUnknownActionCalled);
}
[Fact]
public void ExecuteCore_SavesTempDataOnException()
{
// Arrange
Mock<IAsyncActionInvoker> mockActionInvoker = new Mock<IAsyncActionInvoker>();
mockActionInvoker
.Setup(o => o.BeginInvokeAction(It.IsAny<ControllerContext>(), "SomeAction", It.IsAny<AsyncCallback>(), It.IsAny<object>()))
.Throws(new Exception("Some exception text."));
RequestContext requestContext = GetRequestContext("SomeAction");
EmptyController controller = new EmptyController()
{
ActionInvoker = mockActionInvoker.Object
};
// Act & assert
Assert.Throws<Exception>(
delegate { ((IAsyncController)controller).BeginExecute(requestContext, null, null); },
@"Some exception text.");
Assert.True(controller.TempDataSaved);
}
[Fact]
public void CreateActionInvokerCallsIntoResolverInstance()
{
// Controller uses an IDependencyResolver to create an IActionInvoker.
var controller = new EmptyController();
Mock<IDependencyResolver> resolverMock = new Mock<IDependencyResolver>();
Mock<IAsyncActionInvoker> actionInvokerMock = new Mock<IAsyncActionInvoker>();
resolverMock.Setup(r => r.GetService(typeof(IAsyncActionInvoker))).Returns(actionInvokerMock.Object);
controller.Resolver = resolverMock.Object;
var ai = controller.CreateActionInvoker();
resolverMock.Verify(r => r.GetService(typeof(IAsyncActionInvoker)), Times.Once());
Assert.Same(actionInvokerMock.Object, ai);
}
[Fact]
public void CreateActionInvokerCallsIntoResolverInstanceAndCreatesANewOneIfNecessary()
{
// If IDependencyResolver is set, but empty, falls back and still creates.
var controller = new EmptyController();
Mock<IDependencyResolver> resolverMock = new Mock<IDependencyResolver>();
resolverMock.Setup(r => r.GetService(typeof(IAsyncActionInvoker))).Returns(null);
resolverMock.Setup(r => r.GetService(typeof(IActionInvoker))).Returns(null);
controller.Resolver = resolverMock.Object;
var ai = controller.CreateActionInvoker();
resolverMock.Verify(r => r.GetService(typeof(IAsyncActionInvoker)), Times.Once());
resolverMock.Verify(r => r.GetService(typeof(IActionInvoker)), Times.Once());
Assert.NotNull(ai);
}
private static RequestContext GetRequestContext(string actionName)
{
Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
RouteData routeData = new RouteData();
routeData.Values["action"] = actionName;
return new RequestContext(mockHttpContext.Object, routeData);
}
private class EmptyController : AsyncController
{
public bool TempDataSaved;
public bool HandleUnknownActionCalled;
protected override ITempDataProvider CreateTempDataProvider()
{
return new DummyTempDataProvider();
}
protected override void HandleUnknownAction(string actionName)
{
HandleUnknownActionCalled = true;
}
// Test can expose protected method as public.
public new IActionInvoker CreateActionInvoker()
{
return base.CreateActionInvoker();
}
private class DummyTempDataProvider : ITempDataProvider
{
public IDictionary<string, object> LoadTempData(ControllerContext controllerContext)
{
return new TempDataDictionary();
}
public void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values)
{
((EmptyController)controllerContext.Controller).TempDataSaved = true;
}
}
}
}
}
|