forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipartRelatedStreamProviderTests.cs
More file actions
114 lines (95 loc) · 4.64 KB
/
Copy pathMultipartRelatedStreamProviderTests.cs
File metadata and controls
114 lines (95 loc) · 4.64 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
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
110
111
112
113
114
// 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.Net.Http.Headers;
using System.Threading.Tasks;
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> MultipartRelatedWithStartParameter
{
get
{
return new TheoryDataSet<string>
{
{ String.Format("multipart/related; boundary={0}; start=\"{1}\"", Boundary, ContentID) },
{ String.Format("multipart/related; start={0}; boundary={1}", ContentID, Boundary) },
};
}
}
public static TheoryDataSet<string> MultipartWithMissingOrInvalidStartParameter
{
get
{
return new TheoryDataSet<string>
{
{ String.Format("multipart/form-data; start=\"{0}\"; boundary={1}", ContentID, Boundary) },
{ String.Format("multipart/form-data; start={0}; boundary={1}", ContentID, Boundary) },
{ String.Format("multipart/form-data; boundary={0}", Boundary) },
{ String.Format("multipart/related; boundary={0}", Boundary) },
{ String.Format("multipart/mixed; start={0}; boundary={1}", ContentID, Boundary) },
{ String.Format("multipart/mixed; boundary={1}", ContentID, Boundary) },
};
}
}
[Fact]
public void RootContent_ReturnsNull()
{
MultipartRelatedStreamProvider provider = new MultipartRelatedStreamProvider();
Assert.Null(provider.RootContent);
}
[Theory]
[PropertyData("MultipartRelatedWithStartParameter")]
public async Task RootContent_ReturnsNullIfContentIDIsNotMatched(string mediaType)
{
// 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 = await content.ReadAsMultipartAsync(new MultipartRelatedStreamProvider());
// Act
HttpContent actualRootContent = provider.RootContent;
// Assert
Assert.Null(actualRootContent);
}
[Theory]
[PropertyData("MultipartRelatedWithStartParameter")]
public async Task RootContent_PicksContent_WithStartParameter(string mediaType)
{
var result = await RootContent_PicksContent_Setup(mediaType);
Assert.Equal(ContentIDRootContent, result);
}
[Theory]
[PropertyData("MultipartWithMissingOrInvalidStartParameter")]
public async Task RootContent_PicksContent_WithoutStartParameter(string mediaType)
{
var result = await RootContent_PicksContent_Setup(mediaType);
Assert.Equal(DefaultRootContent, result);
}
private async Task<string> RootContent_PicksContent_Setup(string mediaType)
{
// 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 = await content.ReadAsMultipartAsync(new MultipartRelatedStreamProvider());
// Act
HttpContent actualRootContent = provider.RootContent;
return await actualRootContent.ReadAsStringAsync();
}
}
}