Example #1
0
        public async Task SynchronousWritesThrowIfDisallowedByIHttpBodyControlFeature()
        {
            var allowSynchronousIO = false;
            var mockBodyControl    = new Mock <IHttpBodyControlFeature>();

            mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(() => allowSynchronousIO);
            var mockFrameControl = new Mock <IFrameControl>();

            mockFrameControl.Setup(m => m.WriteAsync(It.IsAny <ArraySegment <byte> >(), CancellationToken.None)).Returns(Task.CompletedTask);

            var stream = new FrameResponseStream(mockBodyControl.Object, mockFrameControl.Object);

            stream.StartAcceptingWrites();

            // WriteAsync doesn't throw.
            await stream.WriteAsync(new byte[1], 0, 1);

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

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

            allowSynchronousIO = true;
            // If IHttpBodyControlFeature.AllowSynchronousIO is true, Write no longer throws.
            stream.Write(new byte[1], 0, 1);
        }
Example #2
0
        public void StopAcceptingWritesCausesWriteToThrowObjectDisposedException()
        {
            var stream = new FrameResponseStream(Mock.Of <IHttpBodyControlFeature>(), Mock.Of <IFrameControl>());

            stream.StartAcceptingWrites();
            stream.StopAcceptingWrites();
            Assert.Throws <ObjectDisposedException>(() => { stream.WriteAsync(new byte[1], 0, 1); });
        }