Exemplo n.º 1
0
        public void CreateRuntimeInfoModel_GetsTheVersionAndAllPackages()
        {
            // Arrage
            var libraries = new ILibraryInformation[] {
                new FakeLibraryInformation()
                {
                    Name = "LibInfo1", Path = "Path1"
                },
                new FakeLibraryInformation()
                {
                    Name = "LibInfo2", Path = "Path2"
                },
            };

            var libraryManagerMock = new Mock <ILibraryManager>(MockBehavior.Strict);

            libraryManagerMock.Setup(l => l.GetLibraries()).Returns(libraries);

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

            var middleware = new RuntimeInfoMiddleware(
                next,
                new RuntimeInfoPageOptions(),
                libraryManagerMock.Object);

            // Act
            var model = middleware.CreateRuntimeInfoModel();

            // Assert
            Assert.False(string.IsNullOrWhiteSpace(model.Version));
            Assert.Same(libraries, model.References);
        }
Exemplo n.º 2
0
        public async void Invoke_WithNonMatchingPath_IgnoresRequest()
        {
            // Arrange
            var libraryManagerMock     = new Mock <ILibraryManager>(MockBehavior.Strict);
            var runtimeEnvironmentMock = new Mock <IRuntimeEnvironment>(MockBehavior.Strict);

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

            var middleware = new RuntimeInfoMiddleware(
                next,
                new RuntimeInfoPageOptions(),
                libraryManagerMock.Object,
                runtimeEnvironmentMock.Object);

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

            contextMock
            .SetupGet(c => c.Request.Path)
            .Returns(new PathString("/nonmatchingpath"));

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

            // Assert
            contextMock.VerifyGet(c => c.Request.Path, Times.Once());
        }
Exemplo n.º 3
0
        public async void Invoke_WithMatchingPath_ReturnsInfoPage_UsingCustomHtmlEncoder()
        {
            // Arrange
            var libraryManagerMock = new Mock <ILibraryManager>(MockBehavior.Strict);

            libraryManagerMock.Setup(l => l.GetLibraries()).Returns(new ILibraryInformation[] {
                new FakeLibraryInformation()
                {
                    Name = "LibInfo1", Version = "1.0.0-beta1", Path = "Path1"
                },
            });

            var runtimeEnvironmentMock = new Mock <IRuntimeEnvironment>(MockBehavior.Strict);

            runtimeEnvironmentMock.Setup(r => r.OperatingSystem).Returns("Windows");
            runtimeEnvironmentMock.Setup(r => r.RuntimeArchitecture).Returns("x64");
            runtimeEnvironmentMock.Setup(r => r.RuntimeType).Returns("clr");
            runtimeEnvironmentMock.Setup(r => r.RuntimeVersion).Returns("1.0.0");

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

            var middleware = new RuntimeInfoMiddleware(
                next,
                new RuntimeInfoPageOptions(),
                libraryManagerMock.Object,
                runtimeEnvironmentMock.Object);

            var buffer = new byte[4096];

            using (var responseStream = new MemoryStream(buffer))
            {
                var contextMock = new Mock <HttpContext>(MockBehavior.Strict);
                contextMock
                .SetupGet(c => c.Request.Path)
                .Returns(new PathString("/runtimeinfo"));
                contextMock
                .SetupGet(c => c.Response.Body)
                .Returns(responseStream);
                contextMock
                .SetupGet(c => c.ApplicationServices)
                .Returns(new ServiceCollection().
                         AddInstance <IHtmlEncoder>(new CustomHtmlEncoder()).
                         BuildServiceProvider());

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

                // Assert
                string response = Encoding.UTF8.GetString(buffer);

                Assert.True(response.Contains("<td>[LibInfo1]</td>"));
                Assert.True(response.Contains("<td>[1.0.0-beta1]</td>"));
                Assert.True(response.Contains("<td>[Path1]</td>"));
            }
        }
