Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
 public async Task CopyToAsync_UseStreamWriteByteWithBufferSizeSmallerThanContentSize_ThrowsHttpRequestException()
 {
     // MockContent uses stream.WriteByte() rather than stream.Write(): Verify that the max. buffer size
     // is also checked when using WriteByte().
     var content = new MockContent(MockOptions.UseWriteByteInCopyTo);
     await Assert.ThrowsAsync <HttpRequestException>(() => content.LoadIntoBufferAsync(content.GetMockData().Length - 1));
 }
Esempio n. 4
0
        public async Task LoadIntoBufferAsync_ThrowCustomExceptionInOverriddenMethod_ThrowsMockException()
        {
            var content = new MockContent(new MockException(), MockOptions.ThrowInSerializeMethods);

            Task t = content.LoadIntoBufferAsync();
            await Assert.ThrowsAsync <MockException>(() => t);
        }
Esempio n. 5
0
        public async Task LoadIntoBufferAsync_ThrowObjectDisposedExceptionInOverriddenMethod_ThrowsWrappedHttpRequestException()
        {
            var content = new MockContent(new ObjectDisposedException(""), MockOptions.ThrowInSerializeMethods);

            Task t = content.LoadIntoBufferAsync();
            HttpRequestException ex = await Assert.ThrowsAsync <HttpRequestException>(() => t);

            Assert.IsType <ObjectDisposedException>(ex.InnerException);
        }
Esempio n. 6
0
        public async Task LoadIntoBufferAsync_ThrowIOExceptionInOverriddenAsyncMethod_ThrowsHttpRequestException()
        {
            var content = new MockContent(new IOException(), MockOptions.ThrowInAsyncSerializeMethods);

            Task t = content.LoadIntoBufferAsync();
            HttpRequestException ex = await Assert.ThrowsAsync <HttpRequestException>(() => t);

            Assert.IsType <IOException>(ex.InnerException);
        }
Esempio n. 7
0
        public async Task ReadAsStream_GetFromBufferedContent_ThrowsAfterReadAsStreamsAsync()
        {
            var content = new MockContent();
            await content.LoadIntoBufferAsync();

            var task = content.ReadAsStreamAsync();

            AssertExtensions.Throws <HttpRequestException>(() => content.ReadAsStream());
        }
Esempio n. 8
0
        public async Task TryComputeLength_RetrieveContentLengthFromBufferedContent_ComputeLengthIsNotCalled()
        {
            var content = new MockContent();
            await content.LoadIntoBufferAsync();

            Assert.Equal(content.GetMockData().Length, content.Headers.ContentLength);

            // Called once to determine the size of the buffer.
            Assert.Equal(1, content.TryComputeLengthCount);
        }
Esempio n. 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);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        public async Task CopyToAsync_BufferContentFirst_UseBufferedStreamAsSource()
        {
            var data    = new byte[10];
            var content = new MockContent(data);
            await content.LoadIntoBufferAsync();

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            var destination = new MemoryStream();
            await content.CopyToAsync(destination);

            // Our MockContent should not be called for the CopyTo() operation since the buffered stream should be
            // used.
            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Assert.Equal(data.Length, destination.Length);
        }
Esempio n. 12
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());
        }
Esempio n. 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);
        }
Esempio n. 14
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);
        }
Esempio n. 15
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());
        }
Esempio n. 16
0
        public async Task CopyToAsync_Buffered_CanBeCanceled()
        {
            // Buffered CopyToAsync will pass the CT to the Stream.WriteAsync
            var content = new MockContent();

            Assert.Equal(0, content.SerializeToStreamAsyncCount);
            await content.LoadIntoBufferAsync();

            Assert.Equal(1, content.SerializeToStreamAsyncCount);

            var cts = new CancellationTokenSource();

            cts.Cancel();

            using var ms = new MemoryStream();
            await Assert.ThrowsAsync <TaskCanceledException>(() => content.CopyToAsync(ms, cts.Token));

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Assert.Equal(0, content.CreateContentReadStreamCount);
        }
Esempio n. 17
0
        public async Task ReadAsStreamAsync_GetFromBufferedContent_SucceedsAfterReadAsStream()
        {
            var content = new MockContent(MockOptions.CanCalculateLength);
            await content.LoadIntoBufferAsync();

            // Call multiple times: CreateContentReadStream() should be called only once.
            Stream stream = content.ReadAsStream();

            stream = content.ReadAsStream();
            Assert.Equal(0, content.CreateContentReadStreamCount);

            stream = await content.ReadAsStreamAsync();

            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);
        }
Esempio n. 18
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);
        }
Esempio n. 19
0
        public async Task CopyToAsync_BufferContentFirst_UseBufferedStreamAsSource()
        {
            var data = new byte[10];
            var content = new MockContent(data);
            content.LoadIntoBufferAsync().Wait();

            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            var destination = new MemoryStream();
            await content.CopyToAsync(destination);

            // Our MockContent should not be called for the CopyTo() operation since the buffered stream should be 
            // used.
            Assert.Equal(1, content.SerializeToStreamAsyncCount);
            Assert.Equal(data.Length, destination.Length);
        }
