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)); }
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)); }
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)); }
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)); }
public void DoesNotThrowExceptionWhenContentTypeIsNull(string contentType) { var stream = new Mock <Stream>(); ReadStreamStrategyFactory.Create(stream.Object, Encoding.UTF8, contentType); }
public void ThrowsExceptionWhenEncodingIsNull() { var stream = new Mock <Stream>(); ReadStreamStrategyFactory.Create(stream.Object, null, "text/plain"); }
public void ThrowsExceptionWhenStreamIsNull() { ReadStreamStrategyFactory.Create(null, Encoding.UTF8, "text/plain"); }