forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterBindingProvidersTest.cs
More file actions
139 lines (114 loc) · 5.88 KB
/
Copy pathParameterBindingProvidersTest.cs
File metadata and controls
139 lines (114 loc) · 5.88 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
// 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.ObjectModel;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ModelBinding
{
public class ParameterBindingProvidersTest
{
[Fact]
public void AsCollection()
{
ParameterBindingRulesCollection pb = new ParameterBindingRulesCollection();
// Knowing that it's a collection means we have known behavior around
// the collection methods (like insert, add, clear, etc).
Assert.True(pb is Collection<Func<HttpParameterDescriptor, HttpParameterBinding>>);
}
[Fact]
public void Lookup_empty_collection_returns_null()
{
// The collection is empty, so lookup will fail.
ParameterBindingRulesCollection pb = new ParameterBindingRulesCollection();
HttpParameterDescriptor parameter = CreateParameterDescriptor(typeof(object), "none");
// Act
HttpParameterBinding binding = pb.LookupBinding(parameter);
// Assert.
Assert.Null(binding);
}
[Fact]
public void Lookup_is_ordered()
{
ParameterBindingRulesCollection pb = new ParameterBindingRulesCollection();
HttpParameterBinding mockBinding1 = new EmptyParameterBinding();
HttpParameterBinding mockBinding2 = new EmptyParameterBinding();
HttpParameterBinding mockBinding3 = new EmptyParameterBinding();
pb.Add(param => param.ParameterName == "first" ? mockBinding1 : null);
pb.Add(param => param.ParameterName == "first" ? mockBinding2 : null);
pb.Add(param => param.ParameterType == typeof(int) ? mockBinding3 : null);
// Act
HttpParameterBinding b1 = pb.LookupBinding(CreateParameterDescriptor(null, "first"));
HttpParameterBinding b2 = pb.LookupBinding(CreateParameterDescriptor(typeof(string), "none"));
HttpParameterBinding b3 = pb.LookupBinding(CreateParameterDescriptor(typeof(int), "first"));
HttpParameterBinding b4 = pb.LookupBinding(CreateParameterDescriptor(typeof(int), "last"));
// Assert
Assert.Equal(mockBinding1, b1);
Assert.Null(b2);
Assert.Equal(mockBinding1, b3);
Assert.Equal(mockBinding3, b4);
}
[Fact]
public void Add_with_type_match()
{
ParameterBindingRulesCollection pb = new ParameterBindingRulesCollection();
HttpParameterBinding mockBinding = new EmptyParameterBinding();
pb.Add(typeof(string), param => mockBinding);
// Act
HttpParameterBinding b1 = pb.LookupBinding(CreateParameterDescriptor(typeof(string), "first"));
HttpParameterBinding b2 = pb.LookupBinding(CreateParameterDescriptor(typeof(int), "first"));
// Assert
Assert.Equal(mockBinding, b1);
Assert.Null(b2); // doesn't match type, misses.
}
[Fact]
public void type_match_user_function_not_invoked_if_type_doesnt_match()
{
ParameterBindingRulesCollection pb = new ParameterBindingRulesCollection();
HttpParameterBinding mockBinding = new EmptyParameterBinding();
pb.Add(typeof(string), param => { throw new InvalidOperationException("shouldn't be called"); });
pb.Insert(0, typeof(string), param => { throw new InvalidOperationException("shouldn't be called"); });
// Act
HttpParameterBinding b2 = pb.LookupBinding(CreateParameterDescriptor(typeof(int), "first"));
// Assert - made it through the action without throwing.
}
[Fact]
public void Insert_with_type_match()
{
ParameterBindingRulesCollection pb = new ParameterBindingRulesCollection();
HttpParameterBinding mockBinding1 = new EmptyParameterBinding();
HttpParameterBinding mockBinding2 = new EmptyParameterBinding();
HttpParameterBinding mockBinding3 = new EmptyParameterBinding();
// Act, test insertion
pb.Add(typeof(string), param => mockBinding2);
pb.Add(typeof(int), param => mockBinding2);
pb.Insert(0, typeof(string), param => mockBinding1);
pb.Insert(2, typeof(int), param => mockBinding3); // goes in middle
// Assert via lookups
Assert.Equal(mockBinding1, pb.LookupBinding(CreateParameterDescriptor(typeof(string), "first")));
Assert.Equal(mockBinding3, pb.LookupBinding(CreateParameterDescriptor(typeof(int), "first")));
}
// For unit testing purposes, just create a unique binding objet.
// We'll just compare object reference identity to determine if binds give the expected binding back.
// ParameterDescriptor is ignored.
private class EmptyParameterBinding : HttpParameterBinding
{
public EmptyParameterBinding()
: base(CreateParameterDescriptor(typeof(object), "dummy"))
{
}
public override Threading.Tasks.Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, Threading.CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
private static HttpParameterDescriptor CreateParameterDescriptor(Type type, string name)
{
Mock<HttpParameterDescriptor> mock = new Mock<HttpParameterDescriptor>();
mock.Setup(p => p.ParameterType).Returns(type);
mock.Setup(p => p.ParameterName).Returns(name);
return mock.Object;
}
}
}