Example #1
0
        public async Task CanReadFromChunkedEncoding()
        {
            using (var input = new TestInput())
            {
                var body = MessageBody.For(HttpVersion.Http11, new FrameRequestHeaders {
                    HeaderTransferEncoding = "chunked"
                }, input.Frame);
                var mockBodyControl = new Mock <IHttpBodyControlFeature>();
                mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(true);
                var stream = new FrameRequestStream(mockBodyControl.Object);
                stream.StartAcceptingReads(body);

                input.Add("5\r\nHello\r\n");

                var buffer = new byte[1024];

                var count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(5, count);
                AssertASCII("Hello", new ArraySegment <byte>(buffer, 0, count));

                input.Add("0\r\n\r\n");

                count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(0, count);

                await body.StopAsync();
            }
        }
        public async Task SynchronousReadsThrowIfDisallowedByIHttpBodyControlFeature()
        {
            var allowSynchronousIO = false;
            var mockBodyControl    = new Mock <IHttpBodyControlFeature>();

            mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(() => allowSynchronousIO);
            var mockMessageBody = new Mock <MessageBody>((Frame)null);

            mockMessageBody.Setup(m => m.ReadAsync(It.IsAny <ArraySegment <byte> >(), CancellationToken.None)).ReturnsAsync(0);

            var stream = new FrameRequestStream(mockBodyControl.Object);

            stream.StartAcceptingReads(mockMessageBody.Object);

            Assert.Equal(0, await stream.ReadAsync(new byte[1], 0, 1));

            var ioEx = Assert.Throws <InvalidOperationException>(() => stream.Read(new byte[1], 0, 1));

            Assert.Equal("Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.", ioEx.Message);

            var ioEx2 = Assert.Throws <InvalidOperationException>(() => stream.CopyTo(Stream.Null));

            Assert.Equal("Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.", ioEx2.Message);

            allowSynchronousIO = true;
            Assert.Equal(0, stream.Read(new byte[1], 0, 1));
        }
Example #3
0
        public async Task CanReadFromRemainingData(HttpVersion httpVersion)
        {
            using (var input = new TestInput())
            {
                var body = MessageBody.For(httpVersion, new FrameRequestHeaders {
                    HeaderConnection = "upgrade"
                }, input.Frame);
                var mockBodyControl = new Mock <IHttpBodyControlFeature>();
                mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(true);
                var stream = new FrameRequestStream(mockBodyControl.Object);
                stream.StartAcceptingReads(body);

                input.Add("Hello");

                var buffer = new byte[1024];

                var count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(5, count);
                AssertASCII("Hello", new ArraySegment <byte>(buffer, 0, count));

                input.Fin();

                await body.StopAsync();
            }
        }
        public void Http10ConnectionClose()
        {
            var input  = new TestInput();
            var body   = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext);
            var stream = new FrameRequestStream().StartAcceptingReads(body);

            input.Add("Hello", true);

            var buffer1 = new byte[1024];
            var count1  = stream.Read(buffer1, 0, 1024);

            AssertASCII("Hello", new ArraySegment <byte>(buffer1, 0, 5));

            var buffer2 = new byte[1024];
            var count2  = stream.Read(buffer2, 0, 1024);

            Assert.Equal(0, count2);
        }
Example #5
0
        public async Task Http10ConnectionClose()
        {
            var input  = new TestInput();
            var body   = MessageBody.For("HTTP/1.0", new Dictionary <string, string[]>(), input.FrameContext);
            var stream = new FrameRequestStream(body);

            input.Add("Hello", true);
            body.Consume();

            var buffer1 = new byte[1024];
            var count1  = stream.Read(buffer1, 0, 1024);

            AssertASCII("Hello", new ArraySegment <byte>(buffer1, 0, 5));

            var buffer2 = new byte[1024];
            var count2  = stream.Read(buffer2, 0, 1024);

            Assert.Equal(0, count2);
        }
Example #6
0
        public async Task ReadFromNoContentLengthReturnsZero(HttpVersion httpVersion)
        {
            using (var input = new TestInput())
            {
                var body            = MessageBody.For(httpVersion, new FrameRequestHeaders(), input.Frame);
                var mockBodyControl = new Mock <IHttpBodyControlFeature>();
                mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(true);
                var stream = new FrameRequestStream(mockBodyControl.Object);
                stream.StartAcceptingReads(body);

                input.Add("Hello");

                var buffer = new byte[1024];
                Assert.Equal(0, stream.Read(buffer, 0, buffer.Length));

                await body.StopAsync();
            }
        }