示例#1
0
        private static void TestExceptionFilter(ApiController controller, Exception expectedException,
                                                Action <HttpConfiguration> configure)
        {
            // Arrange
            Exception      actualException = null;
            IHttpRouteData routeData       = new HttpRouteData(new HttpRoute());

            using (HttpRequestMessage request = new HttpRequestMessage())
                using (HttpConfiguration configuration = new HttpConfiguration())
                    using (HttpResponseMessage response = new HttpResponseMessage())
                    {
                        HttpControllerContext    context = new HttpControllerContext(configuration, routeData, request);
                        HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(configuration,
                                                                                                     "Ignored", controller.GetType());
                        context.Controller           = controller;
                        context.ControllerDescriptor = controllerDescriptor;

                        if (configure != null)
                        {
                            configure.Invoke(configuration);
                        }

                        Mock <IExceptionFilter> spy = new Mock <IExceptionFilter>();
                        spy.Setup(f => f.ExecuteExceptionFilterAsync(It.IsAny <HttpActionExecutedContext>(),
                                                                     It.IsAny <CancellationToken>())).Returns <HttpActionExecutedContext, CancellationToken>(
                            (c, i) =>
                        {
                            actualException = c.Exception;
                            c.Response      = response;
                            return(Task.FromResult <object>(null));
                        });

                        configuration.Filters.Add(spy.Object);

                        // Act
                        HttpResponseMessage ignore = controller.ExecuteAsync(context, CancellationToken.None).Result;
                    }

            // Assert
            Assert.Same(expectedException, actualException);
        }
示例#2
0
        public void ExecuteAsync_InvokesAuthorizationFilters_ThenInvokesModelBinding_ThenInvokesActionFilters_ThenInvokesAction()
        {
            List <string>        log            = new List <string>();
            Mock <ApiController> controllerMock = new Mock <ApiController>()
            {
                CallBase = true
            };
            var controllerContextMock = new Mock <HttpControllerContext>();

            Mock <IActionValueBinder> binderMock        = new Mock <IActionValueBinder>();
            Mock <HttpActionBinding>  actionBindingMock = new Mock <HttpActionBinding>();

            actionBindingMock.Setup(b => b.ExecuteBindingAsync(It.IsAny <HttpActionContext>(), It.IsAny <CancellationToken>())).Returns(() => Task.Factory.StartNew(() => { log.Add("model binding"); }));
            binderMock.Setup(b => b.GetBinding(It.IsAny <HttpActionDescriptor>())).Returns(actionBindingMock.Object);
            HttpConfiguration configuration = new HttpConfiguration();

            HttpControllerContext controllerContext = controllerContextMock.Object;

            controllerContext.Configuration        = configuration;
            controllerContext.ControllerDescriptor = new HttpControllerDescriptor(configuration, "test", typeof(object));
            var actionFilterMock = CreateActionFilterMock((ac, ct, cont) =>
            {
                log.Add("action filters");
                return(cont());
            });
            var authFilterMock = CreateAuthorizationFilterMock((ac, ct, cont) =>
            {
                log.Add("auth filters");
                return(cont());
            });

            var selectorMock = new Mock <IHttpActionSelector>();

            Mock <HttpActionDescriptor> actionDescriptorMock = new Mock <HttpActionDescriptor>();

            actionDescriptorMock.Setup(ad => ad.ActionBinding).Returns(actionBindingMock.Object);
            actionDescriptorMock.Setup(ad => ad.GetFilterPipeline()).
            Returns(new Collection <FilterInfo>(new List <FilterInfo>()
            {
                new FilterInfo(actionFilterMock.Object, FilterScope.Action), new FilterInfo(authFilterMock.Object, FilterScope.Action)
            }));

            selectorMock.Setup(s => s.SelectAction(controllerContext)).Returns(actionDescriptorMock.Object);

            ApiController controller  = controllerMock.Object;
            var           invokerMock = new Mock <IHttpActionInvoker>();

            invokerMock.Setup(i => i.InvokeActionAsync(It.IsAny <HttpActionContext>(), It.IsAny <CancellationToken>()))
            .Returns(() => Task.Factory.StartNew(() =>
            {
                log.Add("action");
                return(new HttpResponseMessage());
            }));
            controllerContext.Configuration.Services.Replace(typeof(IHttpActionInvoker), invokerMock.Object);
            controllerContext.Configuration.Services.Replace(typeof(IHttpActionSelector), selectorMock.Object);
            controllerContext.Configuration.Services.Replace(typeof(IActionValueBinder), binderMock.Object);

            var task = controller.ExecuteAsync(controllerContext, CancellationToken.None);

            Assert.NotNull(task);
            task.WaitUntilCompleted();
            Assert.Equal(new string[] { "auth filters", "model binding", "action filters", "action" }, log.ToArray());
        }