forked from haoduotnt/aspnetwebstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpContentFormDataExtensionsTest.cs
160 lines (143 loc) · 5.23 KB
/
HttpContentFormDataExtensionsTest.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Specialized;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TestCommon;
namespace System.Net.Http
{
public class HttpContentFormDataExtensionsTest
{
public static TheoryDataSet<string> FormDataContentTypes
{
get
{
return new TheoryDataSet<string>
{
"application/x-www-form-urlencoded",
"APPLICATION/x-www-form-urlencoded",
"application/X-WWW-FORM-URLENCODED",
"application/x-www-form-urlencoded; charset=utf-8",
"application/x-www-form-urlencoded; parameter=value",
};
}
}
public static TheoryDataSet<string> NonFormDataContentTypes
{
get
{
return new TheoryDataSet<string>
{
"application/xml",
"APPLICATION/json",
"text/xml",
"text/xml; charset=utf-8",
"text/xml; parameter=value",
};
}
}
public static TheoryDataSet<string> FormData
{
get
{
return new TheoryDataSet<string>
{
"a=b",
"a+c=d+e",
"n1=v1&n2=v2",
"n1=v1a+v1b&n2=v2a+v2b",
"N=%c3%a6%c3%b8%c3%a5",
};
}
}
public static TheoryDataSet<string> IrregularFormData
{
get
{
return new TheoryDataSet<string>
{
"?data",
Char.ConvertFromUtf32(0x0D),
"Hello World",
"<string>Hello World</string>",
"{ \"Message\" : \"Hello World\"",
};
}
}
[Fact]
public void IsFormData_ThrowsOnNull()
{
Assert.ThrowsArgumentNull(() => HttpContentFormDataExtensions.IsFormData(null), "content");
}
[Fact]
public void IsFormData_HandlesNullContentType()
{
HttpContent content = new StringContent(String.Empty);
content.Headers.ContentType = null;
Assert.False(content.IsFormData());
}
[Theory]
[PropertyData("FormDataContentTypes")]
public void IsFormData_AcceptsFormDataMediaTypes(string mediaType)
{
HttpContent content = new StringContent(String.Empty);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
Assert.True(content.IsFormData());
}
[Theory]
[PropertyData("NonFormDataContentTypes")]
public void IsFormData_RejectsNonFormDataMediaTypes(string mediaType)
{
HttpContent content = new StringContent(String.Empty);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
Assert.False(content.IsFormData());
}
[Fact]
public void ReadAsFormDataAsync_ThrowsOnNull()
{
Assert.ThrowsArgumentNull(() => HttpContentFormDataExtensions.ReadAsFormDataAsync(null), "content");
}
[Theory]
[PropertyData("FormData")]
public Task ReadAsFormDataAsync_HandlesFormData(string formData)
{
// Arrange
HttpContent content = new StringContent(formData);
content.Headers.ContentType = MediaTypeConstants.ApplicationFormUrlEncodedMediaType;
// Act
return content.ReadAsFormDataAsync().ContinueWith(
readTask =>
{
NameValueCollection data = readTask.Result;
// Assert
Assert.Equal(TaskStatus.RanToCompletion, readTask.Status);
Assert.Equal(formData, data.ToString());
});
}
[Theory]
[PropertyData("IrregularFormData")]
public Task ReadAsFormDataAsync_HandlesIrregularFormData(string irregularFormData)
{
// Arrange
HttpContent content = new StringContent(irregularFormData);
content.Headers.ContentType = MediaTypeConstants.ApplicationFormUrlEncodedMediaType;
// Act
return content.ReadAsFormDataAsync().ContinueWith(
readTask =>
{
NameValueCollection data = readTask.Result;
// Assert
Assert.Equal(TaskStatus.RanToCompletion, readTask.Status);
Assert.Equal(1, data.Count);
Assert.Equal(irregularFormData, data.AllKeys[0]);
});
}
[Fact]
public void ReadAsFormDataAsync_HandlesNonFormData()
{
HttpContent content = new StringContent("{}", Encoding.UTF8, "test/unknown");
Assert.Throws<UnsupportedMediaTypeException>(() => content.ReadAsFormDataAsync().Wait());
}
}
}