public void AbortWithErrorCausesCopyToAsyncToCancel()
        {
            var stream = new FrameRequestStream(Mock.Of <IHttpBodyControlFeature>());

            stream.StartAcceptingReads(null);
            var error = new Exception();

            stream.Abort(error);
            var task = stream.CopyToAsync(Mock.Of <Stream>());

            Assert.True(task.IsFaulted);
            Assert.Same(error, task.Exception.InnerException);
        }
Example #2
0
        public async Task ReadAsyncFromNoContentLengthReturnsZero(HttpVersion httpVersion)
        {
            using (var input = new TestInput())
            {
                var body   = MessageBody.For(httpVersion, new FrameRequestHeaders(), input.Frame);
                var stream = new FrameRequestStream(Mock.Of <IHttpBodyControlFeature>());
                stream.StartAcceptingReads(body);

                input.Add("Hello");

                var buffer = new byte[1024];
                Assert.Equal(0, await stream.ReadAsync(buffer, 0, buffer.Length));

                await body.StopAsync();
            }
        }
Example #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);
            }
        }
Example #4
0
        public async Task ReadFromNoContentLengthReturnsZero(HttpVersion httpVersion)
        {
            using (var input = new TestInput())
            {
                var body            = MessageBody.For(httpVersion, new FrameRequestHeaders(), input.Frame);
                var mockBodyControl = new Mock <IHttpBodyControlFeature>();
                mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(true);
                var stream = new FrameRequestStream(mockBodyControl.Object);
                stream.StartAcceptingReads(body);

                input.Add("Hello");

                var buffer = new byte[1024];
                Assert.Equal(0, stream.Read(buffer, 0, buffer.Length));

                await body.StopAsync();
            }
        }
Example #5
0
        public void Http10ConnectionClose()
        {
            using (var input = new TestInput())
            {
                var body   = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext);
                var stream = new FrameRequestStream();
                stream.StartAcceptingReads(body);

                input.Add("Hello", true);

                var buffer1 = new byte[1024];
                var count1  = stream.Read(buffer1, 0, 1024);
                AssertASCII("Hello", new ArraySegment <byte>(buffer1, 0, 5));

                var buffer2 = new byte[1024];
                var count2  = stream.Read(buffer2, 0, 1024);
                Assert.Equal(0, count2);
            }
        }
Example #6
0
        public async Task ConnectionUpgradeKeepAlive(string headerConnection)
        {
            using (var input = new TestInput())
            {
                var body = MessageBody.For(HttpVersion.Http11, new FrameRequestHeaders {
                    HeaderConnection = headerConnection
                }, input.Frame);
                var stream = new FrameRequestStream(Mock.Of <IHttpBodyControlFeature>());
                stream.StartAcceptingReads(body);

                input.Add("Hello");

                var buffer = new byte[1024];
                Assert.Equal(5, await stream.ReadAsync(buffer, 0, buffer.Length));
                AssertASCII("Hello", new ArraySegment <byte>(buffer, 0, 5));

                input.Fin();

                await body.StopAsync();
            }
        }
Example #7
0
        public async Task ReadThrowsGivenChunkPrefixGreaterThan8Bytes()
        {
            using (var input = new TestInput())
            {
                var body = MessageBody.For(HttpVersion.Http11, new FrameRequestHeaders {
                    HeaderTransferEncoding = "chunked"
                }, input.Frame);
                var stream = new FrameRequestStream(Mock.Of <IHttpBodyControlFeature>());
                stream.StartAcceptingReads(body);

                input.Add("012345678\r");

                var buffer = new byte[1024];
                var ex     = await Assert.ThrowsAsync <BadHttpRequestException>(async() =>
                                                                                await stream.ReadAsync(buffer, 0, buffer.Length));

                Assert.Equal(CoreStrings.BadRequest_BadChunkSizeData, ex.Message);

                await body.StopAsync();
            }
        }
Example #8
0
        public async Task PumpAsyncDoesNotReturnAfterCancelingInput()
        {
            using (var input = new TestInput())
            {
                var body = MessageBody.For(HttpVersion.Http11, new FrameRequestHeaders {
                    HeaderContentLength = "2"
                }, input.Frame);
                var stream = new FrameRequestStream(Mock.Of <IHttpBodyControlFeature>());
                stream.StartAcceptingReads(body);

                // Add some input and consume it to ensure PumpAsync is running
                input.Add("a");
                Assert.Equal(1, await stream.ReadAsync(new byte[1], 0, 1));

                input.Pipe.Reader.CancelPendingRead();

                // Add more input and verify is read
                input.Add("b");
                Assert.Equal(1, await stream.ReadAsync(new byte[1], 0, 1));

                await body.StopAsync();
            }
        }
Example #9
0
        public async Task StopAsyncPreventsFurtherDataConsumption()
        {
            using (var input = new TestInput())
            {
                var body = MessageBody.For(HttpVersion.Http11, new FrameRequestHeaders {
                    HeaderContentLength = "2"
                }, input.Frame);
                var stream = new FrameRequestStream(Mock.Of <IHttpBodyControlFeature>());
                stream.StartAcceptingReads(body);

                // Add some input and consume it to ensure PumpAsync is running
                input.Add("a");
                Assert.Equal(1, await stream.ReadAsync(new byte[1], 0, 1));

                await body.StopAsync();

                // Add some more data. Checking for cancelation and exiting the loop
                // should take priority over reading this data.
                input.Add("b");

                // There shouldn't be any additional data available
                Assert.Equal(0, await stream.ReadAsync(new byte[1], 0, 1));
            }
        }
Example #10
0
        public async Task CanReadAsyncFromRemainingData(HttpVersion httpVersion)
        {
            using (var input = new TestInput())
            {
                var body = MessageBody.For(httpVersion, new FrameRequestHeaders {
                    HeaderConnection = "upgrade"
                }, input.Frame);
                var stream = new FrameRequestStream(Mock.Of <IHttpBodyControlFeature>());
                stream.StartAcceptingReads(body);

                input.Add("Hello");

                var buffer = new byte[1024];

                var count = await stream.ReadAsync(buffer, 0, buffer.Length);

                Assert.Equal(5, count);
                AssertASCII("Hello", new ArraySegment <byte>(buffer, 0, count));

                input.Fin();

                await body.StopAsync();
            }
        }