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));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 5
0
        public void DoesNotThrowExceptionWhenContentTypeIsNull(string contentType)
        {
            var stream = new Mock <Stream>();

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

            ReadStreamStrategyFactory.Create(stream.Object, null, "text/plain");
        }
Ejemplo n.º 7
0
 public void ThrowsExceptionWhenStreamIsNull()
 {
     ReadStreamStrategyFactory.Create(null, Encoding.UTF8, "text/plain");
 }