示例#1
0
        public async Task ReadAsStreamAsync_UseBaseImplementation_ContentGetsBufferedThenMemoryStreamReturned()
        {
            var    content = new MockContent(MockOptions.DontOverrideCreateContentReadStream);
            Stream stream  = await content.ReadAsStreamAsync();

            Assert.NotNull(stream);
            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Stream stream2 = await content.ReadAsStreamAsync();

            Assert.Same(stream, stream2);
            Assert.Equal(0, stream.Position);
            Assert.Equal((byte)'d', stream.ReadByte());
        }
示例#2
0
        public async Task ReadAsStreamAsync_GetFromBufferedContent_CreateContentReadStreamCalled()
        {
            var content = new MockContent(MockOptions.CanCalculateLength);
            await content.LoadIntoBufferAsync();

            Stream stream = await content.ReadAsStreamAsync();

            Assert.Equal(0, content.CreateContentReadStreamCount);
            Assert.Equal(content.GetMockData().Length, stream.Length);
            Stream stream2 = await content.ReadAsStreamAsync();

            Assert.Same(stream, stream2);
            Assert.Equal(0, stream.Position);
            Assert.Equal((byte)'d', stream.ReadByte());
        }
示例#3
0
        public void ReadAsStream_GetFromUnbufferedContent_ThrowsAfterReadAsStreamsAsync()
        {
            var content = new MockContent();

            var task = content.ReadAsStreamAsync();

            AssertExtensions.Throws <HttpRequestException>(() => content.ReadAsStream());
        }
示例#4
0
        public async Task ReadAsStreamAsync_GetFromUnbufferedContent_CreateContentReadStreamCalledOnce()
        {
            var content = new MockContent(MockOptions.CanCalculateLength);

            // Call multiple times: CreateContentReadStreamAsync() should be called only once.
            Stream stream = await content.ReadAsStreamAsync();

            stream = await content.ReadAsStreamAsync();

            stream = await content.ReadAsStreamAsync();

            Assert.Equal(1, content.CreateContentReadStreamCount);
            Assert.Equal(content.GetMockData().Length, stream.Length);
            Stream stream2 = await content.ReadAsStreamAsync();

            Assert.Same(stream, stream2);
        }
示例#5
0
        public async Task ReadAsStream_GetFromBufferedContent_ThrowsAfterReadAsStreamsAsync()
        {
            var content = new MockContent();
            await content.LoadIntoBufferAsync();

            var task = content.ReadAsStreamAsync();

            AssertExtensions.Throws <HttpRequestException>(() => content.ReadAsStream());
        }
示例#6
0
        public async Task ReadAsStreamAsync_Unbuffered_CustomContent_CanBeCanceled()
        {
            var content = new MockContent();

            var cts = new CancellationTokenSource();

            cts.Cancel();

            await Assert.ThrowsAsync <TaskCanceledException>(() => content.ReadAsStreamAsync(cts.Token));
        }
示例#7
0
        public async Task ReadAsStreamAsync_FirstGetFromUnbufferedContentThenGetFromBufferedContent_SameStream()
        {
            var content = new MockContent(MockOptions.CanCalculateLength);

            Stream before = await content.ReadAsStreamAsync();

            Assert.Equal(1, content.CreateContentReadStreamCount);

            await content.LoadIntoBufferAsync();

            Stream after = await content.ReadAsStreamAsync();

            Assert.Equal(1, content.CreateContentReadStreamCount);

            // Note that ContentReadStream returns always the same stream. If the user gets the stream, buffers content,
            // and gets the stream again, the same instance is returned. Returning a different instance could be
            // confusing, even though there shouldn't be any real world scenario for retrieving the read stream both
            // before and after buffering content.
            Assert.Equal(before, after);
        }
示例#8
0
        public async Task Dispose_GetReadStreamThenDispose_ReadStreamGetsDisposed()
        {
            var content        = new MockContent();
            MockMemoryStream s = (MockMemoryStream)await content.ReadAsStreamAsync();

            Assert.Equal(1, content.CreateContentReadStreamCount);

            Assert.Equal(0, s.DisposeCount);
            content.Dispose();
            Assert.Equal(1, s.DisposeCount);
        }
示例#9
0
        public async Task LoadIntoBufferAsync_CallOnMockContentWithCalculatedContentLength_CopyToAsyncMemoryStreamCalled()
        {
            var content = new MockContent(MockOptions.CanCalculateLength);

            Assert.NotNull(content.Headers.ContentLength);
            await content.LoadIntoBufferAsync();

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Stream stream = await content.ReadAsStreamAsync();

            Assert.False(stream.CanWrite);
        }
