Beispiel #1
0
        public async void Invoke_DoesNotLogWhenDisabledAndNotHandled()
        {
            // Arrange
            var isHandled = false;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RouterMiddleware>,
                TestSink.EnableWithTypeName <RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: false);

            var httpContext = new DefaultHttpContext();

            httpContext.ApplicationServices = new ServiceProvider();
            httpContext.RequestServices     = httpContext.ApplicationServices;

            RequestDelegate next = (c) =>
            {
                return(Task.FromResult <object>(null));
            };

            var router     = new TestRouter(isHandled);
            var middleware = new RouterMiddleware(next, httpContext.ApplicationServices, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

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

            Assert.Equal(typeof(RouterMiddleware).FullName, scope.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", scope.Scope);

            Assert.Empty(sink.Writes);
        }
        public async void Invoke__DoesNotInvokesNextIfNotRService()
        {
            // Arrange
            const bool isHandled  = true;
            const bool isRService = false;
            var        wasCalled  = false;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RServiceRouterMiddleware>,
                TestSink.EnableWithTypeName <RServiceRouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, true);

            var reqServices = new Mock <IServiceProvider>().SetupAllProperties().Object;
            var httpContext = new DefaultHttpContext {
                RequestServices = reqServices
            };

            RequestDelegate next = (c) =>
            {
                wasCalled = true;
                return(Task.FromResult <object>(null));
            };

            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            var router     = new TestRouter(isHandled, isRService);
            var middleware = new RServiceRouterMiddleware(next, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            Assert.False(wasCalled);
        }
        public async void Invoke_DoesNotLogWhenDisabledAndNotHandled()
        {
            // Arrange
            var isHandled = false;

            var sink = new TestSink(
                TestSink.EnableWithTypeName<RouterMiddleware>,
                TestSink.EnableWithTypeName<RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: false);

            var httpContext = new DefaultHttpContext();
            httpContext.ApplicationServices = new ServiceProvider();
            httpContext.RequestServices = httpContext.ApplicationServices;

            RequestDelegate next = (c) =>
            {
                return Task.FromResult<object>(null);
            };

            var router = new TestRouter(isHandled);
            var middleware = new RouterMiddleware(next, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            // Assert
            Assert.Single(sink.Scopes);
            var scope = sink.Scopes[0];
            Assert.Equal(typeof(RouterMiddleware).FullName, scope.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", scope.Scope);

            Assert.Empty(sink.Writes);
        }
Beispiel #4
0
        public async void Invoke_LogsCorrectValues_WhenNotHandled()
        {
            // Arrange
            var expectedMessage = "Request did not match any routes.";
            var isHandled = false;

            var sink = new TestSink(
                TestSink.EnableWithTypeName<RouterMiddleware>,
                TestSink.EnableWithTypeName<RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            var httpContext = new DefaultHttpContext();
            httpContext.RequestServices = new ServiceProvider();

            RequestDelegate next = (c) =>
            {
                return Task.FromResult<object>(null);
            };

            var router = new TestRouter(isHandled);
            var middleware = new RouterMiddleware(next, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            // Assert
            Assert.Empty(sink.Scopes);
            Assert.Single(sink.Writes);
            Assert.Equal(expectedMessage, sink.Writes[0].State?.ToString());
        }
Beispiel #5
0
        public async void Invoke_DoesNotLog_WhenHandled()
        {
            // Arrange
            var isHandled = true;

            var sink = new TestSink(
                TestSink.EnableWithTypeName<RouterMiddleware>,
                TestSink.EnableWithTypeName<RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            var httpContext = new DefaultHttpContext();
            httpContext.RequestServices = new ServiceProvider();

            RequestDelegate next = (c) =>
            {
                return Task.FromResult<object>(null);
            };

            var router = new TestRouter(isHandled);
            var middleware = new RouterMiddleware(next, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            // Assert
            Assert.Empty(sink.Scopes);
            Assert.Empty(sink.Writes);
        }
        public void ReportUnsynchronizableContent_SynchronizesIfSourceVersionsAreIdenticalButSyncVersionNewer()
        {
            // Arrange
            var router       = new TestRouter();
            var lastVersion  = VersionStamp.Default.GetNewerVersion();
            var lastDocument = TestDocumentSnapshot.Create("C:/path/old.cshtml", lastVersion);
            var document     = TestDocumentSnapshot.Create("C:/path/file.cshtml", lastVersion);
            var cache        = new TestDocumentVersionCache(new Dictionary <DocumentSnapshot, long>()
            {
                [document]     = 1338,
                [lastDocument] = 1337,
            });
            var csharpDocument = RazorCSharpDocument.Create("Anything", RazorCodeGenerationOptions.CreateDefault(), Enumerable.Empty <RazorDiagnostic>());

            // Force the state to already be up-to-date
            document.State.HostDocument.GeneratedCodeContainer.SetOutput(csharpDocument, lastDocument);

            var backgroundGenerator = new BackgroundDocumentGenerator(Dispatcher, cache, router, LoggerFactory);
            var work = new[] { new KeyValuePair <string, DocumentSnapshot>(document.FilePath, document) };

            // Act
            backgroundGenerator.ReportUnsynchronizableContent(work);

            // Assert
            var filePath = Assert.Single(router.SynchronizedDocuments);

            Assert.Equal(document.FilePath, filePath);
        }
Beispiel #7
0
        public async Task Invoke_LogsCorrectValues_WhenNotHandled()
        {
            // Arrange
            var expectedMessage = "Request did not match any routes";
            var isHandled       = false;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RouterMiddleware>,
                TestSink.EnableWithTypeName <RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            var httpContext = new DefaultHttpContext();

            httpContext.RequestServices = new ServiceProvider();

            RequestDelegate next = (c) =>
            {
                return(Task.FromResult <object>(null));
            };

            var router     = new TestRouter(isHandled);
            var middleware = new RouterMiddleware(next, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            // Assert
            Assert.Empty(sink.Scopes);
            var write = Assert.Single(sink.Writes);

            Assert.Equal(expectedMessage, write.State?.ToString());
        }
        public void ReportUnsynchronizableContent_DoesNothingForOlderDocuments()
        {
            // Arrange
            var router       = new TestRouter();
            var lastVersion  = VersionStamp.Default.GetNewerVersion();
            var lastDocument = TestDocumentSnapshot.Create("C:/path/old.cshtml", lastVersion);
            var oldDocument  = TestDocumentSnapshot.Create("C:/path/file.cshtml", VersionStamp.Default);
            var cache        = new TestDocumentVersionCache(new Dictionary <DocumentSnapshot, long>()
            {
                [oldDocument]  = 1337,
                [lastDocument] = 1338,
            });
            var csharpDocument = RazorCSharpDocument.Create("Anything", RazorCodeGenerationOptions.CreateDefault(), Enumerable.Empty <RazorDiagnostic>());

            // Force the state to already be up-to-date
            oldDocument.State.HostDocument.GeneratedCodeContainer.SetOutput(lastDocument, csharpDocument, lastVersion, VersionStamp.Default);

            var backgroundGenerator = new BackgroundDocumentGenerator(Dispatcher, cache, Listeners, router, LoggerFactory);
            var work = new[] { new KeyValuePair <string, DocumentSnapshot>(oldDocument.FilePath, oldDocument) };

            // Act
            backgroundGenerator.ReportUnsynchronizableContent(work);

            // Assert
            Assert.Empty(router.SynchronizedDocuments);
        }
Beispiel #9
0
        public async Task Invoke_DoesNotLog_WhenHandled()
        {
            // Arrange
            var isHandled = true;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RouterMiddleware>,
                TestSink.EnableWithTypeName <RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            var httpContext = new DefaultHttpContext();

            httpContext.RequestServices = new ServiceProvider();

            RequestDelegate next = (c) =>
            {
                return(Task.FromResult <object>(null));
            };

            var router     = new TestRouter(isHandled);
            var middleware = new RouterMiddleware(next, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            // Assert
            Assert.Empty(sink.Scopes);
            Assert.Empty(sink.Writes);
        }
        public async void Invoke_LogsCorrectValuesWhenNotHandled()
        {
            // Arrange
            const string expectedMessage = "Request did not match any routes.";
            const bool   isHandled       = false;
            const bool   isRService      = false;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RServiceRouterMiddleware>,
                TestSink.EnableWithTypeName <RServiceRouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, true);

            var reqServices = new Mock <IServiceProvider>().SetupAllProperties().Object;
            var httpContext = new DefaultHttpContext {
                RequestServices = reqServices
            };

            RequestDelegate next = (c) => Task.FromResult <object>(null);

            var router     = new TestRouter(isHandled, isRService);
            var middleware = new RServiceRouterMiddleware(next, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            // Assert
            Assert.Empty(sink.Scopes);
            Assert.Single(sink.Writes);
            Assert.Equal(expectedMessage, sink.Writes[0].State?.ToString());
        }
Beispiel #11
0
        public void DocumentProcessed_DoesNothingForOlderDocuments()
        {
            // Arrange
            var router       = new TestRouter();
            var lastVersion  = VersionStamp.Default.GetNewerVersion();
            var lastDocument = TestDocumentSnapshot.Create("C:/path/old.cshtml", lastVersion);
            var oldDocument  = TestDocumentSnapshot.Create("C:/path/file.cshtml", VersionStamp.Default);
            var cache        = new TestDocumentVersionCache(new Dictionary <DocumentSnapshot, long>()
            {
                [oldDocument]  = 1337,
                [lastDocument] = 1338,
            });
            var csharpDocument = RazorCSharpDocument.Create("Anything", RazorCodeGenerationOptions.CreateDefault(), Enumerable.Empty <RazorDiagnostic>());

            // Force the state to already be up-to-date
            oldDocument.State.HostDocument.GeneratedCodeContainer.SetOutput(lastDocument, csharpDocument, lastVersion, VersionStamp.Default);

            var listener = new UnsynchronizableContentDocumentProcessedListener(Dispatcher, cache, router);

            listener.Initialize(ProjectSnapshotManager);

            // Act
            listener.DocumentProcessed(oldDocument);

            // Assert
            Assert.Empty(router.SynchronizedDocuments);
        }
        public void ReportUnsynchronizableContent_DoesNothingForOldDocuments()
        {
            // Arrange
            var router = new TestRouter();
            var cache  = new TestDocumentVersionCache(new Dictionary <DocumentSnapshot, long>());
            var backgroundGenerator = new BackgroundDocumentGenerator(Dispatcher, cache, router, LoggerFactory);
            var document            = TestDocumentSnapshot.Create("C:/path/file.cshtml");
            var work = new[] { new KeyValuePair <string, DocumentSnapshot>(document.FilePath, document) };

            // Act
            backgroundGenerator.ReportUnsynchronizableContent(work);

            // Assert
            Assert.Empty(router.SynchronizedDocuments);
        }
Beispiel #13
0
        public void DocumentProcessed_DoesNothingForOldDocuments()
        {
            // Arrange
            var router   = new TestRouter();
            var cache    = new TestDocumentVersionCache(new Dictionary <DocumentSnapshot, long>());
            var listener = new UnsynchronizableContentDocumentProcessedListener(Dispatcher, cache, router);

            listener.Initialize(ProjectSnapshotManager);
            var document = TestDocumentSnapshot.Create("C:/path/file.cshtml");

            // Act
            listener.DocumentProcessed(document);

            // Assert
            Assert.Empty(router.SynchronizedDocuments);
        }
Beispiel #14
0
        public async void Invoke_LogsCorrectValuesWhenHandled()
        {
            // Arrange
            var isHandled = true;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RouterMiddleware>,
                TestSink.EnableWithTypeName <RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            var mockContext = new Mock <HttpContext>(MockBehavior.Strict);

            mockContext.Setup(m => m.RequestServices.GetService(typeof(ILoggerFactory)))
            .Returns(loggerFactory);

            RequestDelegate next = (c) =>
            {
                return(Task.FromResult <object>(null));
            };

            var router = new TestRouter(isHandled);
            var mockServiceProvider = new Mock <IServiceProvider>();
            var middleware          = new RouterMiddleware(next, mockServiceProvider.Object, router);

            // Act
            await middleware.Invoke(mockContext.Object);

            // Assert
            // exists a BeginScope, verify contents
            Assert.Single(sink.Scopes);
            var scope = sink.Scopes[0];

            Assert.Equal(typeof(RouterMiddleware).FullName, scope.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", scope.Scope);

            Assert.Single(sink.Writes);

            var write = sink.Writes[0];

            Assert.Equal(typeof(RouterMiddleware).FullName, write.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", write.Scope);
            Assert.Equal(typeof(RouterMiddlewareInvokeValues), write.State.GetType());
            var values = (RouterMiddlewareInvokeValues)write.State;

            Assert.Equal("RouterMiddleware.Invoke", values.Name);
            Assert.Equal(true, values.Handled);
        }
Beispiel #15
0
        public async void Invoke_LogsCorrectValuesWhenHandled()
        {
            // Arrange
            var isHandled = true;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RouterMiddleware>,
                TestSink.EnableWithTypeName <RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            var httpContext = new DefaultHttpContext();

            httpContext.ApplicationServices = new ServiceProvider();
            httpContext.RequestServices     = httpContext.ApplicationServices;

            RequestDelegate next = (c) =>
            {
                return(Task.FromResult <object>(null));
            };

            var router = new TestRouter(isHandled);

            var middleware = new RouterMiddleware(next, httpContext.ApplicationServices, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            // Assert
            // exists a BeginScope, verify contents
            Assert.Single(sink.Scopes);
            var scope = sink.Scopes[0];

            Assert.Equal(typeof(RouterMiddleware).FullName, scope.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", scope.Scope);

            Assert.Single(sink.Writes);

            var write = sink.Writes[0];

            Assert.Equal(typeof(RouterMiddleware).FullName, write.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", write.Scope);
            Assert.Equal(typeof(RouterMiddlewareInvokeValues), write.State.GetType());
            var values = (RouterMiddlewareInvokeValues)write.State;

            Assert.Equal("RouterMiddleware.Invoke", values.Name);
            Assert.Equal(true, values.Handled);
        }
Beispiel #16
0
 public void BasicRouting_ShouldWork()
 {
     Setup();
     try
     {
         var counter = 0;
         var x = new TestRouter();
         x.Route("counter", e => counter++);
         x.Initialise();
         _location.Replace("http://www.example.com#counter");
         _history.CheckUrl();
         Assert.AreEqual(counter, 1, "expect counter to have incremented");
     }
     finally
     {
         Teardown();
     }
 }
Beispiel #17
0
 public void BasicRouting_WithMultipleParameters_ShouldWork()
 {
     Setup();
     try
     {
         JsDictionary<string, string> routeParams = null;
         var x = new TestRouter();
         x.Route("search/:term/p:page", e => routeParams = e);
         x.Initialise();
         _location.Replace("http://www.example.com#search/hello/p2");
         _history.CheckUrl();
         Assert.AreEqual(routeParams["term"], "hello");
         Assert.AreEqual(routeParams["page"], "2");
     }
     finally
     {
         Teardown();
     }
 }
Beispiel #18
0
        public async void Invoke_DoesNotLogWhenDisabledAndNotHandled()
        {
            // Arrange
            var isHandled = false;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RouterMiddleware>,
                TestSink.EnableWithTypeName <RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: false);

            var mockContext = new Mock <HttpContext>(MockBehavior.Strict);

            mockContext.Setup(m => m.RequestServices.GetService(typeof(ILoggerFactory)))
            .Returns(loggerFactory);

            RequestDelegate next = (c) =>
            {
                return(Task.FromResult <object>(null));
            };

            var router = new TestRouter(isHandled);
            var mockServiceProvider = new Mock <IServiceProvider>();
            var middleware          = new RouterMiddleware(next, mockServiceProvider.Object, router);

            // Act
            await middleware.Invoke(mockContext.Object);

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

            Assert.Equal(typeof(RouterMiddleware).FullName, scope.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", scope.Scope);

            Assert.Empty(sink.Writes);
        }
Beispiel #19
0
 public void BasicRouting_WithMultipleParameters_ViaNavigate_ShouldWork()
 {
     Setup();
     try
     {
         JsDictionary<string, string> routeParams = null;
         var x = new TestRouter();
         x.Route("search/:term/p:page", e => routeParams = e);
         x.Initialise();
         _history.Navigate("search/hello/p2", new RouteNavigateOptions {TriggerCallback = true});
         Assert.AreEqual(routeParams["term"], "hello");
         Assert.AreEqual(routeParams["page"], "2");
     }
     finally
     {
         Teardown();
     }
 }
        public async void Invoke_LogsCorrectValuesWhenHandled()
        {
            // Arrange
            var isHandled = true;

            var sink = new TestSink(
                TestSink.EnableWithTypeName<RouterMiddleware>,
                TestSink.EnableWithTypeName<RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            var httpContext = new DefaultHttpContext();
            httpContext.ApplicationServices = new ServiceProvider();
            httpContext.RequestServices = httpContext.ApplicationServices;

            RequestDelegate next = (c) =>
            {
                return Task.FromResult<object>(null);
            };

            var router = new TestRouter(isHandled);

            var middleware = new RouterMiddleware(next, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            // Assert
            // exists a BeginScope, verify contents
            Assert.Single(sink.Scopes);
            var scope = sink.Scopes[0];
            Assert.Equal(typeof(RouterMiddleware).FullName, scope.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", scope.Scope);

            Assert.Single(sink.Writes);

            var write = sink.Writes[0];
            Assert.Equal(typeof(RouterMiddleware).FullName, write.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", write.Scope);
            Assert.Equal(typeof(RouterMiddlewareInvokeValues), write.State.GetType());
            var values = (RouterMiddlewareInvokeValues)write.State;
            Assert.Equal("RouterMiddleware.Invoke", values.Name);
            Assert.Equal(true, values.Handled);
        }