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.WritePipeAsync(It.IsAny <ReadOnlyMemory <byte> >(), CancellationToken.None)).Returns(new ValueTask <FlushResult>(new FlushResult())); var pipeWriter = new HttpResponsePipeWriter(mockHttpResponseControl.Object); var stream = new HttpResponseStream(mockBodyControl.Object, pipeWriter); pipeWriter.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 Streams(IHttpBodyControlFeature bodyControl, HttpResponsePipeWriter writer) { _request = new HttpRequestStream(bodyControl); _emptyRequest = new HttpRequestStream(bodyControl); _response = new HttpResponseStream(bodyControl, writer); _upgradeableResponse = new WrappingStream(_response); _upgradeStream = new HttpUpgradeStream(_request, _response); }
public void StopAcceptingWritesCausesWriteToThrowObjectDisposedException() { var pipeWriter = new HttpResponsePipeWriter(Mock.Of <IHttpResponseControl>()); var stream = new HttpResponseStream(Mock.Of <IHttpBodyControlFeature>(), pipeWriter); pipeWriter.StartAcceptingWrites(); pipeWriter.StopAcceptingWritesAsync(); var ex = Assert.Throws <ObjectDisposedException>(() => { stream.WriteAsync(new byte[1], 0, 1); }); Assert.Contains(CoreStrings.WritingToResponseBodyAfterResponseCompleted, ex.Message); }
public BodyControl(IHttpBodyControlFeature bodyControl, IHttpResponseControl responseControl) { _requestReader = new HttpRequestPipeReader(); _request = new HttpRequestStream(bodyControl, _requestReader); _emptyRequestReader = new HttpRequestPipeReader(); _emptyRequest = new HttpRequestStream(bodyControl, _emptyRequestReader); _responseWriter = new HttpResponsePipeWriter(responseControl); _response = new HttpResponseStream(bodyControl, _responseWriter); _upgradeableResponse = new WrappingStream(_response); _upgradeStream = new HttpUpgradeStream(_request, _response); }
public static WebTransportStream CreateStream(WebTransportStreamType type, Memory <byte>?memory = null) { var features = new FeatureCollection(); features.Set <IStreamIdFeature>(new StreamId(streamCounter++)); features.Set <IStreamDirectionFeature>(new DefaultStreamDirectionFeature(type != WebTransportStreamType.Output, type != WebTransportStreamType.Input)); features.Set(Mock.Of <IConnectionItemsFeature>()); features.Set(Mock.Of <IProtocolErrorCodeFeature>()); var writer = new HttpResponsePipeWriter(new StreamWriterControl(memory)); writer.StartAcceptingWrites(); var transport = new DuplexPipe(new StreamReader(memory), writer); return(new WebTransportStream(TestContextFactory.CreateHttp3StreamContext("id", null, new TestServiceContext(), features, null, null, null, transport), type)); }
public HttpResponseStream(IHttpBodyControlFeature bodyControl, HttpResponsePipeWriter pipeWriter) { _bodyControl = bodyControl; _pipeWriter = pipeWriter; }
private static HttpResponseStream CreateHttpResponseStream() { var pipeWriter = new HttpResponsePipeWriter(Mock.Of <IHttpResponseControl>()); return(new HttpResponseStream(Mock.Of <IHttpBodyControlFeature>(), pipeWriter)); }