public void Should_share_buffered_output()
        {
            var responseCapture1 = new ResponseCapture(_owinContext);
            var responseCapture2 = new ResponseCapture(_owinContext);

            const string testOutput = "This is a test";

            _owinContext.Response.Write(testOutput);

            Assert.AreEqual(0, _mockOwinContext.ResponseStream.Position, "Position of output stream");
            Assert.AreEqual(0, _mockOwinContext.ResponseStream.Length, "Length of output stream");

            var buffer1 = Encoding.UTF8.GetString(responseCapture1.OutputBuffer);
            var buffer2 = Encoding.UTF8.GetString(responseCapture2.OutputBuffer);

            Assert.AreEqual(testOutput, buffer1, "Buffered output from inner capture");
            Assert.AreEqual(testOutput, buffer2, "Buffered output from outer capture");

            responseCapture2.Send();

            // The inner capture should not send to the response stream
            Assert.AreEqual(0, _mockOwinContext.ResponseStream.Position, "Position of output stream");
            Assert.AreEqual(0, _mockOwinContext.ResponseStream.Length, "Length of output stream");

            responseCapture1.Send();

            // The outermost capture should send to the response stream
            Assert.AreEqual(testOutput.Length, _mockOwinContext.ResponseStream.Position, "Position of output stream");
            Assert.AreEqual(testOutput.Length, _mockOwinContext.ResponseStream.Length, "Length of output stream");

            var sentBytes   = _mockOwinContext.ResponseStream.ToArray();
            var sentMessage = Encoding.UTF8.GetString(sentBytes);

            Assert.AreEqual(testOutput, sentMessage);
        }
        public void Should_buffer_response()
        {
            var responseCapture = new ResponseCapture(_owinContext);

            const string testOutput = "This is a test";

            _owinContext.Response.Write(testOutput);

            Assert.AreEqual(0, _mockOwinContext.ResponseStream.Position, "Position of output stream");
            Assert.AreEqual(0, _mockOwinContext.ResponseStream.Length, "Length of output stream");

            var buffer = Encoding.UTF8.GetString(responseCapture.OutputBuffer);

            Assert.AreEqual(testOutput, buffer, "Buffered output");

            responseCapture.Send();

            Assert.AreEqual(testOutput.Length, _mockOwinContext.ResponseStream.Position, "Position of output stream");
            Assert.AreEqual(testOutput.Length, _mockOwinContext.ResponseStream.Length, "Length of output stream");

            var sentBytes   = _mockOwinContext.ResponseStream.ToArray();
            var sentMessage = Encoding.UTF8.GetString(sentBytes);

            Assert.AreEqual(testOutput, sentMessage);
        }
 public void SendCapturedOutput()
 {
     if (_responseCapture != null)
     {
         _responseCapture.Send();
     }
 }
        public void Should_make_output_buffer_available()
        {
            var responseCapture = new ResponseCapture(_owinContext);

            const string testOutput = "This is a test";

            _owinContext.Response.Write(testOutput);

            var responseRewriter = _owinContext.GetFeature <IResponseRewriter>();
            var buffer           = Encoding.UTF8.GetString(responseRewriter.OutputBuffer);

            Assert.AreEqual(testOutput, buffer, "Buffered output");

            responseCapture.Send();
        }
        public void Should_replace_shared_output_buffer()
        {
            var responseCapture1 = new ResponseCapture(_owinContext);
            var responseCapture2 = new ResponseCapture(_owinContext);
            var responseCapture3 = new ResponseCapture(_owinContext);

            const string originalMessage     = "This is a test";
            const string replacementMessage1 = "First replacement message";
            const string replacementMessage2 = "Second replacement message";
            const string replacementMessage3 = "Third replacement message";

            _owinContext.Response.Write(originalMessage);

            var buffer1 = Encoding.UTF8.GetString(responseCapture1.OutputBuffer);
            var buffer2 = Encoding.UTF8.GetString(responseCapture2.OutputBuffer);
            var buffer3 = Encoding.UTF8.GetString(responseCapture3.OutputBuffer);

            Assert.AreEqual(originalMessage, buffer1, "Buffered output from inner capture");
            Assert.AreEqual(originalMessage, buffer2, "Buffered output from middle capture");
            Assert.AreEqual(originalMessage, buffer3, "Buffered output from outer capture");

            responseCapture3.OutputBuffer = Encoding.UTF8.GetBytes(replacementMessage1);
            responseCapture3.Send();

            var buffer4 = Encoding.UTF8.GetString(responseCapture2.OutputBuffer);
            var buffer5 = Encoding.UTF8.GetString(responseCapture3.OutputBuffer);

            Assert.AreEqual(replacementMessage1, buffer4, "Buffered output from middle capture after 1 replacement");
            Assert.AreEqual(replacementMessage1, buffer5, "Buffered output from outer capture after 1 replacement");

            responseCapture2.OutputBuffer = Encoding.UTF8.GetBytes(replacementMessage2);
            responseCapture2.Send();

            var buffer6 = Encoding.UTF8.GetString(responseCapture3.OutputBuffer);

            Assert.AreEqual(replacementMessage2, buffer6, "Buffered output from middle capture after 2 replacements");

            responseCapture1.OutputBuffer = Encoding.UTF8.GetBytes(replacementMessage3);
            responseCapture1.Send();

            Assert.AreEqual(replacementMessage3.Length, _mockOwinContext.ResponseStream.Position, "Position of output stream");
            Assert.AreEqual(replacementMessage3.Length, _mockOwinContext.ResponseStream.Length, "Length of output stream");

            var sentMessage = Encoding.UTF8.GetString(_mockOwinContext.ResponseStream.ToArray());

            Assert.AreEqual(replacementMessage3, sentMessage);
        }
