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")); }
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); }
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()); }
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); }
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); }
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()); }
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); }
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); }
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); }
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)); }
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); }
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); }
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 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()); }
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); }
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); }
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); }
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); }
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); }
public DefaultShellRouteBuilder(IServiceProvider serviceProvider) { Routes = new List<IRouter>(); DefaultHandler = new MvcRouteHandler(); ServiceProvider = serviceProvider; }