forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMimeBodyPartTest.cs
More file actions
84 lines (74 loc) · 3.5 KB
/
Copy pathMimeBodyPartTest.cs
File metadata and controls
84 lines (74 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Moq;
namespace System.Net.Http
{
public class MimeBodyPartTest
{
public static TheoryDataSet<MultipartStreamProvider> BadMultipartStreamProviders
{
get
{
return new TheoryDataSet<MultipartStreamProvider>
{
new HttpContentMultipartExtensionsTests.NullProvider(),
new HttpContentMultipartExtensionsTests.BadStreamProvider(),
new HttpContentMultipartExtensionsTests.ReadOnlyStreamProvider(),
};
}
}
[Theory]
[TestDataSet(typeof(MimeBodyPartTest), "BadMultipartStreamProviders")]
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));
}
[Fact]
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());
}
[Fact]
public async Task 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 }));
await bodypart.WriteSegment(bodypart.Segments[0], CancellationToken.None);
bodypart.IsComplete = true;
// Act
bodypart.GetCompletedHttpContent();
bodypart.Dispose();
// Assert
mockStream.VerifySet(s => s.Position = 0);
}
}
}