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
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Http.Controllers;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Http
{
public class ReflectedHttpParameterDescriptorTest
{
[Fact]
public void Parameter_Constructor()
{
UsersRpcController controller = new UsersRpcController();
Func<string, string, User> echoUserMethod = controller.EchoUser;
ReflectedHttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor { MethodInfo = echoUserMethod.Method };
ParameterInfo parameterInfo = echoUserMethod.Method.GetParameters()[0];
ReflectedHttpParameterDescriptor parameterDescriptor = new ReflectedHttpParameterDescriptor(actionDescriptor, parameterInfo);
Assert.Equal(actionDescriptor, parameterDescriptor.ActionDescriptor);
Assert.Null(parameterDescriptor.DefaultValue);
Assert.Equal(parameterInfo, parameterDescriptor.ParameterInfo);
Assert.Equal(parameterInfo.Name, parameterDescriptor.ParameterName);
Assert.Equal(typeof(string), parameterDescriptor.ParameterType);
Assert.Null(parameterDescriptor.Prefix);
Assert.Null(parameterDescriptor.ModelBinderAttribute);
}
[Fact]
public void Constructor_Throws_IfParameterInfoIsNull()
{
Assert.ThrowsArgumentNull(
() => new ReflectedHttpParameterDescriptor(new Mock<HttpActionDescriptor>().Object, null),
"parameterInfo");
}
[Fact]
public void Constructor_Throws_IfActionDescriptorIsNull()
{
Assert.ThrowsArgumentNull(
() => new ReflectedHttpParameterDescriptor(null, new Mock<ParameterInfo>().Object),
"actionDescriptor");
}
[Fact]
public void ParameterInfo_Property()
{
ParameterInfo referenceParameter = new Mock<ParameterInfo>().Object;
Assert.Reflection.Property(new ReflectedHttpParameterDescriptor(), d => d.ParameterInfo, expectedDefaultValue: null, allowNull: false, roundTripTestValue: referenceParameter);
}
[Fact]
public void IsDefined_Retruns_True_WhenParameterAttributeIsFound()
{
UsersRpcController controller = new UsersRpcController();
Action<User> addUserMethod = controller.AddUser;
ReflectedHttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor { MethodInfo = addUserMethod.Method };
ParameterInfo parameterInfo = addUserMethod.Method.GetParameters()[0];
ReflectedHttpParameterDescriptor parameterDescriptor = new ReflectedHttpParameterDescriptor(actionDescriptor, parameterInfo);
}
[Fact]
public void GetCustomAttributes_Returns_ParameterAttributes()
{
UsersRpcController controller = new UsersRpcController();
Action<User> addUserMethod = controller.AddUser;
ReflectedHttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor { MethodInfo = addUserMethod.Method };
ParameterInfo parameterInfo = addUserMethod.Method.GetParameters()[0];
ReflectedHttpParameterDescriptor parameterDescriptor = new ReflectedHttpParameterDescriptor(actionDescriptor, parameterInfo);
object[] attributes = parameterDescriptor.GetCustomAttributes<object>().ToArray();
Assert.Equal(1, attributes.Length);
Assert.Equal(typeof(FromBodyAttribute), attributes[0].GetType());
}
[Fact]
public void GetCustomAttributes_AttributeType_Returns_ParameterAttributes()
{
UsersRpcController controller = new UsersRpcController();
Action<User> addUserMethod = controller.AddUser;
ReflectedHttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor { MethodInfo = addUserMethod.Method };
ParameterInfo parameterInfo = addUserMethod.Method.GetParameters()[0];
ReflectedHttpParameterDescriptor parameterDescriptor = new ReflectedHttpParameterDescriptor(actionDescriptor, parameterInfo);
IEnumerable<FromBodyAttribute> attributes = parameterDescriptor.GetCustomAttributes<FromBodyAttribute>();
Assert.Equal(1, attributes.Count());
}
}
}
|