forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpValueCollectionTest.cs
More file actions
286 lines (235 loc) · 9.83 KB
/
Copy pathHttpValueCollectionTest.cs
File metadata and controls
286 lines (235 loc) · 9.83 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Net.Http.Formatting.Internal;
using System.Web.WebPages.TestUtils;
using Microsoft.TestCommon;
namespace System.Net.Http.Internal
{
public class HttpValueCollectionTest
{
#if !NETCOREAPP // Unused on .NET Core.
private static readonly int _maxCollectionKeys = 1000;
#endif
private static HttpValueCollection CreateInstance()
{
return HttpValueCollection.Create();
}
#if !NETCOREAPP // Unsupported on .NET Core.
private static void RunInIsolation(Action action)
{
AppDomainUtils.RunInSeparateAppDomain(action);
}
#endif
public static TheoryDataSet<IEnumerable<KeyValuePair<string, string>>> KeyValuePairs
{
get
{
return new TheoryDataSet<IEnumerable<KeyValuePair<string, string>>>()
{
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string,string>(null, null),
new KeyValuePair<string,string>("n0", ""),
new KeyValuePair<string,string>("n1", "v1"),
new KeyValuePair<string,string>("n@2", "v@2"),
new KeyValuePair<string,string>("n 3", "v 3"),
new KeyValuePair<string,string>("n+4", "v+4"),
new KeyValuePair<string,string>("n;5", "v;5"),
new KeyValuePair<string,string>("n=5", "v=5"),
}
};
}
}
internal class TestPropertyHolder
{
public static TheoryDataSet<HttpValueCollection, string> ToStringTestData
{
get
{
TheoryDataSet<HttpValueCollection, string> dataSet = new TheoryDataSet<HttpValueCollection, string>();
var hvc1 = CreateInstance();
hvc1.Add(null, null);
dataSet.Add(hvc1, "");
var hvc2 = CreateInstance();
hvc2.Add("name", null);
dataSet.Add(hvc2, "name");
var hvc3 = CreateInstance();
hvc3.Add("name", "");
dataSet.Add(hvc3, "name");
var hvc4 = CreateInstance();
hvc4.Add("na me", "");
dataSet.Add(hvc4, "na+me");
string encoded5 = "n%22%2C%3B%5Cn";
var hvc5 = CreateInstance();
hvc5.Add("n\",;\\n", "");
dataSet.Add(hvc5, encoded5);
var hvc6 = CreateInstance();
hvc6.Add("", "v1");
hvc6.Add("", "v2");
hvc6.Add("", "v3");
hvc6.Add("", "v4");
dataSet.Add(hvc6, "=v1&=v2&=v3&=v4");
var hvc7 = CreateInstance();
hvc7.Add("n1", "v1");
hvc7.Add("n2", "v2");
hvc7.Add("n3", "v3");
hvc7.Add("n4", "v4");
dataSet.Add(hvc7, "n1=v1&n2=v2&n3=v3&n4=v4");
string encoded8 = "n%2C1=v%2C1&n%3B2=v%3B2";
var hvc8 = CreateInstance();
hvc8.Add("n,1", "v,1");
hvc8.Add("n;2", "v;2");
dataSet.Add(hvc8, encoded8);
string encoded9 = "n1=%26&n2=%3B&n3=%26&n4=%2B&n5=%26&n6=%3D&n7=%26";
var hvc9 = CreateInstance();
hvc9.Add("n1", "&");
hvc9.Add("n2", ";");
hvc9.Add("n3", "&");
hvc9.Add("n4", "+");
hvc9.Add("n5", "&");
hvc9.Add("n6", "=");
hvc9.Add("n7", "&");
dataSet.Add(hvc9, encoded9);
var hvc10 = CreateInstance();
hvc10.Add("n1", "&");
hvc10.Add("n2", null);
hvc10.Add("n3", "null");
dataSet.Add(hvc10, "n1=%26&n2&n3=null");
return dataSet;
}
}
}
[Fact]
public void Create_CreatesEmptyCollection()
{
var nvc = CreateInstance();
Assert.IsType<HttpValueCollection>(nvc);
Assert.Empty(nvc);
}
#if !NETCOREAPP // Able to run on a separate AppDomain only on .NET Framework.
// This set of tests requires running on a separate appdomain so we don't
// touch the static property MediaTypeFormatter.MaxHttpCollectionKeys.
[Fact]
public void Create_CreateTooManyKeysThrows()
{
RunInIsolation(Create_CreateTooManyKeysThrowsPrivate);
}
private static void Create_CreateTooManyKeysThrowsPrivate()
{
// Arrange
MediaTypeFormatter.MaxHttpCollectionKeys = _maxCollectionKeys;
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
for (int i = 0; i < _maxCollectionKeys + 1; i++)
{
list.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString()));
}
// Act & Assert
Assert.Throws<InvalidOperationException>(() => HttpValueCollection.Create(list), TooManyKeysError);
}
private static string TooManyKeysError
{
get
{
return "The number of keys in a NameValueCollection has exceeded the limit of '" + _maxCollectionKeys + "'. You can adjust it by modifying the MaxHttpCollectionKeys property on the 'System.Net.Http.Formatting.MediaTypeFormatter' class.";
}
}
[Fact]
public void Create_CreateDoesntThrowTooManyValues()
{
RunInIsolation(Create_CreateDoesntThrowTooManyValuesPrivate);
}
private static void Create_CreateDoesntThrowTooManyValuesPrivate()
{
// note this is static, but also the expected type in a real run.
MediaTypeFormatter.MaxHttpCollectionKeys = _maxCollectionKeys;
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
for (int i = 0; i < _maxCollectionKeys + 1; i++)
{
list.Add(new KeyValuePair<string, string>("key", i.ToString()));
}
Assert.DoesNotThrow(() => HttpValueCollection.Create(list));
}
[Fact]
public void AddTooManyKeysThrows()
{
RunInIsolation(AddTooManyKeysThrowsPrivate);
}
private static void AddTooManyKeysThrowsPrivate()
{
// Note this is static, but also the expected type in a real run.
MediaTypeFormatter.MaxHttpCollectionKeys = _maxCollectionKeys;
HttpValueCollection collection = CreateInstance();
for (int i = 0; i < _maxCollectionKeys; i++)
{
collection.Add(i.ToString(), i.ToString());
}
// Act && Assert
Assert.Throws<InvalidOperationException>(
() => collection.Add(_maxCollectionKeys.ToString(), _maxCollectionKeys.ToString()),
TooManyKeysError);
}
[Fact]
public void AddDoesntThrowTooManyValues()
{
RunInIsolation(AddDoesntThrowTooManyValuesPrivate);
}
private static void AddDoesntThrowTooManyValuesPrivate()
{
// Note this is static, but also the expected type in a real run.
MediaTypeFormatter.MaxHttpCollectionKeys = _maxCollectionKeys;
HttpValueCollection collection = CreateInstance();
// Act && Assert
Assert.DoesNotThrow(() =>
{
for (int i = 0; i < 1001; i++)
{
collection.Add("key", i.ToString());
}
});
}
#endif
[Theory]
[PropertyData("KeyValuePairs")]
public void Create_InitializesCorrectly(IEnumerable<KeyValuePair<string, string>> input)
{
var nvc = HttpValueCollection.Create(input);
int count = input.Count();
Assert.IsType<HttpValueCollection>(nvc);
Assert.Equal(count, nvc.Count);
int index = 0;
foreach (KeyValuePair<string, string> kvp in input)
{
string expectedKey = kvp.Key ?? String.Empty;
string expectedValue = kvp.Value ?? String.Empty;
string actualKey = nvc.AllKeys[index];
string actualValue = nvc[index];
index++;
Assert.Equal(expectedKey, actualKey);
Assert.Equal(expectedValue, actualValue);
}
}
[Theory]
[PropertyData("KeyValuePairs")]
public void GetIsEquivalentToIndexerProperty(IEnumerable<KeyValuePair<string, string>> input)
{
var nvc = HttpValueCollection.Create(input);
int count = input.Count();
Assert.IsType<HttpValueCollection>(nvc);
Assert.Equal(count, nvc.Count);
foreach (KeyValuePair<string, string> kvp in input)
{
Assert.Equal(nvc[kvp.Key], nvc.Get(kvp.Key));
}
}
[Theory]
[PropertyData("ToStringTestData", PropertyType = typeof(TestPropertyHolder))]
internal void ToString_GeneratesCorrectOutput(HttpValueCollection input, string expectedOutput)
{
string actualOutput = input.ToString();
Assert.Equal(expectedOutput, actualOutput);
}
}
}