Exemplo n.º 4
0
        public async void Invoke_WithMatchingPath_ReturnsInfoPage()
        {
            // Arrange
            var libraryManagerMock = new Mock <ILibraryManager>(MockBehavior.Strict);

            libraryManagerMock.Setup(l => l.GetLibraries()).Returns(new ILibraryInformation[] {
                new FakeLibraryInformation()
                {
                    Name = "LibInfo1", Version = "1.0.0-beta1", Path = "Path1"
                },
                new FakeLibraryInformation()
                {
                    Name = "LibInfo2", Version = "1.0.0-beta2", Path = "Path2"
                },
            });

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

            var middleware = new RuntimeInfoMiddleware(
                next,
                new RuntimeInfoPageOptions(),
                libraryManagerMock.Object);

            var buffer = new byte[4096];

            using (var responseStream = new MemoryStream(buffer))
            {
                var contextMock = new Mock <HttpContext>(MockBehavior.Strict);
                contextMock
                .SetupGet(c => c.Request.Path)
                .Returns(new PathString("/runtimeinfo"));
                contextMock
                .SetupGet(c => c.Response.Body)
                .Returns(responseStream);

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

                // Assert
                string response = Encoding.UTF8.GetString(buffer);

                Assert.True(response.Contains("<td>LibInfo1</td>"));
                Assert.True(response.Contains("<td>1.0.0-beta1</td>"));
                Assert.True(response.Contains("<td>Path1</td>"));
                Assert.True(response.Contains("<td>LibInfo2</td>"));
                Assert.True(response.Contains("<td>1.0.0-beta2</td>"));
                Assert.True(response.Contains("<td>Path2</td>"));
            }
        }
