Пример #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);
        }
Пример #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); });
        }
Пример #3
0
        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);
            }
        }