Esempio n. 20
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());
        }
Esempio n. 21
0
 public async Task CopyToAsync_UseStreamWriteByteWithBufferSizeSmallerThanContentSize_ThrowsHttpRequestException()
 {
     // MockContent uses stream.WriteByte() rather than stream.Write(): Verify that the max. buffer size
     // is also checked when using WriteByte().
     var content = new MockContent(MockOptions.UseWriteByteInCopyTo);
     await Assert.ThrowsAsync<HttpRequestException>(() => content.LoadIntoBufferAsync(content.GetMockData().Length - 1));
 }
Esempio n. 22
0
        public async Task LoadIntoBufferAsync_ThrowIOExceptionInOverriddenAsyncMethod_ThrowsHttpRequestException()
        {
            var content = new MockContent(new IOException(), MockOptions.ThrowInAsyncSerializeMethods);

            HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => content.LoadIntoBufferAsync());
            Assert.IsType<IOException>(ex.InnerException);
        }
Esempio n. 23
0
        public async Task LoadIntoBufferAsync_ThrowCustomExceptionInOverriddenAsyncMethod_ExceptionBubblesUpToCaller()
        {
            var content = new MockContent(new MockException(), MockOptions.ThrowInAsyncSerializeMethods);

            await Assert.ThrowsAsync<MockException>(() => content.LoadIntoBufferAsync());
        }
Esempio n. 24
0
 public async Task LoadIntoBufferAsync_BufferSizeSmallerThanContentSizeWithCalculatedContentLength_ThrowsHttpRequestException()
 {
     var  content = new MockContent(MockOptions.CanCalculateLength);
     Task t       = content.LoadIntoBufferAsync(content.GetMockData().Length - 1);
     await Assert.ThrowsAsync <HttpRequestException>(() => t);
 }
Esempio n. 25
0
 public async Task LoadIntoBufferAsync_BufferSizeSmallerThanContentSizeWithNullContentLength_ThrowsHttpRequestException()
 {
     var content = new MockContent();
     await Assert.ThrowsAsync <HttpRequestException>(() => content.LoadIntoBufferAsync(content.GetMockData().Length - 1));
 }
Esempio n. 26
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);
        }
Esempio n. 27
0
        public async Task TryComputeLength_RetrieveContentLengthFromBufferedContent_ComputeLengthIsNotCalled()
        {
            var content = new MockContent();
            await content.LoadIntoBufferAsync();

            Assert.Equal(content.GetMockData().Length, content.Headers.ContentLength);
            
            // Called once to determine the size of the buffer.
            Assert.Equal(1, content.TryComputeLengthCount); 
        }
Esempio n. 28
0
        public async Task LoadIntoBufferAsync_ThrowCustomExceptionInOverriddenAsyncMethod_ExceptionBubblesUpToCaller()
        {
            var content = new MockContent(new MockException(), MockOptions.ThrowInAsyncSerializeMethods);

            await Assert.ThrowsAsync <MockException>(() => content.LoadIntoBufferAsync());
        }
Esempio n. 29
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());
        }
Esempio n. 30
0
        public async Task LoadIntoBufferAsync_ThrowCustomExceptionInOverriddenMethod_ThrowsMockException()
        {
            var content = new MockContent(new MockException(), MockOptions.ThrowInSerializeMethods);

            Task t = content.LoadIntoBufferAsync();
            await Assert.ThrowsAsync<MockException>(() => t);
        }
Esempio n. 31
0
        public async Task LoadIntoBufferAsync_ThrowObjectDisposedExceptionInOverriddenMethod_ThrowsWrappedHttpRequestException()
        {
            var content = new MockContent(new ObjectDisposedException(""), MockOptions.ThrowInSerializeMethods);

            HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => content.LoadIntoBufferAsync());
            Assert.IsType<ObjectDisposedException>(ex.InnerException);
        }
Esempio n. 32
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);
        }
Esempio n. 33
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);
        }
Esempio n. 34
0
 public async Task LoadIntoBufferAsync_BufferSizeSmallerThanContentSizeWithNullContentLength_ThrowsHttpRequestException()
 {
     var content = new MockContent();
     await Assert.ThrowsAsync<HttpRequestException>(() => content.LoadIntoBufferAsync(content.GetMockData().Length - 1));
 }
Esempio n. 35
0
 public async Task LoadIntoBufferAsync_BufferSizeSmallerThanContentSizeWithCalculatedContentLength_ThrowsHttpRequestException()
 {
     var content = new MockContent(MockOptions.CanCalculateLength);
     Task t = content.LoadIntoBufferAsync(content.GetMockData().Length - 1);
     await Assert.ThrowsAsync<HttpRequestException>(() => t);
 }