Example #1
0
        public void ProcessRequest_Ignores_Position_Of_Replacement_Stream()
        {
            using (var hostStream = new MemoryStream()) {
                _Environment[EnvironmentKey.ResponseBody] = hostStream;

                var middleware = new MockMiddleware();
                middleware.Action = () => {
                    var pipelineStream = middleware.LastEnvironment[EnvironmentKey.ResponseBody] as Stream;
                    Assert.IsNotNull(pipelineStream);
                    Assert.AreNotSame(hostStream, pipelineStream);

                    pipelineStream.Write(new byte[] { 1, 2, 3 });
                    --pipelineStream.Position;
                };

                _StreamManipulatorBuilders.Add(middleware.AppFuncBuilder);
                _Pipeline.Construct(_BuilderEnvironment.Object);

                _Pipeline.ProcessRequest(_Environment).Wait();

                var hostStreamContent = hostStream.ToArray();
                Assert.AreEqual(3, hostStreamContent.Length);
                Assert.IsTrue(new byte[] { 1, 2, 3 }.SequenceEqual(hostStreamContent));
            }
        }
Example #2
0
        public void ProcessRequest_Deals_With_Exceptions_In_Accordance_With_Builder_Flags(bool exceptionLoggingRequested, bool exceptionSwallowingRequested, int expectedLoggerCallCount, bool expectedSeenException, int?expectedStatusCode)
        {
            _BuilderEnvironment.Object.PipelineLogsExceptions     = exceptionLoggingRequested;
            _BuilderEnvironment.Object.PipelineSwallowsExceptions = exceptionSwallowingRequested;
            var logger     = SetupExceptionLogger();
            var middleware = new MockMiddleware();
            var exception  = new InvalidOperationException();

            middleware.Action = () => throw exception;
            _MiddlewareBuilders.Add(middleware.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            var seenException = false;
            int?statusCode    = null;

            try {
                _Pipeline.ProcessRequest(_Environment).Wait();
            } catch (AggregateException ex) {
                Assert.AreEqual(1, ex.InnerExceptions.Count);
                seenException = Object.ReferenceEquals(ex.InnerException, exception);
            }
            if (_Environment.ContainsKey(EnvironmentKey.ResponseStatusCode))
            {
                statusCode = (int)_Environment[EnvironmentKey.ResponseStatusCode];
            }

            Assert.AreEqual(expectedLoggerCallCount, logger.CallCount);
            Assert.AreEqual(expectedSeenException, seenException);
            Assert.AreEqual(expectedStatusCode, statusCode);
        }
Example #3
0
        public void ProcessRequest_Replaces_Response_Stream_If_Stream_Manipulators_Are_Used()
        {
            using (var hostStream = new MemoryStream()) {
                _Environment[EnvironmentKey.ResponseBody] = hostStream;

                var middleware = new MockMiddleware();
                middleware.Action = () => {
                    var pipelineStream = middleware.LastEnvironment[EnvironmentKey.ResponseBody] as Stream;
                    Assert.IsNotNull(pipelineStream);
                    Assert.AreNotSame(hostStream, pipelineStream);

                    pipelineStream.WriteByte(99);
                };

                _StreamManipulatorBuilders.Add(middleware.AppFuncBuilder);
                _Pipeline.Construct(_BuilderEnvironment.Object);

                _Pipeline.ProcessRequest(_Environment).Wait();

                Assert.AreSame(hostStream, _Environment[EnvironmentKey.ResponseBody]);

                var hostStreamContent = hostStream.ToArray();
                Assert.AreEqual(1, hostStreamContent.Length);
                Assert.AreEqual(99, hostStreamContent[0]);
            }
        }
Example #4
0
        public void ProcessRequest_Log_Exceptions_Has_No_Effect_If_No_Logger_Registered()
        {
            _BuilderEnvironment.Object.PipelineLogsExceptions     = true;
            _BuilderEnvironment.Object.PipelineSwallowsExceptions = true;
            var middleware = new MockMiddleware();
            var exception  = new InvalidOperationException();

            middleware.Action = () => throw exception;
            _MiddlewareBuilders.Add(middleware.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            var seenException = false;
            int?statusCode    = null;

            try {
                _Pipeline.ProcessRequest(_Environment).Wait();
            } catch (AggregateException ex) {
                Assert.AreEqual(1, ex.InnerExceptions.Count);
                seenException = Object.ReferenceEquals(ex.InnerException, exception);
            }
            if (_Environment.ContainsKey(EnvironmentKey.ResponseStatusCode))
            {
                statusCode = (int)_Environment[EnvironmentKey.ResponseStatusCode];
            }

            Assert.AreEqual(true, seenException);
            Assert.AreEqual(null, statusCode);
        }
Example #5
0
        public void Construct_Builds_Middleware_AppFunc_And_ProcessRequest_Uses_It()
        {
            var middleware = new MockMiddleware();

            _MiddlewareBuilders.Add(middleware.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Pipeline.ProcessRequest(_Environment).Wait();
            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(1, middleware.AppFuncBuilderCallCount);
        }
Example #6
0
        public void ProcessRequest_Sets_Status_200_If_Pipeline_Finishes_Early_Without_Setting_Status_And_Without_Stream_Manipulators()
        {
            var middleware = new MockMiddleware {
                ChainToNextAppFunc = false
            };

            _MiddlewareBuilders.Add(middleware.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(200, (int)_Environment[EnvironmentKey.ResponseStatusCode]);
        }
Example #7
0
        public void ProcessRequest_Calls_Stream_Manipulators()
        {
            var middleware = new MockMiddleware();

            _StreamManipulatorBuilders.Add(middleware.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(1, middleware.Environments.Count);
            Assert.AreSame(_Environment, middleware.LastEnvironment);

            Assert.AreEqual(1, middleware.Nexts.Count);
            Assert.IsNotNull(middleware.LastNext);
        }
Example #8
0
        public void ProcessRequest_Does_Not_Replace_Stream_If_Host_Stream_Is_Null()
        {
            _Environment[EnvironmentKey.ResponseBody] = Stream.Null;

            var middleware = new MockMiddleware();

            middleware.Action = () => {
                var pipelineStream = middleware.LastEnvironment[EnvironmentKey.ResponseBody] as Stream;
                Assert.IsTrue(pipelineStream == Stream.Null);
            };

            _StreamManipulatorBuilders.Add(middleware.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Pipeline.ProcessRequest(_Environment).Wait();
        }
Example #9
0
        public void ProcessRequest_Middleware_Can_End_Processing_Early()
        {
            var middleware_1 = new MockMiddleware()
            {
                ChainToNextAppFunc = false,
            };
            var middleware_2 = new MockMiddleware();

            _MiddlewareBuilders.Add(middleware_1.AppFuncBuilder);
            _MiddlewareBuilders.Add(middleware_2.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(1, middleware_1.AppFuncCallCount);
            Assert.AreEqual(0, middleware_2.AppFuncCallCount);
        }
Example #10
0
        public void ProcessRequest_Does_Not_Replace_Stream_If_Stream_Manipulators_Are_Not_Used()
        {
            using (var hostStream = new MemoryStream()) {
                _Environment[EnvironmentKey.ResponseBody] = hostStream;

                var middleware = new MockMiddleware();
                middleware.Action = () => {
                    var pipelineStream = middleware.LastEnvironment[EnvironmentKey.ResponseBody] as Stream;
                    Assert.AreSame(hostStream, pipelineStream);
                };

                _MiddlewareBuilders.Add(middleware.AppFuncBuilder);
                _Pipeline.Construct(_BuilderEnvironment.Object);

                _Pipeline.ProcessRequest(_Environment).Wait();
            }
        }
Example #11
0
        public void ProcessRequest_Calls_Two_Middleware()
        {
            var middleware_1 = new MockMiddleware();
            var middleware_2 = new MockMiddleware();

            middleware_1.Action = () => Assert.AreEqual(0, middleware_2.AppFuncCallCount);
            middleware_2.Action = () => Assert.AreEqual(1, middleware_1.AppFuncCallCount);

            _MiddlewareBuilders.Add(middleware_1.AppFuncBuilder);
            _MiddlewareBuilders.Add(middleware_2.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(1, middleware_1.AppFuncCallCount);
            Assert.AreEqual(1, middleware_2.AppFuncCallCount);
        }
Example #12
0
        public void ProcessRequest_Calls_Stream_Manipulators_After_Middleware()
        {
            var middleware        = new MockMiddleware();
            var streamManipulator = new MockMiddleware();

            middleware.Action        = () => Assert.AreEqual(0, streamManipulator.AppFuncCallCount);
            streamManipulator.Action = () => Assert.AreEqual(1, middleware.AppFuncCallCount);

            _MiddlewareBuilders.Add(middleware.AppFuncBuilder);
            _StreamManipulatorBuilders.Add(streamManipulator.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(1, middleware.AppFuncCallCount);
            Assert.AreEqual(1, streamManipulator.AppFuncCallCount);
        }
Example #13
0
        public void ProcessRequest_Does_Not_Let_Stream_Manipulators_Break_The_Chain()
        {
            var middleware_1 = new MockMiddleware()
            {
                ChainToNextAppFunc = false,
            };
            var middleware_2 = new MockMiddleware();

            _StreamManipulatorBuilders.Add(middleware_1.AppFuncBuilder);
            _StreamManipulatorBuilders.Add(middleware_2.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(1, middleware_1.AppFuncCallCount);
            Assert.AreEqual(1, middleware_2.AppFuncCallCount);
        }
Example #14
0
        public void ProcessRequest_Middleware_Can_End_Chain_In_One_Request_And_Not_End_Chain_In_Another()
        {
            var middleware_1 = new MockMiddleware()
            {
                ChainToNextAppFunc = false,
            };
            var middleware_2 = new MockMiddleware();

            _MiddlewareBuilders.Add(middleware_1.AppFuncBuilder);
            _MiddlewareBuilders.Add(middleware_2.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);
            _Pipeline.ProcessRequest(_Environment).Wait();
            middleware_1.ChainToNextAppFunc = true;
            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(2, middleware_1.AppFuncCallCount);
            Assert.AreEqual(1, middleware_2.AppFuncCallCount);
        }
Example #15
0
        public void ProcessRequest_Calls_Stream_Manipulators_Even_If_Middleware_Breaks_Chain()
        {
            var middleware_1 = new MockMiddleware()
            {
                ChainToNextAppFunc = false,
            };
            var middleware_2      = new MockMiddleware();
            var streamManipulator = new MockMiddleware();

            _MiddlewareBuilders.Add(middleware_1.AppFuncBuilder);
            _MiddlewareBuilders.Add(middleware_2.AppFuncBuilder);
            _StreamManipulatorBuilders.Add(streamManipulator.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(1, middleware_1.AppFuncCallCount);
            Assert.AreEqual(0, middleware_2.AppFuncCallCount);
            Assert.AreEqual(1, streamManipulator.AppFuncCallCount);
        }
Example #16
0
        public void ProcessRequest_Exception_Logging_Tries_To_Pass_Request_Url_To_Logger()
        {
            var logger = SetupExceptionLogger();

            _BuilderEnvironment.Object.PipelineLogsExceptions     = true;
            _BuilderEnvironment.Object.PipelineSwallowsExceptions = true;
            var middleware = new MockMiddleware();
            var exception  = new InvalidOperationException();

            middleware.Action = () => throw exception;
            _MiddlewareBuilders.Add(middleware.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);

            _Environment[EnvironmentKey.RequestPathBase]    = "/root";
            _Environment[EnvironmentKey.RequestPath]        = "/path/to/file";
            _Environment[EnvironmentKey.RequestQueryString] = "a=1&b=c%20d";

            _Pipeline.ProcessRequest(_Environment).Wait();

            Assert.AreEqual(1, logger.RequestUrls.Count);
            Assert.AreEqual("/root/path/to/file?a=1&b=c%20d", logger.RequestUrls[0]);
        }
Example #17
0
        public void ProcessRequest_Adds_Custom_Key_With_Unique_ID_For_Request()
        {
            var middleware = new MockMiddleware();

            _MiddlewareBuilders.Add(middleware.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);
            _Pipeline.ProcessRequest(_Environment).Wait();

            var id1 = (long)_Environment[CustomEnvironmentKey.RequestID];

            TestCleanup();
            TestInitialise();

            middleware = new MockMiddleware();
            _MiddlewareBuilders.Add(middleware.AppFuncBuilder);
            _Pipeline.Construct(_BuilderEnvironment.Object);
            _Pipeline.ProcessRequest(_Environment).Wait();

            var id2 = (long)_Environment[CustomEnvironmentKey.RequestID];

            Assert.AreNotEqual(0L, id1);
            Assert.AreNotEqual(0L, id2);
            Assert.AreNotEqual(id1, id2);
        }
Example #18
0
        public void ProcessRequest_Overrides_Content_Length_Header_If_Using_Replacement_Stream()
        {
            using (var hostStream = new MemoryStream()) {
                _Environment[EnvironmentKey.ResponseBody] = hostStream;
                var responseHeaders = new Dictionary <string, string[]>(StringComparer.OrdinalIgnoreCase);
                _Environment[EnvironmentKey.ResponseHeaders] = responseHeaders;

                var middleware = new MockMiddleware();
                middleware.Action = () => {
                    var pipelineStream = middleware.LastEnvironment[EnvironmentKey.ResponseBody] as Stream;
                    pipelineStream.Write(new byte[] { 1, 2, 3 });
                    responseHeaders["Content-Length"] = new string[] { "20" };
                    --pipelineStream.Position;
                };

                _StreamManipulatorBuilders.Add(middleware.AppFuncBuilder);
                _Pipeline.Construct(_BuilderEnvironment.Object);

                _Pipeline.ProcessRequest(_Environment).Wait();

                Assert.AreEqual(1, responseHeaders["Content-Length"].Length);
                Assert.AreEqual("3", responseHeaders["Content-Length"][0]);
            }
        }