public static void NullInputsInConstructor_ExpectArgumentNullException(Stream stream, Encoding encoding, ArrayPool <byte> bytePool, ArrayPool <char> charPool)
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         var httpRequestStreamReader = new HttpRequestStreamReader(stream, encoding, 1, bytePool, charPool);
     });
 }
 public static void NegativeOrZeroBufferSize_ExpectArgumentOutOfRangeException(int size)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() =>
     {
         var httpRequestStreamReader = new HttpRequestStreamReader(new MemoryStream(), Encoding.UTF8, size, ArrayPool <byte> .Shared, ArrayPool <char> .Shared);
     });
 }
        public static async Task ReadLine_CarriageReturnAndLineFeedAcrossBufferBundaries(Func <HttpRequestStreamReader, Task <string> > action)
        {
            // Arrange
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);

            writer.Write("123456789\r\nfoo");
            writer.Flush();
            stream.Position = 0;

            var reader = new HttpRequestStreamReader(stream, Encoding.UTF8, 10);

            // Act & Assert
            var data = await action(reader);

            Assert.Equal("123456789", data);

            data = await action(reader);

            Assert.Equal("foo", data);

            var eof = await action(reader);

            Assert.Null(eof);
        }
        public static async Task StreamDisposed_ExpectObjectDisposedExceptionAsync(Func <HttpRequestStreamReader, Task> action)
        {
            var httpRequestStreamReader = new HttpRequestStreamReader(new MemoryStream(), Encoding.UTF8, 10, ArrayPool <byte> .Shared, ArrayPool <char> .Shared);

            httpRequestStreamReader.Dispose();

            await Assert.ThrowsAsync <ObjectDisposedException>(() => action(httpRequestStreamReader));
        }
        public static async Task ReadToEndAsync()
        {
            // Arrange
            var reader = new HttpRequestStreamReader(GetLargeStream(), Encoding.UTF8);

            var result = await reader.ReadToEndAsync();

            Assert.Equal(5000, result.Length);
        }
        public static void StreamCannotRead_ExpectArgumentException()
        {
            var mockStream = new Mock <Stream>();

            mockStream.Setup(m => m.CanRead).Returns(false);
            Assert.Throws <ArgumentException>(() =>
            {
                var httpRequestStreamReader = new HttpRequestStreamReader(mockStream.Object, Encoding.UTF8, 1, ArrayPool <byte> .Shared, ArrayPool <char> .Shared);
            });
        }
        public static void StreamDisposed_ExpectedObjectDisposedException(Action <HttpRequestStreamReader> action)
        {
            var httpRequestStreamReader = new HttpRequestStreamReader(new MemoryStream(), Encoding.UTF8, 10, ArrayPool <byte> .Shared, ArrayPool <char> .Shared);

            httpRequestStreamReader.Dispose();

            Assert.Throws <ObjectDisposedException>(() =>
            {
                action(httpRequestStreamReader);
            });
        }
        public static async Task ReadLine_EOF(Func <HttpRequestStreamReader, Task <string> > action)
        {
            // Arrange
            var stream = new MemoryStream();
            var reader = new HttpRequestStreamReader(stream, Encoding.UTF8);

            // Act & Assert
            var eof = await action(reader);

            Assert.Null(eof);
        }
        public static async Task StreamDisposed_ExpectObjectDisposedExceptionAsync()
        {
            var httpRequestStreamReader = new HttpRequestStreamReader(new MemoryStream(), Encoding.UTF8, 10, ArrayPool <byte> .Shared, ArrayPool <char> .Shared);

            httpRequestStreamReader.Dispose();

            await Assert.ThrowsAsync <ObjectDisposedException>(() =>
            {
                return(httpRequestStreamReader.ReadAsync(new char[10], 0, 1));
            });
        }
        public static void EmptyStream()
        {
            // Arrange
            var reader = new HttpRequestStreamReader(new MemoryStream(), Encoding.UTF8);
            var buffer = new char[10];

            // Act
            var read = reader.Read(buffer, 0, 1);

            // Assert
            Assert.Equal(0, read);
        }
        public static async Task ReadToEndAsync_Reads_Asynchronously()
        {
            // Arrange
            var    stream       = new AsyncOnlyStreamWrapper(GetLargeStream());
            var    reader       = new HttpRequestStreamReader(stream, Encoding.UTF8);
            var    streamReader = new StreamReader(GetLargeStream());
            string expected     = await streamReader.ReadToEndAsync();

            // Act
            var actual = await reader.ReadToEndAsync();

            // Assert
            Assert.Equal(expected, actual);
        }
        public static async Task ReadLine_NewLineOnly(Func <HttpRequestStreamReader, Task <string> > action)
        {
            // Arrange
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);

            writer.Write("\r\n");
            writer.Flush();
            stream.Position = 0;

            var reader = new HttpRequestStreamReader(stream, Encoding.UTF8);

            // Act & Assert
            var empty = await action(reader);

            Assert.Equal(string.Empty, empty);
        }
        public static async Task ReadLine_MultipleContinuousLines(Func <HttpRequestStreamReader, Task <string> > action)
        {
            // Arrange
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);

            writer.Write("\n\n\r\r\n\r");
            writer.Flush();
            stream.Position = 0;

            var reader = new HttpRequestStreamReader(stream, Encoding.UTF8);

            // Act & Assert
            for (var i = 0; i < 5; i++)
            {
                var data = await action(reader);

                Assert.Equal(string.Empty, data);
            }

            var eof = await action(reader);

            Assert.Null(eof);
        }
        public static async Task ReadLineAsync_MultipleContinuousLines()
        {
            // Arrange
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);

            writer.Write("\n\n\r\r\n");
            writer.Flush();
            stream.Position = 0;

            var reader = new HttpRequestStreamReader(stream, Encoding.UTF8);

            // Act & Assert
            for (var i = 0; i < 4; i++)
            {
                var data = await reader.ReadLineAsync();

                Assert.Equal(string.Empty, data);
            }

            var eol = await reader.ReadLineAsync();

            Assert.Null(eol);
        }