Exemple #1
0
        /// <summary>
        /// See base docs.
        /// </summary>
        /// <param name="environment"></param>
        private void ManipulateResponseStream(IDictionary <string, object> environment)
        {
            var context       = OwinContext.Create(environment);
            var isHtmlContent = context.ResponseHeadersDictionary.ContentTypeValue.MediaTypeParsed == MediaType.Html;

            if (isHtmlContent)
            {
                var stream = context.ResponseBody;
                stream.Position = 0;
                var textContent = TextContent.Load(stream, leaveOpen: true);

                foreach (var manipulator in _Config.GetTextResponseManipulators())
                {
                    manipulator.ManipulateTextResponse(environment, textContent);
                }

                if (textContent.IsDirty)
                {
                    stream.Position = 0;
                    stream.SetLength(0);
                    var bytes = textContent.GetBytes(includePreamble: true);
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
        }
        public void HtmlManipulatorConfiguration_AddTextResponseManipulator_Adds_Manipulator()
        {
            _Config.AddTextResponseManipulator(_Manipulator);

            var manipulators = _Config.GetTextResponseManipulators().ToArray();

            Assert.AreEqual(1, manipulators.Length);
            Assert.AreSame(_Manipulator, manipulators[0]);
        }
Exemple #3
0
        /// <summary>
        /// See base docs.
        /// </summary>
        /// <param name="environment"></param>
        public void ManipulateResponseStream(IDictionary <string, object> environment)
        {
            var context = PipelineContext.GetOrCreate(environment);

            if (context.Response.IsHtmlContentType)
            {
                var stream = context.Response.Body;
                stream.Position = 0;
                var textContent = TextContent.Load(stream, leaveOpen: true);

                foreach (var manipulator in _Config.GetTextResponseManipulators())
                {
                    manipulator.ManipulateTextResponse(environment, textContent);
                }

                if (textContent.IsDirty)
                {
                    stream.Position = 0;
                    stream.SetLength(0);
                    var bytes = textContent.GetBytes(includePreamble: true);
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
        }