private async Task TestBodyStream_Impl()
        {
            var listener = new NamedPipeListener(TestContext.TestName);
            var payload  = Encoding.UTF8.GetBytes("Hello world");
            await listener.StartAsync(con =>
            {
                Task.Run(async() =>
                {
                    try
                    {
                        await con.WriteAsync(payload, 0, payload.Length);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("... WriteAsync exception:" + e.Message);
                    }
                });
            }, CancellationToken.None);


            var dialer = new NamedPipeDialer(TestContext.TestName);
            var stream = await dialer.DialAsync(new HttpRequestMessage(), CancellationToken.None);

            var bodyStream = new BodyStream(stream, payload.Length);
            var data       = new byte[4096];
            var read       = await bodyStream.ReadAsync(data, 0, data.Length);

            Assert.AreEqual(payload.Length, read);
            read = await bodyStream.ReadAsync(data, 0, data.Length);

            Assert.AreEqual(0, read);

            // Clean up
            await listener.StopAsync(CancellationToken.None);
        }
Beispiel #2
0
        public async Task TestBodyStreamCloseOnReadEnd()
        {
            byte[] payload    = new byte[10];
            var    ms         = new TestStream(payload);
            var    bodyStream = new BodyStream(ms, 10, closeOnReachEnd: true);
            var    result     = new byte[10];
            var    read       = await bodyStream.ReadAsync(result, 0, 10);

            Assert.Equal(10, read);
            Assert.True(ms.Disposed);
        }