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
|
// Copyright (c) Microsoft Corporation. All rights reserved. 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.Providers;
using System.Web.Http.ModelBinding.Binders;
using System.Web.Http.Util;
using System.Web.Http.Validation;
using Moq;
using Xunit;
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(Skip = "is this test checking a valid invariant?")]
public void BindSimpleCollection_SubBinderDoesNotExist()
{
// Arrange
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
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), null); // completely remove from resolution?
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(context, bindingContext, new int[1], culture);
// Assert
Assert.Equal(new[] { 0 }, boundCollection.ToArray());
Assert.Empty(bindingContext.ValidationNode.ChildNodes);
}
[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());
}
}
}
|