示例#10
0
        public async Task LoadIntoBufferAsync_CallMultipleTimesWithNullContentLength_CopyToAsyncMemoryStreamCalledOnce()
        {
            var content = new MockContent();
            await content.LoadIntoBufferAsync();

            await content.LoadIntoBufferAsync();

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Stream stream = await content.ReadAsStreamAsync();

            Assert.False(stream.CanWrite);
        }
示例#11
0
        public async Task LoadIntoBufferAsync_CallMultipleTimesWithCalculatedContentLength_CopyToAsyncMemoryStreamCalledOnce(bool readStreamAsync)
        {
            var content = new MockContent(MockOptions.CanCalculateLength);
            await content.LoadIntoBufferAsync();

            await content.LoadIntoBufferAsync();

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Stream stream = await content.ReadAsStreamAsync(readStreamAsync);

            Assert.False(stream.CanWrite);
        }
示例#12
0
        public async Task LoadIntoBufferAsync_CallOnMockContentWithNullContentLength_CopyToAsyncMemoryStreamCalled(bool readStreamAsync)
        {
            var content = new MockContent();

            Assert.Null(content.Headers.ContentLength);
            await content.LoadIntoBufferAsync();

            Assert.NotNull(content.Headers.ContentLength);
            Assert.Equal(content.MockData.Length, content.Headers.ContentLength);

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Stream stream = await content.ReadAsStreamAsync(readStreamAsync);

            Assert.False(stream.CanWrite);
        }
示例#13
0
        public async Task LoadIntoBufferAsync_CallOnMockContentWithLessLengthThanContentLengthHeader_BufferedStreamLengthMatchesActualLengthNotContentLengthHeaderValue()
        {
            byte[] data    = Encoding.UTF8.GetBytes("16 bytes of data");
            var    content = new MockContent(data);

            content.Headers.ContentLength = 32; // Set the Content-Length header to a value > actual data length.
            Assert.Equal(32, content.Headers.ContentLength);

            await content.LoadIntoBufferAsync();

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Assert.NotNull(content.Headers.ContentLength);
            Assert.Equal(32, content.Headers.ContentLength);
            Stream stream = await content.ReadAsStreamAsync();

            Assert.Equal(data.Length, stream.Length);
        }
示例#14
0
        public void Dispose_DisposedObjectThenAccessMembers_ThrowsObjectDisposedException()
        {
            var content = new MockContent();

            content.Dispose();

            var m = new MemoryStream();

            Assert.Throws <ObjectDisposedException>(() => { content.CopyToAsync(m); });
            Assert.Throws <ObjectDisposedException>(() => { content.ReadAsByteArrayAsync(); });
            Assert.Throws <ObjectDisposedException>(() => { content.ReadAsStringAsync(); });
            Assert.Throws <ObjectDisposedException>(() => { content.ReadAsStreamAsync(); });
            Assert.Throws <ObjectDisposedException>(() => { content.LoadIntoBufferAsync(); });

            // Note that we don't throw when users access the Headers property. This is useful e.g. to be able to
            // read the headers of a content, even though the content is already disposed. Note that the .NET guidelines
            // only require members to throw ObjectDisposedExcpetion for members "that cannot be used after the object
            // has been disposed of".
            _output.WriteLine(content.Headers.ToString());
        }
示例#15
0
        public async Task ReadAsStreamAsync_GetFromUnbufferedContent_CreateContentReadStreamCalledOnce()
        {
            var content = new MockContent(MockOptions.CanCalculateLength);

            // Call multiple times: CreateContentReadStreamAsync() should be called only once.
            Stream stream = await content.ReadAsStreamAsync();
            stream = await content.ReadAsStreamAsync();
            stream = await content.ReadAsStreamAsync();

            Assert.Equal(1, content.CreateContentReadStreamCount);
            Assert.Equal(content.GetMockData().Length, stream.Length);
            Stream stream2 = await content.ReadAsStreamAsync();
            Assert.Same(stream, stream2);
        }
示例#16
0
        public async Task LoadIntoBufferAsync_CallOnMockContentWithLessLengthThanContentLengthHeader_BufferedStreamLengthMatchesActualLengthNotContentLengthHeaderValue()
        {
            byte[] data = Encoding.UTF8.GetBytes("16 bytes of data");
            var content = new MockContent(data);
            content.Headers.ContentLength = 32; // Set the Content-Length header to a value > actual data length.
            Assert.Equal(32, content.Headers.ContentLength);

            await content.LoadIntoBufferAsync();

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Assert.NotNull(content.Headers.ContentLength);
            Assert.Equal(32, content.Headers.ContentLength);
            Stream stream = await content.ReadAsStreamAsync();
            Assert.Equal(data.Length, stream.Length);
        }
