public async Task Dispose_ClosesOutputStreamOnNonMemoryStream()
        {
            // Arrange
            HttpContent   parent     = new StringContent("hello");
            Mock <Stream> mockStream = new Mock <Stream>()
            {
                CallBase = true
            };

            mockStream.Setup(s => s.CanWrite).Returns(true);
            Mock <MultipartStreamProvider> mockStreamProvider = new Mock <MultipartStreamProvider>();

            mockStreamProvider.Setup(sp => sp.GetStream(It.IsAny <HttpContent>(), It.IsAny <HttpContentHeaders>())).Returns(mockStream.Object);
            MimeBodyPart bodypart = new MimeBodyPart(mockStreamProvider.Object, 1024, parent);

            bodypart.Segments.Add(new ArraySegment <byte>(new byte[] { 1 }));
            await bodypart.WriteSegment(bodypart.Segments[0], CancellationToken.None);

            bodypart.IsComplete = true;

            // Act
            bodypart.GetCompletedHttpContent();
            bodypart.Dispose();

            // Assert
            mockStream.Verify(s => s.Close(), Times.Once());
        }
Exemplo n.º 2
0
        public void GetOutputStream_ThrowsOnInvalidStreamProvider(MultipartStreamProvider streamProvider)
        {
            // Arrange
            HttpContent parent = new StringContent("hello");
            MimeBodyPart bodypart = new MimeBodyPart(streamProvider, 1024, parent);
            bodypart.Segments.Add(new ArraySegment<byte>(new byte[] { 1 }));

            // Act and Assert
            Assert.Throws<InvalidOperationException>(() => bodypart.WriteSegment(bodypart.Segments[0]).Wait());
        }
        public async Task GetOutputStream_ThrowsOnInvalidStreamProvider(MultipartStreamProvider streamProvider)
        {
            // Arrange
            HttpContent  parent   = new StringContent("hello");
            MimeBodyPart bodypart = new MimeBodyPart(streamProvider, 1024, parent);

            bodypart.Segments.Add(new ArraySegment <byte>(new byte[] { 1 }));

            // Act and Assert
            await Assert.ThrowsAsync <InvalidOperationException>(() => bodypart.WriteSegment(bodypart.Segments[0], CancellationToken.None));
        }
Exemplo n.º 4
0
        public void GetOutputStream_ThrowsOnInvalidStreamProvider(MultipartStreamProvider streamProvider)
        {
            // Arrange
            HttpContent  parent   = new StringContent("hello");
            MimeBodyPart bodypart = new MimeBodyPart(streamProvider, 1024, parent);

            bodypart.Segments.Add(new ArraySegment <byte>(new byte[] { 1 }));

            // Act and Assert
            Assert.Throws <InvalidOperationException>(() => bodypart.WriteSegment(bodypart.Segments[0]).Wait());
        }
Exemplo n.º 5
0
        public void Dispose_SetsPositionToZeroOnMemoryStream()
        {
            // Arrange
            HttpContent parent = new StringContent("hello");
            Mock<MemoryStream> mockStream = new Mock<MemoryStream> { CallBase = true };
            Mock<MultipartStreamProvider> mockStreamProvider = new Mock<MultipartStreamProvider>();
            mockStreamProvider.Setup(sp => sp.GetStream(It.IsAny<HttpContent>(), It.IsAny<HttpContentHeaders>())).Returns(mockStream.Object);
            MimeBodyPart bodypart = new MimeBodyPart(mockStreamProvider.Object, 1024, parent);
            bodypart.Segments.Add(new ArraySegment<byte>(new byte[] { 1 }));
            bodypart.WriteSegment(bodypart.Segments[0]).Wait();
            bodypart.IsComplete = true;

            // Act
            bodypart.GetCompletedHttpContent();
            bodypart.Dispose();

            // Assert
            mockStream.VerifySet(s => s.Position = 0);
        }
Exemplo n.º 6
0
        public void Dispose_ClosesOutputStreamOnNonMemoryStream()
        {
            // Arrange
            HttpContent parent = new StringContent("hello");
            Mock<Stream> mockStream = new Mock<Stream>() { CallBase = true };
            mockStream.Setup(s => s.CanWrite).Returns(true);
            Mock<MultipartStreamProvider> mockStreamProvider = new Mock<MultipartStreamProvider>();
            mockStreamProvider.Setup(sp => sp.GetStream(It.IsAny<HttpContent>(), It.IsAny<HttpContentHeaders>())).Returns(mockStream.Object);
            MimeBodyPart bodypart = new MimeBodyPart(mockStreamProvider.Object, 1024, parent);
            bodypart.Segments.Add(new ArraySegment<byte>(new byte[] { 1 }));
            bodypart.WriteSegment(bodypart.Segments[0], CancellationToken.None).Wait();
            bodypart.IsComplete = true;

            // Act
            bodypart.GetCompletedHttpContent();
            bodypart.Dispose();

            // Assert
            mockStream.Verify(s => s.Close(), Times.Once());
        }
Exemplo n.º 7
0
        public void Dispose_SetsPositionToZeroOnMemoryStream()
        {
            // Arrange
            HttpContent         parent     = new StringContent("hello");
            Mock <MemoryStream> mockStream = new Mock <MemoryStream> {
                CallBase = true
            };
            Mock <MultipartStreamProvider> mockStreamProvider = new Mock <MultipartStreamProvider>();

            mockStreamProvider.Setup(sp => sp.GetStream(It.IsAny <HttpContent>(), It.IsAny <HttpContentHeaders>())).Returns(mockStream.Object);
            MimeBodyPart bodypart = new MimeBodyPart(mockStreamProvider.Object, 1024, parent);

            bodypart.Segments.Add(new ArraySegment <byte>(new byte[] { 1 }));
            bodypart.WriteSegment(bodypart.Segments[0], CancellationToken.None).Wait();
            bodypart.IsComplete = true;

            // Act
            bodypart.GetCompletedHttpContent();
            bodypart.Dispose();

            // Assert
            mockStream.VerifySet(s => s.Position = 0);
        }