Beispiel #1
0
        public void ReturnsZero_WhenTryingToReadMoreThanStreamLength(int maxBytesPerRead)
        {
            var bytes         = Encoding.ASCII.GetBytes("This is the stream content.");
            var networkStream = new NetworkStreamEmulator(new MemoryStream(bytes))
            {
                MaxBytesPerRead = maxBytesPerRead
            };
            var fixLengthInputStream = new FixLengthInputStream(networkStream, bytes.Length);

            var buff = new byte[bytes.Length + 1];

            while (fixLengthInputStream.NbRemainingBytesToRead > 0)
            {
                fixLengthInputStream.Read(buff);
            }

            Assert.Equal(0, fixLengthInputStream.Read(buff));
            Assert.Equal(0, fixLengthInputStream.NbRemainingBytesToRead);

            Assert.Equal(0, fixLengthInputStream.Read(new byte[500], 0, 500));
            Assert.Equal(0, fixLengthInputStream.NbRemainingBytesToRead);

            Assert.Equal(0, fixLengthInputStream.Read(new byte[1000], 0, 1000));
            Assert.Equal(0, fixLengthInputStream.NbRemainingBytesToRead);
        }
Beispiel #2
0
        public void ReturnsZero_WhenCountIsZero()
        {
            var networkStream        = new NetworkStreamEmulator(new MemoryStream());
            var fixLengthInputStream = new FixLengthInputStream(networkStream, 5);

            const int count = 0;

            Assert.Equal(0, fixLengthInputStream.Read(new byte[50], 0, count));
        }
Beispiel #3
0
        public Stream Create(Stream inputStream, IHttpHeader header)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException(nameof(inputStream));
            }
            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }


            var streamTmp = inputStream;

            var contentLength = header.Headers.ContentLength.Value;

            if (contentLength != null)
            {
                if (contentLength <= 0)
                {
                    return(new NullInputStream());
                }

                streamTmp = new FixLengthInputStream(streamTmp, contentLength.Value);
            }

            foreach (var transferEncodingValue in header.Headers.TransferEncoding.Values)
            {
                var cleanedTransferEncoding = transferEncodingValue.ToLower().Trim();

                switch (cleanedTransferEncoding)
                {
                case "identity":
                    break;

                case "gzip":
                    streamTmp = new GZipStream(streamTmp, CompressionLevel.Fastest);
                    break;

                case "deflate":
                    streamTmp = new DeflateStream(streamTmp, CompressionLevel.Fastest);
                    break;

                case "chunked":
                    streamTmp = new ChunkedInputStream(streamTmp);
                    break;

                default:
                    throw new InputTransferEncodingNotSupportedException();
                }
            }

            return(streamTmp);
        }
Beispiel #4
0
        public void ThrowsStreamEndException(int nbMissingBytes, int maxBytesPerRead)
        {
            var bytes         = Encoding.ASCII.GetBytes("This is the stream content.");
            var networkStream = new NetworkStreamEmulator(new MemoryStream(bytes))
            {
                MaxBytesPerRead = maxBytesPerRead
            };
            var fixLengthInputStream = new FixLengthInputStream(networkStream, bytes.Length + nbMissingBytes);

            Assert.Throws <StreamEndException>(() =>
            {
                var buff = new byte[10];
                while (fixLengthInputStream.NbRemainingBytesToRead > 0)
                {
                    fixLengthInputStream.Read(buff);
                }
            });
        }