public static ILogResponseBodyStrategy Create(Stream stream, Encoding encoding, Logger logger)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (!stream.CanRead)
            {
                return(new NullLogResponseBody());
            }

            if (stream.Length > Constants.MaximumAllowedFileSizeInBytes)
            {
                return(new LogResponseBodySizeTooLargeException(logger, stream.Length, Constants.MaximumAllowedFileSizeInBytes));
            }

            var    headers          = logger.DataContainer.HttpProperties.Response?.Properties?.Headers;
            string contentType      = headers?.FirstOrDefault(p => string.Compare(p.Key, "Content-Type", StringComparison.OrdinalIgnoreCase) == 0).Value;
            string responseFileName = InternalHelpers.GenerateResponseFileName(headers);

            IReadStreamStrategy strategy = ReadStreamStrategyFactory.Create(stream, encoding, contentType);

            return(new LogResponseBody(logger, responseFileName, strategy));
        }
예제 #2
0
        public void ReturnsNullReadStreamWhenStreamIsDisposed()
        {
            var stream = new Mock <Stream>();

            stream.Setup(p => p.CanRead).Returns(false);

            IReadStreamStrategy strategy = ReadStreamStrategyFactory.Create(stream.Object, Encoding.UTF8, "text/plain");

            Assert.IsInstanceOfType(strategy, typeof(NullReadStream));
        }
예제 #3
0
        public void ReturnsReadStreamAsStringForValidContentTypes(string contentType)
        {
            var stream = new Mock <Stream>();

            stream.Setup(p => p.CanRead).Returns(true);
            stream.Setup(p => p.Length).Returns(Constants.ReadStreamAsStringMaxContentLengthInBytes);

            IReadStreamStrategy strategy = ReadStreamStrategyFactory.Create(stream.Object, Encoding.UTF8, contentType);

            Assert.IsInstanceOfType(strategy, typeof(ReadStreamAsString));
        }
예제 #4
0
        public void ReturnsReadStreamToTemporaryFileWhenContentSizeIsGtThanMaximumContentLength()
        {
            var stream = new Mock <Stream>();

            stream.Setup(p => p.CanRead).Returns(true);
            stream.Setup(p => p.Length).Returns(Constants.ReadStreamAsStringMaxContentLengthInBytes + 1);

            IReadStreamStrategy strategy = ReadStreamStrategyFactory.Create(stream.Object, Encoding.UTF8, "text/plain");

            Assert.IsInstanceOfType(strategy, typeof(ReadStreamToTemporaryFile));
        }
예제 #5
0
        public void DoesNotThrowExceptionWhenContentTypeIsNull(string contentType)
        {
            var stream = new Mock <Stream>();

            ReadStreamStrategyFactory.Create(stream.Object, Encoding.UTF8, contentType);
        }
예제 #6
0
        public void ThrowsExceptionWhenEncodingIsNull()
        {
            var stream = new Mock <Stream>();

            ReadStreamStrategyFactory.Create(stream.Object, null, "text/plain");
        }
예제 #7
0
 public void ThrowsExceptionWhenStreamIsNull()
 {
     ReadStreamStrategyFactory.Create(null, Encoding.UTF8, "text/plain");
 }