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 (Stream request, Stream response) Start() { _request.StartAcceptingReads(_context); _response.StartAcceptingWrites(); return(_upgradeableRequest, _upgradeableResponse); }
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); }
public (Stream request, Stream response) Start(MessageBody body) { _request.StartAcceptingReads(body); _emptyRequest.StartAcceptingReads(MessageBody.ZeroContentLengthClose); _response.StartAcceptingWrites(); if (body.RequestUpgrade) { // until Upgrade() is called, context.Response.Body should use the normal output stream _upgradeableResponse.SetInnerStream(_response); // upgradeable requests should never have a request body return(_emptyRequest, _upgradeableResponse); } else { return(_request, _response); } }
public void Start() { _request.StartAcceptingReads(); _response.StartAcceptingWrites(); }