Exemplo n.º 5
0
        public void CreateRuntimeInfoModel_GetsTheVersionAndAllPackages()
        {
            // Arrage
            var libraries = new ILibraryInformation[] {
                new FakeLibraryInformation()
                {
                    Name = "LibInfo1", Path = "Path1"
                },
                new FakeLibraryInformation()
                {
                    Name = "LibInfo2", Path = "Path2"
                },
            };

            var libraryManagerMock = new Mock <ILibraryManager>(MockBehavior.Strict);

            libraryManagerMock.Setup(l => l.GetLibraries()).Returns(libraries);

            var runtimeEnvironmentMock = new Mock <IRuntimeEnvironment>(MockBehavior.Strict);

            runtimeEnvironmentMock.Setup(r => r.OperatingSystem).Returns("Windows");
            runtimeEnvironmentMock.Setup(r => r.RuntimeArchitecture).Returns("x64");
            runtimeEnvironmentMock.Setup(r => r.RuntimeType).Returns("clr");
            runtimeEnvironmentMock.Setup(r => r.RuntimeVersion).Returns("1.0.0");

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

            var middleware = new RuntimeInfoMiddleware(
                next,
                new RuntimeInfoPageOptions(),
                libraryManagerMock.Object,
                runtimeEnvironmentMock.Object);

            // Act
            var model = middleware.CreateRuntimeInfoModel();

            // Assert
            Assert.Equal("1.0.0", model.Version);
            Assert.Equal("Windows", model.OperatingSystem);
            Assert.Equal("clr", model.RuntimeType);
            Assert.Equal("x64", model.RuntimeArchitecture);
            Assert.Same(libraries, model.References);
        }
        public async void Invoke_WithMatchingPath_ReturnsInfoPage()
        {
            // Arrange
            RequestDelegate next = _ =>
            {
                return(Task.FromResult <object>(null));
            };
            var runtimeEnvironment = PlatformServices.Default.Runtime;

            var middleware = new RuntimeInfoMiddleware(
                next,
                Options.Create(new RuntimeInfoPageOptions()));

            var buffer = new byte[4096];

            using (var responseStream = new MemoryStream(buffer))
            {
                var contextMock = new Mock <HttpContext>(MockBehavior.Strict);
                contextMock
                .SetupGet(c => c.Request.Path)
                .Returns(new PathString("/runtimeinfo"));
                contextMock
                .SetupGet(c => c.Response.Body)
                .Returns(responseStream);
                contextMock
                .SetupGet(c => c.RequestServices)
                .Returns(() => null);

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

                // Assert
                var response = Encoding.UTF8.GetString(buffer);

                Assert.Contains($"<p>Runtime Version: {runtimeEnvironment.RuntimeVersion}</p>", response);
                Assert.Contains($"<p>Operating System: {runtimeEnvironment.OperatingSystem}</p>", response);
                Assert.Contains($"<p>Runtime Architecture: {runtimeEnvironment.RuntimeArchitecture}</p>", response);
                Assert.Contains($"<p>Runtime Type: {runtimeEnvironment.RuntimeType}</p>", response);
            }
        }
        public void CreateRuntimeInfoModel_GetsTheVersionAndAllPackages()
        {
            // Arrage
            var libraries = new [] {
                new Library("LibInfo1", string.Empty, "Path1", string.Empty, Enumerable.Empty<string>(), Enumerable.Empty<AssemblyName>()),
                new Library("LibInfo2", string.Empty, "Path2", string.Empty, Enumerable.Empty<string>(), Enumerable.Empty<AssemblyName>())
            };

            var libraryManagerMock = new Mock<ILibraryManager>(MockBehavior.Strict);
            libraryManagerMock.Setup(l => l.GetLibraries()).Returns(libraries);

            var runtimeEnvironmentMock = new Mock<IRuntimeEnvironment>(MockBehavior.Strict);
            runtimeEnvironmentMock.Setup(r => r.OperatingSystem).Returns("Windows");
            runtimeEnvironmentMock.Setup(r => r.RuntimeArchitecture).Returns("x64");
            runtimeEnvironmentMock.Setup(r => r.RuntimeType).Returns("clr");
            runtimeEnvironmentMock.Setup(r => r.RuntimeVersion).Returns("1.0.0");

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

            var middleware = new RuntimeInfoMiddleware(
                next,
                new RuntimeInfoPageOptions(),
                libraryManagerMock.Object,
                runtimeEnvironmentMock.Object);

            // Act
            var model = middleware.CreateRuntimeInfoModel();

            // Assert
            Assert.Equal("1.0.0", model.Version);
            Assert.Equal("Windows", model.OperatingSystem);
            Assert.Equal("clr", model.RuntimeType);
            Assert.Equal("x64", model.RuntimeArchitecture);
            Assert.Same(libraries, model.References);
        }
        public async void Invoke_WithNonMatchingPath_IgnoresRequest()
        {
            // Arrange
            var libraryManagerMock = new Mock<ILibraryManager>(MockBehavior.Strict);
            var runtimeEnvironmentMock = new Mock<IRuntimeEnvironment>(MockBehavior.Strict);

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

            var middleware = new RuntimeInfoMiddleware(
               next,
               new RuntimeInfoPageOptions(),
               libraryManagerMock.Object,
               runtimeEnvironmentMock.Object);

            var contextMock = new Mock<HttpContext>(MockBehavior.Strict);
            contextMock
                .SetupGet(c => c.Request.Path)
                .Returns(new PathString("/nonmatchingpath"));

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

            // Assert
            contextMock.VerifyGet(c => c.Request.Path, Times.Once());
        }
        public async void Invoke_WithMatchingPath_ReturnsInfoPage_UsingCustomHtmlEncoder()
        {
            // Arrange
            var libraryManagerMock = new Mock<ILibraryManager>(MockBehavior.Strict);
            libraryManagerMock.Setup(l => l.GetLibraries()).Returns(new [] {
                        new Library("LibInfo1", "1.0.0-beta1", "Path1", string.Empty, Enumerable.Empty<string>(), Enumerable.Empty<AssemblyName>()),
                    });

            var runtimeEnvironmentMock = new Mock<IRuntimeEnvironment>(MockBehavior.Strict);
            runtimeEnvironmentMock.Setup(r => r.OperatingSystem).Returns("Windows");
            runtimeEnvironmentMock.Setup(r => r.RuntimeArchitecture).Returns("x64");
            runtimeEnvironmentMock.Setup(r => r.RuntimeType).Returns("clr");
            runtimeEnvironmentMock.Setup(r => r.RuntimeVersion).Returns("1.0.0");

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

            var middleware = new RuntimeInfoMiddleware(
                next,
                new RuntimeInfoPageOptions(),
                libraryManagerMock.Object,
                runtimeEnvironmentMock.Object);

            var buffer = new byte[4096];
            using (var responseStream = new MemoryStream(buffer))
            {
                var contextMock = new Mock<HttpContext>(MockBehavior.Strict);
                contextMock
                    .SetupGet(c => c.Request.Path)
                    .Returns(new PathString("/runtimeinfo"));
                contextMock
                    .SetupGet(c => c.Response.Body)
                    .Returns(responseStream);
                contextMock
                    .SetupGet(c => c.ApplicationServices)
                    .Returns(new ServiceCollection().
                                AddInstance<IHtmlEncoder>(new CustomHtmlEncoder()).
                                BuildServiceProvider());

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

                // Assert
                string response = Encoding.UTF8.GetString(buffer);

                Assert.True(response.Contains("<td>[LibInfo1]</td>"));
                Assert.True(response.Contains("<td>[1.0.0-beta1]</td>"));
                Assert.True(response.Contains("<td>[Path1]</td>"));
            }
        }
        public async void Invoke_WithMatchingPath_ReturnsInfoPage()
        {
            // Arrange
            var libraryManagerMock = new Mock<ILibraryManager>(MockBehavior.Strict);
            libraryManagerMock.Setup(l => l.GetLibraries()).Returns(new ILibraryInformation[] {
                        new FakeLibraryInformation() { Name ="LibInfo1", Version = "1.0.0-beta1", Path = "Path1" },
                        new FakeLibraryInformation() { Name ="LibInfo2", Version = "1.0.0-beta2", Path = "Path2" },
                    });

            var runtimeEnvironmentMock = new Mock<IRuntimeEnvironment>(MockBehavior.Strict);
            runtimeEnvironmentMock.Setup(r => r.OperatingSystem).Returns("Windows");
            runtimeEnvironmentMock.Setup(r => r.RuntimeArchitecture).Returns("x64");
            runtimeEnvironmentMock.Setup(r => r.RuntimeType).Returns("clr");
            runtimeEnvironmentMock.Setup(r => r.RuntimeVersion).Returns("1.0.0");

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

            var middleware = new RuntimeInfoMiddleware(
                next,
                new RuntimeInfoPageOptions(),
                libraryManagerMock.Object,
                runtimeEnvironmentMock.Object);

            var buffer = new byte[4096];
            using (var responseStream = new MemoryStream(buffer))
            {
                var contextMock = new Mock<HttpContext>(MockBehavior.Strict);
                contextMock
                    .SetupGet(c => c.Request.Path)
                    .Returns(new PathString("/runtimeinfo"));
                contextMock
                    .SetupGet(c => c.Response.Body)
                    .Returns(responseStream);
                contextMock
                    .SetupGet(c => c.ApplicationServices)
                    .Returns(() => null);

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

                // Assert
                string response = Encoding.UTF8.GetString(buffer);

                Assert.Contains("<p>Runtime Version: 1.0.0</p>", response);
                Assert.Contains("<p>Operating System: Windows</p>", response);
                Assert.Contains("<p>Runtime Architecture: x64</p>", response);
                Assert.Contains("<p>Runtime Type: clr</p>", response);
                Assert.Contains("<td>LibInfo1</td>", response);
                Assert.Contains("<td>1.0.0-beta1</td>", response);
                Assert.Contains("<td>Path1</td>", response);
                Assert.Contains("<td>LibInfo2</td>", response);
                Assert.Contains("<td>1.0.0-beta2</td>", response);
                Assert.Contains("<td>Path2</td>", response);
            }
        }
        public async void Invoke_WithMatchingPath_ReturnsInfoPage()
        {
            // Arrange
            var libraryManagerMock = new Mock <ILibraryManager>(MockBehavior.Strict);

            libraryManagerMock.Setup(l => l.GetLibraries()).Returns(new [] {
                new Library("LibInfo1", "1.0.0-beta1", "Path1", string.Empty, Enumerable.Empty <string>(), Enumerable.Empty <AssemblyName>()),
                new Library("LibInfo2", "1.0.0-beta2", "Path2", string.Empty, Enumerable.Empty <string>(), Enumerable.Empty <AssemblyName>())
            });

            var runtimeEnvironmentMock = new Mock <IRuntimeEnvironment>(MockBehavior.Strict);

            runtimeEnvironmentMock.Setup(r => r.OperatingSystem).Returns("Windows");
            runtimeEnvironmentMock.Setup(r => r.RuntimeArchitecture).Returns("x64");
            runtimeEnvironmentMock.Setup(r => r.RuntimeType).Returns("clr");
            runtimeEnvironmentMock.Setup(r => r.RuntimeVersion).Returns("1.0.0");

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

            var middleware = new RuntimeInfoMiddleware(
                next,
                new RuntimeInfoPageOptions(),
                libraryManagerMock.Object,
                runtimeEnvironmentMock.Object);

            var buffer = new byte[4096];

            using (var responseStream = new MemoryStream(buffer))
            {
                var contextMock = new Mock <HttpContext>(MockBehavior.Strict);
                contextMock
                .SetupGet(c => c.Request.Path)
                .Returns(new PathString("/runtimeinfo"));
                contextMock
                .SetupGet(c => c.Response.Body)
                .Returns(responseStream);
                contextMock
                .SetupGet(c => c.ApplicationServices)
                .Returns(() => null);

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

                // Assert
                string response = Encoding.UTF8.GetString(buffer);

                Assert.Contains("<p>Runtime Version: 1.0.0</p>", response);
                Assert.Contains("<p>Operating System: Windows</p>", response);
                Assert.Contains("<p>Runtime Architecture: x64</p>", response);
                Assert.Contains("<p>Runtime Type: clr</p>", response);
                Assert.Contains("<td>LibInfo1</td>", response);
                Assert.Contains("<td>1.0.0-beta1</td>", response);
                Assert.Contains("<td>Path1</td>", response);
                Assert.Contains("<td>LibInfo2</td>", response);
                Assert.Contains("<td>1.0.0-beta2</td>", response);
                Assert.Contains("<td>Path2</td>", response);
            }
        }