forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionModelBinderTest.cs
More file actions
334 lines (293 loc) · 14.9 KB
/
Copy pathCollectionModelBinderTest.cs
File metadata and controls
334 lines (293 loc) · 14.9 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// 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.Globalization;
using System.Linq;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using System.Web.Http.Metadata.Providers;
using System.Web.Http.ModelBinding.Binders;
using System.Web.Http.Util;
using System.Web.Http.Validation;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ModelBinding
{
public class CollectionModelBinderTest
{
[Fact]
public void BindComplexCollectionFromIndexes_FiniteIndexes()
{
// Arrange
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
Mock<IModelBinder> mockIntBinder = new Mock<IModelBinder>();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ValueProvider = new SimpleHttpValueProvider
{
{ "someName[foo]", "42" },
{ "someName[baz]", "200" }
}
};
HttpActionContext context = ContextUtil.CreateActionContext();
context.ControllerContext.Configuration.Services.Replace(typeof(ModelBinderProvider), new SimpleModelBinderProvider(typeof(int), mockIntBinder.Object));
mockIntBinder
.Setup(o => o.BindModel(context, It.IsAny<ModelBindingContext>()))
.Returns((HttpActionContext ec, ModelBindingContext mbc) =>
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(context, bindingContext, new[] { "foo", "bar", "baz" });
// Assert
Assert.Equal(new[] { 42, 0, 200 }, boundCollection.ToArray());
Assert.Equal(new[] { "someName[foo]", "someName[baz]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
}
[Fact]
public void BindComplexCollectionFromIndexes_InfiniteIndexes()
{
// Arrange
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
Mock<IModelBinder> mockIntBinder = new Mock<IModelBinder>();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ValueProvider = new SimpleHttpValueProvider
{
{ "someName[0]", "42" },
{ "someName[1]", "100" },
{ "someName[3]", "400" }
}
};
HttpActionContext context = ContextUtil.CreateActionContext();
context.ControllerContext.Configuration.Services.Replace(typeof(ModelBinderProvider), new SimpleModelBinderProvider(typeof(int), mockIntBinder.Object));
mockIntBinder
.Setup(o => o.BindModel(context, It.IsAny<ModelBindingContext>()))
.Returns((HttpActionContext ec, ModelBindingContext mbc) =>
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(context, bindingContext, null /* indexNames */);
// Assert
Assert.Equal(new[] { 42, 100 }, boundCollection.ToArray());
Assert.Equal(new[] { "someName[0]", "someName[1]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
}
[Fact]
public void BindModel_ComplexCollection()
{
// Arrange
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
Mock<IModelBinder> mockIntBinder = new Mock<IModelBinder>();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ValueProvider = new SimpleHttpValueProvider
{
{ "someName.index", new[] { "foo", "bar", "baz" } },
{ "someName[foo]", "42" },
{ "someName[bar]", "100" },
{ "someName[baz]", "200" }
}
};
HttpActionContext context = ContextUtil.CreateActionContext();
context.ControllerContext.Configuration.Services.Replace(typeof(ModelBinderProvider), new SimpleModelBinderProvider(typeof(int), mockIntBinder.Object));
mockIntBinder
.Setup(o => o.BindModel(context, It.IsAny<ModelBindingContext>()))
.Returns((HttpActionContext ec, ModelBindingContext mbc) =>
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
CollectionModelBinder<int> modelBinder = new CollectionModelBinder<int>();
// Act
bool retVal = modelBinder.BindModel(context, bindingContext);
// Assert
Assert.Equal(new[] { 42, 100, 200 }, ((List<int>)bindingContext.Model).ToArray());
}
[Fact]
public void BindModel_SimpleCollection()
{
// Arrange
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
Mock<IModelBinder> mockIntBinder = new Mock<IModelBinder>();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ValueProvider = new SimpleHttpValueProvider
{
{ "someName", new[] { "42", "100", "200" } }
}
};
HttpActionContext context = ContextUtil.CreateActionContext();
context.ControllerContext.Configuration.Services.Replace(typeof(ModelBinderProvider), new SimpleModelBinderProvider(typeof(int), mockIntBinder.Object));
mockIntBinder
.Setup(o => o.BindModel(context, It.IsAny<ModelBindingContext>()))
.Returns((HttpActionContext ec, ModelBindingContext mbc) =>
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
CollectionModelBinder<int> modelBinder = new CollectionModelBinder<int>();
// Act
bool retVal = modelBinder.BindModel(context, bindingContext);
// Assert
Assert.True(retVal);
Assert.Equal(new[] { 42, 100, 200 }, ((List<int>)bindingContext.Model).ToArray());
}
[Fact]
public void BindSimpleCollection_RawValueIsEmptyCollection_ReturnsEmptyList()
{
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(null, null, new object[0], null);
// Assert
Assert.NotNull(boundCollection);
Assert.Empty(boundCollection);
}
[Fact]
public void BindSimpleCollection_RawValueIsNull_ReturnsNull()
{
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(null, null, null, null);
// Assert
Assert.Null(boundCollection);
}
[Fact]
public void BindSimpleCollection_SubBindingSucceeds()
{
// Arrange
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
Mock<IModelBinder> mockIntBinder = new Mock<IModelBinder>();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ValueProvider = new SimpleHttpValueProvider()
};
HttpActionContext context = ContextUtil.CreateActionContext();
context.ControllerContext.Configuration.Services.Replace(typeof(ModelBinderProvider), new SimpleModelBinderProvider(typeof(int), mockIntBinder.Object));
ModelValidationNode childValidationNode = null;
mockIntBinder
.Setup(o => o.BindModel(context, It.IsAny<ModelBindingContext>()))
.Returns((HttpActionContext ec, ModelBindingContext mbc) =>
{
Assert.Equal("someName", mbc.ModelName);
childValidationNode = mbc.ValidationNode;
mbc.Model = 42;
return true;
});
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(context, bindingContext, new int[1], culture);
// Assert
Assert.Equal(new[] { 42 }, boundCollection.ToArray());
Assert.Equal(new[] { childValidationNode }, bindingContext.ValidationNode.ChildNodes.ToArray());
}
[Fact]
public void BindingUnboundedCollection_WhenNoValuesArePresent_ProducesSingleEntryCollection()
{
// Arrange
string propertyName = "Addresses";
ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(
modelAccessor: null,
containerType: typeof(UserWithAddress),
propertyName: propertyName);
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = modelMetadata,
ModelName = propertyName,
ValueProvider = new SimpleHttpValueProvider { { propertyName, "some value" } }
};
HttpActionContext context = ContextUtil.CreateActionContext();
CollectionModelBinder<UserWithAddress> binder = new CollectionModelBinder<UserWithAddress>();
// Act
bool result = binder.BindModel(context, bindingContext);
// Assert
Assert.True(result);
List<UserWithAddress> boundModel = Assert.IsType<List<UserWithAddress>>(bindingContext.Model);
UserWithAddress listModel = Assert.Single(boundModel);
Assert.Null(listModel.Addresses);
}
[Fact]
public void BindingNestedUnboundedCollection_WhenNoValuesArePresent_ProducesEmptyCollection()
{
// Arrange
string propertyName = "Addresses";
ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(
modelAccessor: null,
containerType: typeof(UserWithAddress),
propertyName: propertyName);
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = modelMetadata,
ModelName = propertyName,
ValueProvider = new SimpleHttpValueProvider { { "Addresses.AddressLines", "some value" } }
};
HttpActionContext context = ContextUtil.CreateActionContext();
CollectionModelBinder<UserWithAddress> binder = new CollectionModelBinder<UserWithAddress>();
// Act
bool result = binder.BindModel(context, bindingContext);
// Assert
Assert.True(result);
List<UserWithAddress> boundModel = Assert.IsType<List<UserWithAddress>>(bindingContext.Model);
Assert.Empty(boundModel);
}
[Fact]
public void BindingListsWithIndex_ProducesSingleLengthCollection_WithNullValues()
{
// Arrange
string propertyName = "Addresses";
ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(
modelAccessor: null,
containerType: typeof(UserWithAddress),
propertyName: propertyName);
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = modelMetadata,
ModelName = propertyName,
ValueProvider = new SimpleHttpValueProvider { { "Addresses.index", "10000" } }
};
HttpActionContext context = ContextUtil.CreateActionContext();
CollectionModelBinder<UserWithAddress> binder = new CollectionModelBinder<UserWithAddress>();
// Act
bool result = binder.BindModel(context, bindingContext);
// Assert
Assert.True(result);
List<UserWithAddress> boundModel = Assert.IsType<List<UserWithAddress>>(bindingContext.Model);
UserWithAddress listModel = Assert.Single(boundModel);
Assert.Null(listModel);
}
[Fact]
public void BindingUnboundedCollection_WithRecursiveRelation_ProducesSingleLengthCollection()
{
// Arrange
string propertyName = "People";
ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(
modelAccessor: null,
containerType: typeof(PeopleModel),
propertyName: propertyName);
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = modelMetadata,
ModelName = propertyName,
ValueProvider = new SimpleHttpValueProvider { { propertyName, "test value" } }
};
HttpActionContext context = ContextUtil.CreateActionContext();
CollectionModelBinder<Person> binder =
new CollectionModelBinder<Person>();
// Act
bool result = binder.BindModel(context, bindingContext);
// Assert
Assert.True(result);
List<Person> boundModel = Assert.IsType<List<Person>>(bindingContext.Model);
Person type = Assert.Single(boundModel);
Assert.Null(type.Name);
}
}
}