コード例 #1
0
        public async Task ExecuteAsync_ChainsFiltersInOrderFollowedByInnerActionContinuation()
        {
            // Arrange
            HttpActionContext           actionContextInstance = ContextUtil.CreateActionContext();
            List <string>               log = new List <string>();
            Mock <IAuthorizationFilter> globalFilterMock = CreateAuthorizationFilterMock(
                (ctx, ct, continuation) =>
            {
                log.Add("globalFilter");
                return(continuation());
            }
                );
            Mock <IAuthorizationFilter> actionFilterMock = CreateAuthorizationFilterMock(
                (ctx, ct, continuation) =>
            {
                log.Add("actionFilter");
                return(continuation());
            }
                );
            Mock <IHttpActionResult> innerResultMock = new Mock <IHttpActionResult>();

            innerResultMock
            .Setup(r => r.ExecuteAsync(It.IsAny <CancellationToken>()))
            .Returns(
                () =>
            {
                log.Add("innerAction");
                return(Task.FromResult <HttpResponseMessage>(null));
            }
                );
            IHttpActionResult innerResult = innerResultMock.Object;
            var filters = new IAuthorizationFilter[]
            {
                globalFilterMock.Object,
                actionFilterMock.Object,
            };
            IHttpActionResult authorizationFilter = new AuthorizationFilterResult(
                actionContextInstance,
                filters,
                innerResult
                );

            // Act
            await authorizationFilter.ExecuteAsync(CancellationToken.None);

            // Assert
            Assert.Equal(new[] { "globalFilter", "actionFilter", "innerAction" }, log.ToArray());
            globalFilterMock.Verify();
            actionFilterMock.Verify();
        }
コード例 #2
0
        public void ExecuteAsync_ChainsFiltersInOrderFollowedByInnerActionContinuation()
        {
            // Arrange
            HttpActionContext actionContextInstance = ContextUtil.CreateActionContext();
            List<string> log = new List<string>();
            Mock<IAuthorizationFilter> globalFilterMock = CreateAuthorizationFilterMock((ctx, ct, continuation) =>
            {
                log.Add("globalFilter");
                return continuation();
            });
            Mock<IAuthorizationFilter> actionFilterMock = CreateAuthorizationFilterMock((ctx, ct, continuation) =>
            {
                log.Add("actionFilter");
                return continuation();
            });
            Mock<IHttpActionResult> innerResultMock = new Mock<IHttpActionResult>();
            innerResultMock.Setup(r => r.ExecuteAsync(It.IsAny<CancellationToken>())).Returns(() =>
            {
                log.Add("innerAction");
                return Task.FromResult<HttpResponseMessage>(null);
            });
            IHttpActionResult innerResult = innerResultMock.Object;
            var filters = new IAuthorizationFilter[] {
                globalFilterMock.Object,
                actionFilterMock.Object,
            };
            IHttpActionResult authorizationFilter = new AuthorizationFilterResult(actionContextInstance, filters,
                innerResult);

            // Act
            Task<HttpResponseMessage> result = authorizationFilter.ExecuteAsync(CancellationToken.None);

            // Assert
            Assert.NotNull(result);
            result.WaitUntilCompleted();
            Assert.Equal(new[] { "globalFilter", "actionFilter", "innerAction" }, log.ToArray());
            globalFilterMock.Verify();
            actionFilterMock.Verify();
        }