forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpParameterBindingTest.cs
More file actions
153 lines (116 loc) · 4.51 KB
/
Copy pathHttpParameterBindingTest.cs
File metadata and controls
153 lines (116 loc) · 4.51 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
// 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 Microsoft.TestCommon;
using Moq;
namespace System.Web.Http
{
public class HttpParameterBindingTest
{
[Fact]
public void GetValueMissing()
{
CustomBinding binding = new CustomBinding("p1");
HttpActionContext ctx = new HttpActionContext();
var result = binding.GetValue(ctx);
Assert.Null(result);
}
[Fact]
public void GetValue_Returns_Set()
{
CustomBinding binding = new CustomBinding("p1");
HttpActionContext ctx = new HttpActionContext();
// Act
object result = "abc";
binding.SetValue(ctx, result);
var result2 = binding.GetValue(ctx);
// Assert
Assert.Same(result, result2);
}
[Fact]
public void Can_Set_Null()
{
// It's legal to set a parameter to null. Test against spurious null checks.
CustomBinding binding = new CustomBinding("p1");
HttpActionContext ctx = new HttpActionContext();
// Act
binding.SetValue(ctx, null);
var resultFinal = binding.GetValue(ctx);
// Assert
Assert.Null(resultFinal);
}
[Fact]
public void Call_Set_Multiple_Times()
{
// Make sure a binding can set the argument multiple times and that we get the latest.
// This is interesting with composite bindings that chain to an inner binding.
CustomBinding binding = new CustomBinding("p1");
HttpActionContext ctx = new HttpActionContext();
// Act
object result1 = "abc";
binding.SetValue(ctx, result1);
object result2 = 123;
binding.SetValue(ctx, result2);
var resultFinal = binding.GetValue(ctx);
// Assert
Assert.Same(result2, resultFinal);
}
[Fact]
public void Set_Modifies_Dictionary()
{
string name = "p1";
CustomBinding binding = new CustomBinding(name);
HttpActionContext ctx = new HttpActionContext();
// Act
object result = "abc";
binding.SetValue(ctx, result);
var result2 = ctx.ActionArguments[name];
// Assert
Assert.Same(result, result2);
}
[Fact]
public void Reuse_Binding_With_Different_Contexts()
{
// The same binding can be used across multiple action contexts.
string name = "p1";
CustomBinding binding = new CustomBinding(name);
HttpActionContext ctx1 = new HttpActionContext();
HttpActionContext ctx2 = new HttpActionContext();
// Act
object result1 = "abc";
binding.SetValue(ctx1, result1);
object result2 = 123;
binding.SetValue(ctx2, result2);
// Assert
Assert.Same(result1, binding.GetValue(ctx1));
Assert.Same(result2, binding.GetValue(ctx2));
}
// Helper for testing.
// Easily construct, mock out the the right things, and expose protected members to the tests.
public class CustomBinding : HttpParameterBinding
{
public CustomBinding(string paramName) : base(Build(paramName))
{
}
static HttpParameterDescriptor Build(string paramName)
{
Mock<HttpParameterDescriptor> mock = new Mock<HttpParameterDescriptor>();
mock.Setup(x => x.ParameterName).Returns(paramName);
return mock.Object;
}
public override Threading.Tasks.Task ExecuteBindingAsync(Metadata.ModelMetadataProvider metadataProvider, HttpActionContext actionContext, Threading.CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
// Expose protected
public new object GetValue(HttpActionContext actionContext)
{
return base.GetValue(actionContext);
}
public new void SetValue(HttpActionContext actionContext, object value)
{
base.SetValue(actionContext, value);
}
}
}
}