示例#17
0
        public async Task ReadAsStreamAsync_FirstGetFromUnbufferedContentThenGetFromBufferedContent_SameStream()
        {
            var content = new MockContent(MockOptions.CanCalculateLength);

            Stream before = await content.ReadAsStreamAsync();
            Assert.Equal(1, content.CreateContentReadStreamCount);

            await content.LoadIntoBufferAsync();

            Stream after = await content.ReadAsStreamAsync();
            Assert.Equal(1, content.CreateContentReadStreamCount);

            // Note that ContentReadStream returns always the same stream. If the user gets the stream, buffers content,
            // and gets the stream again, the same instance is returned. Returning a different instance could be 
            // confusing, even though there shouldn't be any real world scenario for retrieving the read stream both
            // before and after buffering content.
            Assert.Equal(before, after);
        }
示例#18
0
        public async Task ReadAsStreamAsync_GetFromBufferedContent_CreateContentReadStreamCalled()
        {
            var content = new MockContent(MockOptions.CanCalculateLength);
            await content.LoadIntoBufferAsync();

            Stream stream = await content.ReadAsStreamAsync();

            Assert.Equal(0, content.CreateContentReadStreamCount);
            Assert.Equal(content.GetMockData().Length, stream.Length);
            Stream stream2 = await content.ReadAsStreamAsync();
            Assert.Same(stream, stream2);
            Assert.Equal(0, stream.Position);
            Assert.Equal((byte)'d', stream.ReadByte());
        }
示例#19
0
        public async Task ReadAsStreamAsync_UseBaseImplementation_ContentGetsBufferedThenMemoryStreamReturned()
        {
            var content = new MockContent(MockOptions.DontOverrideCreateContentReadStream);
            Stream stream = await content.ReadAsStreamAsync();

            Assert.NotNull(stream);
            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Stream stream2 = await content.ReadAsStreamAsync();
            Assert.Same(stream, stream2);
            Assert.Equal(0, stream.Position);
            Assert.Equal((byte)'d', stream.ReadByte());
        }
示例#20
0
        public async Task LoadIntoBufferAsync_CallOnMockContentWithNullContentLength_CopyToAsyncMemoryStreamCalled()
        {
            var content = new MockContent();
            Assert.Null(content.Headers.ContentLength);
            await content.LoadIntoBufferAsync();
            Assert.NotNull(content.Headers.ContentLength);
            Assert.Equal(content.MockData.Length, content.Headers.ContentLength);

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Stream stream = await content.ReadAsStreamAsync();
            Assert.False(stream.CanWrite);
        }
示例#21
0
        public async Task LoadIntoBufferAsync_CallMultipleTimesWithNullContentLength_CopyToAsyncMemoryStreamCalledOnce()
        {
            var content = new MockContent();
            await content.LoadIntoBufferAsync();
            await content.LoadIntoBufferAsync();

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Stream stream = await content.ReadAsStreamAsync();
            Assert.False(stream.CanWrite);
        }
示例#22
0
        public async Task Dispose_GetReadStreamThenDispose_ReadStreamGetsDisposed()
        {
            var content = new MockContent();
            MockMemoryStream s = (MockMemoryStream) await content.ReadAsStreamAsync();
            Assert.Equal(1, content.CreateContentReadStreamCount);

            Assert.Equal(0, s.DisposeCount);
            content.Dispose();
            Assert.Equal(1, s.DisposeCount);
        }
示例#23
0
        public async Task Dispose_DisposedObjectThenAccessMembers_ThrowsObjectDisposedException()
        {
            var content = new MockContent();
            content.Dispose();

            var m = new MemoryStream();

            await Assert.ThrowsAsync<ObjectDisposedException>(() => content.CopyToAsync(m));
            await Assert.ThrowsAsync<ObjectDisposedException>(() => content.ReadAsByteArrayAsync());
            await Assert.ThrowsAsync<ObjectDisposedException>(() => content.ReadAsStringAsync());
            await Assert.ThrowsAsync<ObjectDisposedException>(() => content.ReadAsStreamAsync());
            await Assert.ThrowsAsync<ObjectDisposedException>(() => content.LoadIntoBufferAsync());

            // Note that we don't throw when users access the Headers property. This is useful e.g. to be able to 
            // read the headers of a content, even though the content is already disposed. Note that the .NET guidelines
            // only require members to throw ObjectDisposedExcpetion for members "that cannot be used after the object 
            // has been disposed of".
            _output.WriteLine(content.Headers.ToString());
        }