forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayModelBinderTest.cs
More file actions
96 lines (82 loc) · 3.46 KB
/
Copy pathArrayModelBinderTest.cs
File metadata and controls
96 lines (82 loc) · 3.46 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
// 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.Web.Http.Controllers;
using System.Web.Http.Metadata.Providers;
using System.Web.Http.Util;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ModelBinding.Binders
{
public class ArrayModelBinderTest
{
[Fact]
public void BindModel()
{
// Arrange
Mock<IModelBinder> mockIntBinder = new Mock<IModelBinder>();
HttpActionContext context = ContextUtil.CreateActionContext();
context.ControllerContext.Configuration.Services.Replace(typeof(ModelBinderProvider), new SimpleModelBinderProvider(typeof(int), mockIntBinder.Object));
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])),
ModelName = "someName",
ValueProvider = new SimpleHttpValueProvider
{
{ "someName[0]", "42" },
{ "someName[1]", "84" }
}
};
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
bool retVal = new ArrayModelBinder<int>().BindModel(context, bindingContext);
// Assert
Assert.True(retVal);
int[] array = bindingContext.Model as int[];
Assert.Equal(new[] { 42, 84 }, array);
}
[Fact]
public void GetBinder_ValueProviderDoesNotContainPrefix_ReturnsNull()
{
// Arrange
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])),
ModelName = "foo",
ValueProvider = new SimpleHttpValueProvider()
};
ArrayModelBinderProvider binderProvider = new ArrayModelBinderProvider();
// Act
IModelBinder binder = binderProvider.GetBinder(null, bindingContext.ModelType);
bool bound = binder.BindModel(null, bindingContext);
// Assert
Assert.False(bound);
}
[Fact]
public void GetBinder_ModelMetadataReturnsReadOnly_ReturnsNull()
{
// Arrange
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])),
ModelName = "foo",
ValueProvider = new SimpleHttpValueProvider
{
{ "foo[0]", "42" },
}
};
bindingContext.ModelMetadata.IsReadOnly = true;
ArrayModelBinderProvider binderProvider = new ArrayModelBinderProvider();
// Act
IModelBinder binder = binderProvider.GetBinder(null, bindingContext.ModelType);
bool bound = binder.BindModel(null, bindingContext);
// Assert
Assert.False(bound);
}
}
}