forked from haoduotnt/aspnetwebstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultipartRelatedStreamProviderTests.cs
109 lines (91 loc) · 4.45 KB
/
MultipartRelatedStreamProviderTests.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Net.Http.Headers;
using Microsoft.TestCommon;
namespace System.Net.Http
{
public class MultipartRelatedStreamProviderTests : MultipartStreamProviderTestBase<MultipartRelatedStreamProvider>
{
private const string ContentID = "12345";
private const string Boundary = "-A-";
private const string DefaultRootContent = "Default root content";
private const string ContentIDRootContent = "Content with matching Content-ID";
private const string OtherContent = "Other Content";
public static TheoryDataSet<string, bool> MultipartRelatedWithStartParameter
{
get
{
return new TheoryDataSet<string, bool>
{
{ String.Format("multipart/related; boundary={0}; start=\"{1}\"", Boundary, ContentID), true },
{ String.Format("multipart/related; start={0}; boundary={1}", ContentID, Boundary), true },
};
}
}
public static TheoryDataSet<string, bool> MultipartWithMissingOrInvalidStartParameter
{
get
{
return new TheoryDataSet<string, bool>
{
{ String.Format("multipart/form-data; start=\"{0}\"; boundary={1}", ContentID, Boundary), false },
{ String.Format("multipart/form-data; start={0}; boundary={1}", ContentID, Boundary), false },
{ String.Format("multipart/form-data; boundary={0}", Boundary), false },
{ String.Format("multipart/related; boundary={0}", Boundary), false },
{ String.Format("multipart/mixed; start={0}; boundary={1}", ContentID, Boundary), false },
{ String.Format("multipart/mixed; boundary={1}", ContentID, Boundary), false },
};
}
}
[Fact]
public void RootContent_ReturnsNull()
{
MultipartRelatedStreamProvider provider = new MultipartRelatedStreamProvider();
Assert.Null(provider.RootContent);
}
[Theory]
[PropertyData("MultipartRelatedWithStartParameter")]
public void RootContent_ReturnsNullIfContentIDIsNotMatched(string mediaType, bool hasStartParameter)
{
// Arrange
MultipartContent content = new MultipartContent("related", Boundary);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
content.Add(new StringContent(DefaultRootContent));
content.Add(new StringContent(OtherContent));
HttpContent expectedRootContent = new StringContent(ContentIDRootContent);
expectedRootContent.Headers.Add("Content-ID", "NoMatch");
content.Add(expectedRootContent);
MultipartRelatedStreamProvider provider = content.ReadAsMultipartAsync(new MultipartRelatedStreamProvider()).Result;
// Act
HttpContent actualRootContent = provider.RootContent;
// Assert
Assert.Null(actualRootContent);
}
[Theory]
[PropertyData("MultipartRelatedWithStartParameter")]
[PropertyData("MultipartWithMissingOrInvalidStartParameter")]
public void RootContent_PicksContent(string mediaType, bool hasStartParameter)
{
// Arrange
MultipartContent content = new MultipartContent("related", Boundary);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
content.Add(new StringContent(DefaultRootContent));
content.Add(new StringContent(OtherContent));
HttpContent contentIDContent = new StringContent(ContentIDRootContent);
contentIDContent.Headers.Add("Content-ID", ContentID);
content.Add(contentIDContent);
MultipartRelatedStreamProvider provider = content.ReadAsMultipartAsync(new MultipartRelatedStreamProvider()).Result;
// Act
HttpContent actualRootContent = provider.RootContent;
string result = actualRootContent.ReadAsStringAsync().Result;
// Assert
if (hasStartParameter)
{
Assert.Equal(ContentIDRootContent, result);
}
else
{
Assert.Equal(DefaultRootContent, result);
}
}
}
}