예제 #1
0
        public async Task RouteAsync_Notifies_ActionSelected()
        {
            // Arrange
            var listener = new TestNotificationListener();

            var context = CreateRouteContext(notificationListener: listener);

            context.RouteData.Values.Add("tag", "value");

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.NotNull(listener?.ActionSelected.ActionDescriptor);
            Assert.NotNull(listener?.ActionSelected.HttpContext);

            var routeValues = listener?.ActionSelected?.RouteData?.Values;

            Assert.NotNull(routeValues);

            Assert.Equal(1, routeValues.Count);
            Assert.Contains(routeValues, kvp => kvp.Key == "tag" && string.Equals(kvp.Value, "value"));
        }
예제 #2
0
        public async Task RouteAsync_Success_LogsCorrectValues()
        {
            // Arrange
            var sink          = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var context = CreateRouteContext(loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            var scope = Assert.Single(sink.Scopes);

            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            var write = Assert.Single(sink.Writes);

            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType <MvcRouteHandlerRouteAsyncValues>(write.State);

            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.True(values.ActionSelected);
            Assert.True(values.ActionInvoked);
            Assert.True(values.Handled);
        }
예제 #3
0
        public async Task RouteAsync_FailOnNoAction_LogsCorrectValues()
        {
            // Arrange
            var sink = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            var mockActionSelector = new Mock<IActionSelector>();
            mockActionSelector.Setup(a => a.SelectAsync(It.IsAny<RouteContext>()))
                .Returns(Task.FromResult<ActionDescriptor>(null));

            var context = CreateRouteContext(
                actionSelector: mockActionSelector.Object,
                loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();
            var expectedMessage = "No actions matched the current request.";

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.Empty(sink.Scopes);
            Assert.Single(sink.Writes);
            Assert.Equal(expectedMessage, sink.Writes[0].State?.ToString());
        }
예제 #4
0
        public async Task RouteAsync_FailOnNoAction_LogsCorrectValues()
        {
            // Arrange
            var sink = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var mockActionSelector = new Mock<IActionSelector>();
            mockActionSelector.Setup(a => a.SelectAsync(It.IsAny<RouteContext>()))
                .Returns(Task.FromResult<ActionDescriptor>(null));

            var context = CreateRouteContext(
                actionSelector: mockActionSelector.Object,
                loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            var scope = Assert.Single(sink.Scopes);
            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            var write = Assert.Single(sink.Writes);
            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType<MvcRouteHandlerRouteAsyncValues>(write.State);
            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.False(values.ActionSelected);
            Assert.False(values.ActionInvoked);
            Assert.False(values.Handled);
        }
예제 #5
0
        public async Task RouteAsync_Success_LogsCorrectValues()
        {
            // Arrange
            var sink = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var context = CreateRouteContext(loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            var scope = Assert.Single(sink.Scopes);
            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            var write = Assert.Single(sink.Writes);
            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType<MvcRouteHandlerRouteAsyncValues>(write.State);
            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.True(values.ActionSelected);
            Assert.True(values.ActionInvoked);
            Assert.True(values.Handled);
        }
예제 #6
0
        public async Task RouteAsync_FailOnNoAction_LogsCorrectValues()
        {
            // Arrange
            var sink          = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            var mockActionSelector = new Mock <IActionSelector>();

            mockActionSelector.Setup(a => a.SelectAsync(It.IsAny <RouteContext>()))
            .Returns(Task.FromResult <ActionDescriptor>(null));

            var context = CreateRouteContext(
                actionSelector: mockActionSelector.Object,
                loggerFactory: loggerFactory);

            var handler         = new MvcRouteHandler();
            var expectedMessage = "No actions matched the current request.";

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.Empty(sink.Scopes);
            Assert.Single(sink.Writes);
            Assert.Equal(expectedMessage, sink.Writes[0].State?.ToString());
        }
예제 #7
0
        public async void RouteAsync_FailOnNoInvoker_LogsCorrectValues()
        {
            // Arrange
            var sink          = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var mockInvokerFactory = new Mock <IActionInvokerFactory>();

            mockInvokerFactory.Setup(f => f.CreateInvoker(It.IsAny <ActionContext>()))
            .Returns <IActionInvoker>(null);

            var context = CreateRouteContext(
                invokerFactory: mockInvokerFactory.Object,
                loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await Assert.ThrowsAsync <InvalidOperationException>(async() =>
                                                                 await handler.RouteAsync(context));

            // Assert
            Assert.Equal(1, sink.Scopes.Count);
            var scope = sink.Scopes[0];

            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            // There is a record for IsEnabled and one for WriteCore.
            Assert.Equal(2, sink.Writes.Count);

            var enabled = sink.Writes[0];

            Assert.Equal(typeof(MvcRouteHandler).FullName, enabled.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", enabled.Scope);
            Assert.Null(enabled.State);

            var write = sink.Writes[1];

            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType <MvcRouteHandlerRouteAsyncValues>(write.State);

            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.Equal(true, values.ActionSelected);
            Assert.Equal(false, values.ActionInvoked);
            Assert.Equal(false, values.Handled);
        }
예제 #8
0
        public async Task RouteAsync_Notifies_ActionInvoked()
        {
            // Arrange
            var listener = new TestNotificationListener();

            var context = CreateRouteContext(notificationListener: listener);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.NotNull(listener.AfterAction?.ActionDescriptor);
            Assert.NotNull(listener.AfterAction?.HttpContext);
        }
예제 #9
0
        public async void RouteAsync_FailOnNoAction_LogsCorrectValues()
        {
            // Arrange
            var sink          = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var mockActionSelector = new Mock <IActionSelector>();

            mockActionSelector.Setup(a => a.SelectAsync(It.IsAny <RouteContext>()))
            .Returns(Task.FromResult <ActionDescriptor>(null));

            var context = CreateRouteContext(
                actionSelector: mockActionSelector.Object,
                loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.Equal(1, sink.Scopes.Count);
            var scope = sink.Scopes[0];

            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            // There is a record for IsEnabled and one for WriteCore.
            Assert.Equal(2, sink.Writes.Count);

            var enabled = sink.Writes[0];

            Assert.Equal(typeof(MvcRouteHandler).FullName, enabled.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", enabled.Scope);
            Assert.Null(enabled.State);

            var write = sink.Writes[1];

            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType <MvcRouteHandlerRouteAsyncValues>(write.State);

            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.Equal(false, values.ActionSelected);
            Assert.Equal(false, values.ActionInvoked);
            Assert.Equal(false, values.Handled);
        }
예제 #10
0
        public async Task RouteAsync_ResetsRouteDataOnException()
        {
            // Arrange
            RouteData actionRouteData = null;
            var       invoker         = new Mock <IActionInvoker>();

            invoker
            .Setup(i => i.InvokeAsync())
            .Throws(new Exception());

            var invokerFactory = new Mock <IActionInvokerFactory>();

            invokerFactory
            .Setup(f => f.CreateInvoker(It.IsAny <ActionContext>()))
            .Returns <ActionContext>((c) =>
            {
                actionRouteData = c.RouteData;
                c.RouteData.Values.Add("action", "Index");
                return(invoker.Object);
            });

            var context = CreateRouteContext(invokerFactory: invokerFactory.Object);
            var handler = new MvcRouteHandler();

            var initialRouter = Mock.Of <IRouter>();

            var originalRouteData = context.RouteData;

            originalRouteData.Routers.Add(initialRouter);

            // Act
            await Assert.ThrowsAsync <Exception>(() => handler.RouteAsync(context));

            // Assert
            Assert.Same(originalRouteData, context.RouteData);
            Assert.NotSame(originalRouteData, actionRouteData);
            Assert.NotSame(actionRouteData, context.RouteData);

            // The new routedata is a copy
            Assert.Null(context.RouteData.Values["action"]);
            Assert.Equal("Index", actionRouteData.Values["action"]);

            Assert.Equal(initialRouter, Assert.Single(actionRouteData.Routers));
        }
예제 #11
0
        public async void RouteAsync_FailOnNoAction_LogsCorrectValues()
        {
            // Arrange
            var sink = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var mockActionSelector = new Mock<IActionSelector>();
            mockActionSelector.Setup(a => a.SelectAsync(It.IsAny<RouteContext>()))
                .Returns(Task.FromResult<ActionDescriptor>(null));

            var context = CreateRouteContext(
                actionSelector: mockActionSelector.Object,
                loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.Equal(1, sink.Scopes.Count);
            var scope = sink.Scopes[0];
            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            // There is a record for IsEnabled and one for WriteCore.
            Assert.Equal(2, sink.Writes.Count);

            var enabled = sink.Writes[0];
            Assert.Equal(typeof(MvcRouteHandler).FullName, enabled.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", enabled.Scope);
            Assert.Null(enabled.State);

            var write = sink.Writes[1];
            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType<MvcRouteHandlerRouteAsyncValues>(write.State);
            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.Equal(false, values.ActionSelected);
            Assert.Equal(false, values.ActionInvoked);
            Assert.Equal(false, values.Handled);
        }
예제 #12
0
        public async void RouteAsync_Success_LogsCorrectValues()
        {
            // Arrange
            var sink          = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var context = CreateRouteContext(loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.Equal(1, sink.Scopes.Count);
            var scope = sink.Scopes[0];

            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            // There is a record for IsEnabled and one for WriteCore.
            Assert.Equal(2, sink.Writes.Count);

            var enabled = sink.Writes[0];

            Assert.Equal(typeof(MvcRouteHandler).FullName, enabled.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", enabled.Scope);
            Assert.Null(enabled.State);

            var write = sink.Writes[1];

            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType <MvcRouteHandlerRouteAsyncValues>(write.State);

            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.Equal(true, values.ActionSelected);
            Assert.Equal(true, values.ActionInvoked);
            Assert.Equal(true, values.Handled);
        }
예제 #13
0
        public async Task RouteAsync_CreatesNewRouteData()
        {
            // Arrange
            RouteData actionRouteData = null;
            var invoker = new Mock<IActionInvoker>();
            invoker
                .Setup(i => i.InvokeAsync())
                .Returns(Task.FromResult(true));

            var invokerFactory = new Mock<IActionInvokerFactory>();
            invokerFactory
                .Setup(f => f.CreateInvoker(It.IsAny<ActionContext>()))
                .Returns<ActionContext>((c) =>
                {
                    actionRouteData = c.RouteData;
                    return invoker.Object;
                });

            var initialRouter = Mock.Of<IRouter>();

            var context = CreateRouteContext(invokerFactory: invokerFactory.Object);
            var handler = new MvcRouteHandler();

            var originalRouteData = context.RouteData;
            originalRouteData.Routers.Add(initialRouter);
            originalRouteData.Values.Add("action", "Index");

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.NotSame(originalRouteData, context.RouteData);
            Assert.NotSame(originalRouteData, actionRouteData);
            Assert.Same(actionRouteData, context.RouteData);

            // The new routedata is a copy
            Assert.Equal("Index", context.RouteData.Values["action"]);

            Assert.Equal(initialRouter, Assert.Single(context.RouteData.Routers));
        }
예제 #14
0
        public async Task RouteAsync_Success_LogsCorrectValues()
        {
            // Arrange
            var sink = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);
            var displayName = "A.B.C";
            var actionDescriptor = new Mock<ActionDescriptor>();
            actionDescriptor.SetupGet(ad => ad.DisplayName)
                            .Returns(displayName);
            var context = CreateRouteContext(actionDescriptor: actionDescriptor.Object, loggerFactory: loggerFactory);
            var handler = new MvcRouteHandler();
            var expectedMessage = $"Executing action {displayName}";

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.Single(sink.Scopes);
            Assert.StartsWith("ActionId: ", sink.Scopes[0].Scope?.ToString());
            Assert.Single(sink.Writes);
            Assert.Equal(expectedMessage, sink.Writes[0].State?.ToString());
        }
예제 #15
0
        public async Task RouteAsync_Success_LogsCorrectValues()
        {
            // Arrange
            var sink = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);
            var displayName = "A.B.C";
            var actionDescriptor = new Mock<ActionDescriptor>();
            actionDescriptor.SetupGet(ad => ad.DisplayName)
                            .Returns(displayName);
            var context = CreateRouteContext(actionDescriptor: actionDescriptor.Object, loggerFactory: loggerFactory);
            var handler = new MvcRouteHandler();
            var expectedMessage = $"Executing action {displayName}";

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.Single(sink.Scopes);
            Assert.StartsWith("ActionId: ", sink.Scopes[0].Scope?.ToString());
            Assert.Single(sink.Writes);
            Assert.Equal(expectedMessage, sink.Writes[0].State?.ToString());
        }
예제 #16
0
        public async Task RouteAsync_FailOnNoAction_LogsCorrectValues()
        {
            // Arrange
            var sink          = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var mockActionSelector = new Mock <IActionSelector>();

            mockActionSelector.Setup(a => a.SelectAsync(It.IsAny <RouteContext>()))
            .Returns(Task.FromResult <ActionDescriptor>(null));

            var context = CreateRouteContext(
                actionSelector: mockActionSelector.Object,
                loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            var scope = Assert.Single(sink.Scopes);

            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            var write = Assert.Single(sink.Writes);

            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType <MvcRouteHandlerRouteAsyncValues>(write.State);

            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.False(values.ActionSelected);
            Assert.False(values.ActionInvoked);
            Assert.False(values.Handled);
        }
예제 #17
0
        public async void RouteAsync_Success_LogsCorrectValues()
        {
            // Arrange
            var sink = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var context = CreateRouteContext(loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.Equal(1, sink.Scopes.Count);
            var scope = sink.Scopes[0];
            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            // There is a record for IsEnabled and one for WriteCore.
            Assert.Equal(2, sink.Writes.Count);

            var enabled = sink.Writes[0];
            Assert.Equal(typeof(MvcRouteHandler).FullName, enabled.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", enabled.Scope);
            Assert.Null(enabled.State);

            var write = sink.Writes[1];
            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType<MvcRouteHandlerRouteAsyncValues>(write.State);
            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.Equal(true, values.ActionSelected);
            Assert.Equal(true, values.ActionInvoked);
            Assert.Equal(true, values.Handled);
        }
예제 #18
0
        public async Task RouteAsync_SetsMaxErrorCountOnModelStateDictionary()
        {
            // Arrange
            var expected        = 199;
            var optionsAccessor = new Mock <IOptions <MvcOptions> >();
            var options         = new MvcOptions
            {
                MaxModelValidationErrors = expected
            };

            optionsAccessor.SetupGet(o => o.Options)
            .Returns(options);

            var invoked            = false;
            var mockInvokerFactory = new Mock <IActionInvokerFactory>();

            mockInvokerFactory.Setup(f => f.CreateInvoker(It.IsAny <ActionContext>()))
            .Callback <ActionContext>(c =>
            {
                Assert.Equal(expected, c.ModelState.MaxAllowedErrors);
                invoked = true;
            })
            .Returns(Mock.Of <IActionInvoker>());

            var context = CreateRouteContext(
                invokerFactory: mockInvokerFactory.Object,
                optionsAccessor: optionsAccessor.Object);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.True(invoked);
        }
예제 #19
0
        public async Task RouteAsync_SetsMaxErrorCountOnModelStateDictionary()
        {
            // Arrange
            var expected = 199;
            var optionsAccessor = new Mock<IOptions<MvcOptions>>();
            var options = new MvcOptions
            {
                MaxModelValidationErrors = expected
            };
            optionsAccessor.SetupGet(o => o.Options)
                           .Returns(options);

            var invoked = false;
            var mockInvokerFactory = new Mock<IActionInvokerFactory>();
            mockInvokerFactory.Setup(f => f.CreateInvoker(It.IsAny<ActionContext>()))
                              .Callback<ActionContext>(c =>
                              {
                                  Assert.Equal(expected, c.ModelState.MaxAllowedErrors);
                                  invoked = true;
                              })
                              .Returns(Mock.Of<IActionInvoker>());

            var context = CreateRouteContext(
                invokerFactory: mockInvokerFactory.Object,
                optionsAccessor: optionsAccessor.Object);

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.True(invoked);
        }
예제 #20
0
        public async void RouteAsync_FailOnNoInvoker_LogsCorrectValues()
        {
            // Arrange
            var sink = new TestSink();
            var loggerFactory = new TestLoggerFactory(sink);

            var mockInvokerFactory = new Mock<IActionInvokerFactory>();
            mockInvokerFactory.Setup(f => f.CreateInvoker(It.IsAny<ActionContext>()))
                .Returns<IActionInvoker>(null);

            var context = CreateRouteContext(
                invokerFactory: mockInvokerFactory.Object,
                loggerFactory: loggerFactory);

            var handler = new MvcRouteHandler();

            // Act
            await Assert.ThrowsAsync<InvalidOperationException>(async () =>
                await handler.RouteAsync(context));

            // Assert
            Assert.Equal(1, sink.Scopes.Count);
            var scope = sink.Scopes[0];
            Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope);

            // There is a record for IsEnabled and one for WriteCore.
            Assert.Equal(2, sink.Writes.Count);

            var enabled = sink.Writes[0];
            Assert.Equal(typeof(MvcRouteHandler).FullName, enabled.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", enabled.Scope);
            Assert.Null(enabled.State);

            var write = sink.Writes[1];
            Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName);
            Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope);
            var values = Assert.IsType<MvcRouteHandlerRouteAsyncValues>(write.State);
            Assert.Equal("MvcRouteHandler.RouteAsync", values.Name);
            Assert.Equal(true, values.ActionSelected);
            Assert.Equal(false, values.ActionInvoked);
            Assert.Equal(false, values.Handled);
        }
예제 #21
0
        public async Task RouteAsync_CreatesNewRouteData()
        {
            // Arrange
            RouteData actionRouteData = null;
            var invoker = new Mock<IActionInvoker>();
            invoker
                .Setup(i => i.InvokeAsync())
                .Returns(Task.FromResult(true));

            var invokerFactory = new Mock<IActionInvokerFactory>();
            invokerFactory
                .Setup(f => f.CreateInvoker(It.IsAny<ActionContext>()))
                .Returns<ActionContext>((c) =>
                {
                    actionRouteData = c.RouteData;
                    return invoker.Object;
                });

            var initialRouter = Mock.Of<IRouter>();

            var context = CreateRouteContext(invokerFactory: invokerFactory.Object);
            var handler = new MvcRouteHandler();

            var originalRouteData = context.RouteData;
            originalRouteData.Routers.Add(initialRouter);
            originalRouteData.Values.Add("action", "Index");

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.NotSame(originalRouteData, context.RouteData);
            Assert.NotSame(originalRouteData, actionRouteData);
            Assert.Same(actionRouteData, context.RouteData);

            // The new routedata is a copy
            Assert.Equal("Index", context.RouteData.Values["action"]);

            Assert.Equal(initialRouter, Assert.Single(context.RouteData.Routers));
        }
 public DefaultShellRouteBuilder(IServiceProvider serviceProvider)
 {
     Routes = new List<IRouter>();
     DefaultHandler = new MvcRouteHandler();
     ServiceProvider = serviceProvider;
 }
예제 #23
0
        public async Task RouteAsync_Notifies_ActionSelected()
        {
            // Arrange
            var listener = new TestNotificationListener();

            var context = CreateRouteContext(notificationListener: listener);
            context.RouteData.Values.Add("tag", "value");

            var handler = new MvcRouteHandler();

            // Act
            await handler.RouteAsync(context);

            // Assert
            Assert.NotNull(listener?.ActionSelected.ActionDescriptor);
            Assert.NotNull(listener?.ActionSelected.HttpContext);

            var routeValues = listener?.ActionSelected?.RouteData?.Values;
            Assert.NotNull(routeValues);

            Assert.Equal(1, routeValues.Count);
            Assert.Contains(routeValues, kvp => kvp.Key == "tag" && string.Equals(kvp.Value, "value"));
        }