コード例 #1
0
        public async void Invoke_WithMatchingPath_FulfillsRequest()
        {
            // Arrange
            var elmStore    = new ElmStore();
            var factory     = new LoggerFactory();
            var optionsMock = new Mock <IOptions <ElmOptions> >();

            optionsMock
            .SetupGet(o => o.Value)
            .Returns(new ElmOptions());
            factory.AddProvider(new ElmLoggerProvider(elmStore, optionsMock.Object));

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

            var captureMiddleware = new ElmCaptureMiddleware(
                next,
                factory,
                optionsMock.Object);
            var pageMiddleware = new ElmPageMiddleware(
                next,
                optionsMock.Object,
                elmStore);
            var contextMock = GetMockContext("/Elm");

            using (var responseStream = new MemoryStream())
            {
                contextMock
                .SetupGet(c => c.Response.Body)
                .Returns(responseStream);
                contextMock
                .SetupGet(c => c.RequestServices)
                .Returns(() => null);

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

                await pageMiddleware.Invoke(contextMock.Object);

                string response = Encoding.UTF8.GetString(responseStream.ToArray());

                // Assert
                contextMock.VerifyGet(c => c.Request.Query, Times.AtLeastOnce());
                Assert.Contains("<title>ASP.NET Core Logs</title>", response);
            }
        }
コード例 #2
0
        public async void Invoke_BadRequestShowsError()
        {
            // Arrange
            var elmStore    = new ElmStore();
            var factory     = new LoggerFactory();
            var optionsMock = new Mock <IOptions <ElmOptions> >();

            optionsMock
            .SetupGet(o => o.Options)
            .Returns(new ElmOptions());
            factory.AddProvider(new ElmLoggerProvider(elmStore, optionsMock.Object.Options));

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

            var captureMiddleware = new ElmCaptureMiddleware(
                next,
                factory,
                optionsMock.Object);
            var pageMiddleware = new ElmPageMiddleware(
                next,
                optionsMock.Object,
                elmStore);
            var contextMock = GetMockContext("/Elm/666");

            using (var responseStream = new MemoryStream())
            {
                contextMock
                .SetupGet(c => c.Response.Body)
                .Returns(responseStream);

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

                await pageMiddleware.Invoke(contextMock.Object);

                string response = Encoding.UTF8.GetString(responseStream.ToArray());

                // Assert
                contextMock.VerifyGet(c => c.Request.Query, Times.AtLeastOnce());
                Assert.True(response.Contains("Invalid Id"));
            }
        }
コード例 #3
0
        public async void Invoke_WithNonMatchingPath_IgnoresRequest()
        {
            // Arrange
            var elmStore    = new ElmStore();
            var factory     = new LoggerFactory();
            var optionsMock = new Mock <IOptions <ElmOptions> >();

            optionsMock
            .SetupGet(o => o.Value)
            .Returns(new ElmOptions());
            factory.AddProvider(new ElmLoggerProvider(elmStore, optionsMock.Object));

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

            var captureMiddleware = new ElmCaptureMiddleware(
                next,
                factory,
                optionsMock.Object);
            var pageMiddleware = new ElmPageMiddleware(
                next,
                optionsMock.Object,
                elmStore);

            var contextMock = GetMockContext("/nonmatchingpath");

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

            await pageMiddleware.Invoke(contextMock.Object);

            // Assert
            // Request.Query is used by the ElmPageMiddleware to parse the query parameters
            contextMock.VerifyGet(c => c.Request.Query, Times.Never());
        }