public void Will_raise_event()
        {
            //Arrange
            bool wasCalled = false;

            //Act
            using (var stream = new EventRaisingStream())
            using (var writer = new StreamWriter(stream))
            {
                stream.BeforeClose += (sender, args) => wasCalled = true;
                writer.Write("this is a test");
            }

            //Assert
            Assert.That(wasCalled);
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var context = filterContext.HttpContext;
            var originalHttpStream = context.Response.Filter;
            var replacementStream = new EventRaisingStream();

            closeHandler = (sender, args) =>
                           {
                               replacementStream.BeforeClose -= closeHandler;

                               context.Response.Filter = originalHttpStream; // put back original stream
                               transport.Send(replacementStream, context, this);
                               //originalHttpStream.Close();
                           };

            replacementStream.BeforeClose += closeHandler;

            // replace with our new stream to intercept output
            context.Response.Filter = replacementStream;
        }