示例#6
0
            public void Send(bool copyToCache)
            {
                if (ResponseCapture == null)
                {
                    return;
                }

                if (copyToCache)
                {
                    // We need to copy the output buffer because other middleware upstream
                    // might make further modifications - compression, encryption etc
                    var buffer = ResponseCapture.OutputBuffer;
                    CachedContent = new byte[buffer.Length];
                    buffer.CopyTo(CachedContent, 0);
                }

                ResponseCapture.Send();
            }
        public void Should_append_to_replacement_buffer()
        {
            var responseCapture1 = new ResponseCapture(_owinContext);
            var responseCapture2 = new ResponseCapture(_owinContext);

            const string testOutput         = "This is a test";
            const string replacementMessage = "First replacement message";
            const string appendedText       = " with appendage";

            _owinContext.Response.Write(testOutput);
            responseCapture2.OutputBuffer = Encoding.UTF8.GetBytes(replacementMessage);
            _owinContext.Response.Write(appendedText);

            responseCapture2.Send();
            responseCapture1.Send();

            var sentBytes   = _mockOwinContext.ResponseStream.ToArray();
            var sentMessage = Encoding.UTF8.GetString(sentBytes);

            Assert.AreEqual(replacementMessage + appendedText, sentMessage, "Sent message");
        }
        public void Should_replace_output_buffer()
        {
            var responseCapture = new ResponseCapture(_owinContext);

            const string originalMessage    = "This is a test";
            const string replacementMessage = "A different output";

            _owinContext.Response.Write(originalMessage);

            var responseRewriter = _owinContext.GetFeature <IResponseRewriter>();

            responseRewriter.OutputBuffer = Encoding.UTF8.GetBytes(replacementMessage);

            responseCapture.Send();

            Assert.AreEqual(replacementMessage.Length, _mockOwinContext.ResponseStream.Position, "Position of output stream");
            Assert.AreEqual(replacementMessage.Length, _mockOwinContext.ResponseStream.Length, "Length of output stream");

            var sentBytes   = _mockOwinContext.ResponseStream.ToArray();
            var sentMessage = Encoding.UTF8.GetString(sentBytes);

            Assert.AreEqual(replacementMessage, sentMessage);
        }
示例#9
0
            public void Send(IOwinContext context)
            {
                if (_response == null)
                {
                    _traceFilter.Trace(context, TraceLevel.Debug, () => typeof(VersioningMiddleware).Name + " no captured response to send");
                    return;
                }

                var contentType = context.Response.ContentType;

                var mimeType = contentType;
                var encoding = Encoding.UTF8;

                if (!string.IsNullOrEmpty(contentType))
                {
                    foreach (var contentTypeHeader in contentType.Split(';').Select(h => h.Trim()).Where(h => h.Length > 0))
                    {
                        if (contentTypeHeader.Contains('='))
                        {
                            if (contentTypeHeader.StartsWith("charset="))
                            {
                                encoding = Encoding.GetEncoding(contentTypeHeader.Substring(8));
                            }
                        }
                        else
                        {
                            mimeType = contentTypeHeader;
                        }
                    }
                }

                _traceFilter.Trace(context, TraceLevel.Debug, () => typeof(VersioningMiddleware).Name + " mime type " + mimeType + " with " + encoding.EncodingName + " encoding");

                if (IsVersioned)
                {
                    if (_configuration.BrowserCacheTime.HasValue)
                    {
                        _traceFilter.Trace(context, TraceLevel.Information, () => typeof(VersioningMiddleware).Name + " adding headers to cache versioned asset for " + _configuration.BrowserCacheTime.Value);
                        context.Response.Expires = DateTime.UtcNow + _configuration.BrowserCacheTime.Value;
                        context.Response.Headers.Set(
                            "Cache-Control",
                            "public, max-age=" + (int)_configuration.BrowserCacheTime.Value.TotalSeconds);
                    }
                    else
                    {
                        _traceFilter.Trace(context, TraceLevel.Information, () => typeof(VersioningMiddleware).Name + " adding headers to disable browser caching");
                        context.Response.Headers.Set("Cache-Control", "no-cache");
                    }
                }
                else
                {
                    _traceFilter.Trace(context, TraceLevel.Debug, () => typeof(VersioningMiddleware).Name + " request is not for a versioned asset");
                }

                if (_configuration.MimeTypes != null &&
                    _configuration.MimeTypes.Length > 0 &&
                    _configuration.MimeTypes.Any(m => string.Equals(m, mimeType, StringComparison.OrdinalIgnoreCase)))
                {
                    var text = encoding.GetString(_response.OutputBuffer);

                    if (_configuration.Version.HasValue)
                    {
                        _traceFilter.Trace(context, TraceLevel.Debug, () => typeof(VersioningMiddleware).Name + " replacing version markers with " + _versionPrefix + _configuration.Version.Value);
                        text = text.Replace(_versionMarker, _versionPrefix + _configuration.Version.Value);
                    }
                    else
                    {
                        _traceFilter.Trace(context, TraceLevel.Information, () => typeof(VersioningMiddleware).Name + " removing version markers");
                        text = text.Replace(_versionMarker, string.Empty);
                    }

                    _response.OutputBuffer = encoding.GetBytes(text);
                    VersionUrlsReplaced    = true;
                }
                else
                {
                    _traceFilter.Trace(context, TraceLevel.Debug, () => typeof(VersioningMiddleware).Name + " is not configured to replace version numbers in " + mimeType + " responses");
                }

                _traceFilter.Trace(context, TraceLevel.Debug, () => typeof(VersioningMiddleware).Name + " sending buffered response to actual response stream");
                _response.Send();
            }