public async Task SynchronousWritesThrowIfDisallowedByIHttpBodyControlFeature() { var allowSynchronousIO = false; var mockBodyControl = new Mock <IHttpBodyControlFeature>(); mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(() => allowSynchronousIO); var mockHttpResponseControl = new Mock <IHttpResponseControl>(); mockHttpResponseControl.Setup(m => m.WriteAsync(It.IsAny <ArraySegment <byte> >(), CancellationToken.None)).Returns(Task.CompletedTask); var stream = new HttpResponseStream(mockBodyControl.Object, mockHttpResponseControl.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); }
public void StopAcceptingWritesCausesWriteToThrowObjectDisposedException() { var stream = new HttpResponseStream(Mock.Of <IHttpBodyControlFeature>(), Mock.Of <IHttpResponseControl>()); stream.StartAcceptingWrites(); stream.StopAcceptingWrites(); Assert.Throws <ObjectDisposedException>(() => { stream.WriteAsync(new byte[1], 0, 1); }); }
public void StopAcceptingWritesCausesWriteToThrowObjectDisposedException() { var stream = new HttpResponseStream(Mock.Of <IHttpBodyControlFeature>(), Mock.Of <IHttpResponseControl>()); stream.StartAcceptingWrites(); stream.StopAcceptingWrites(); var ex = Assert.Throws <ObjectDisposedException>(() => { stream.WriteAsync(new byte[1], 0, 1); }); Assert.Contains(CoreStrings.WritingToResponseBodyAfterResponseCompleted, ex.Message); }