Esempio n. 1
0
        public async Task SynchronousReadsThrowIfDisallowedByIHttpBodyControlFeature()
        {
            var allowSynchronousIO = false;
            var mockBodyControl    = new Mock <IHttpBodyControlFeature>();

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

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

            var stream = new HttpRequestStream(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));
        }