This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
ApiBehaviorApplicationModelProviderTest.cs
191 lines (160 loc) · 7.97 KB
/
ApiBehaviorApplicationModelProviderTest.cs
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
// 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;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.ApplicationModels
{
public class ApiBehaviorApplicationModelProviderTest
{
[Fact]
public void OnProvidersExecuting_ThrowsIfControllerWithAttribute_HasActionsWithoutAttributeRouting()
{
// Arrange
var actionName = $"{typeof(TestApiController).FullName}.{nameof(TestApiController.TestAction)} ({typeof(TestApiController).Assembly.GetName().Name})";
var expected = $"Action '{actionName}' does not have an attribute route. Action methods on controllers annotated with ApiControllerAttribute must be attribute routed.";
var controllerModel = new ControllerModel(typeof(TestApiController).GetTypeInfo(), new[] { new ApiControllerAttribute() });
var method = typeof(TestApiController).GetMethod(nameof(TestApiController.TestAction));
var actionModel = new ActionModel(method, Array.Empty<object>())
{
Controller = controllerModel,
};
controllerModel.Actions.Add(actionModel);
var context = new ApplicationModelProviderContext(new[] { controllerModel.ControllerType });
context.Result.Controllers.Add(controllerModel);
var provider = GetProvider();
// Act & Assert
var ex = Assert.Throws<InvalidOperationException>(() => provider.OnProvidersExecuting(context));
Assert.Equal(expected, ex.Message);
}
[Fact]
public void OnProvidersExecuting_AppliesConventions()
{
// Arrange
var controllerModel = new ControllerModel(typeof(TestApiController).GetTypeInfo(), new[] { new ApiControllerAttribute() })
{
Selectors = { new SelectorModel { AttributeRouteModel = new AttributeRouteModel() } },
};
var method = typeof(TestApiController).GetMethod(nameof(TestApiController.TestAction));
var actionModel = new ActionModel(method, Array.Empty<object>())
{
Controller = controllerModel,
};
controllerModel.Actions.Add(actionModel);
var parameter = method.GetParameters()[0];
var parameterModel = new ParameterModel(parameter, Array.Empty<object>())
{
Action = actionModel,
};
actionModel.Parameters.Add(parameterModel);
var context = new ApplicationModelProviderContext(new[] { controllerModel.ControllerType });
context.Result.Controllers.Add(controllerModel);
var provider = GetProvider();
// Act
provider.OnProvidersExecuting(context);
// Assert
// Verify some of the side-effects of executing API behavior conventions.
Assert.True(actionModel.ApiExplorer.IsVisible);
Assert.NotEmpty(actionModel.Filters.OfType<ModelStateInvalidFilterFactory>());
Assert.NotEmpty(actionModel.Filters.OfType<ClientErrorResultFilterFactory>());
Assert.Equal(BindingSource.Body, parameterModel.BindingInfo.BindingSource);
}
[Fact]
public void Constructor_SetsUpConventions()
{
// Arrange
var provider = GetProvider();
// Act & Assert
Assert.Collection(
provider.ActionModelConventions,
c => Assert.IsType<ApiVisibilityConvention>(c),
c => Assert.IsType<ClientErrorResultFilterConvention>(c),
c => Assert.IsType<InvalidModelStateFilterConvention>(c),
c => Assert.IsType<ConsumesConstraintForFormFileParameterConvention>(c),
c =>
{
var convention = Assert.IsType<ApiConventionApplicationModelConvention>(c);
Assert.Equal(typeof(ProblemDetails), convention.DefaultErrorResponseType.Type);
},
c => Assert.IsType<InferParameterBindingInfoConvention>(c));
}
[Fact]
public void Constructor_DoesNotAddClientErrorResultFilterConvention_IfSuppressMapClientErrorsIsSet()
{
// Arrange
var provider = GetProvider(new ApiBehaviorOptions { SuppressMapClientErrors = true });
// Act & Assert
Assert.Empty(provider.ActionModelConventions.OfType<ClientErrorResultFilterConvention>());
}
[Fact]
public void Constructor_DoesNotAddInvalidModelStateFilterConvention_IfSuppressModelStateInvalidFilterIsSet()
{
// Arrange
var provider = GetProvider(new ApiBehaviorOptions { SuppressModelStateInvalidFilter = true });
// Act & Assert
Assert.Empty(provider.ActionModelConventions.OfType<InvalidModelStateFilterConvention>());
}
[Fact]
public void Constructor_DoesNotAddConsumesConstraintForFormFileParameterConvention_IfSuppressConsumesConstraintForFormFileParametersIsSet()
{
// Arrange
var provider = GetProvider(new ApiBehaviorOptions { SuppressConsumesConstraintForFormFileParameters = true });
// Act & Assert
Assert.Empty(provider.ActionModelConventions.OfType<ConsumesConstraintForFormFileParameterConvention>());
}
[Fact]
public void Constructor_DoesNotAddInferParameterBindingInfoConvention_IfSuppressInferBindingSourcesForParametersIsSet()
{
// Arrange
var provider = GetProvider(new ApiBehaviorOptions { SuppressInferBindingSourcesForParameters = true });
// Act & Assert
Assert.Empty(provider.ActionModelConventions.OfType<InferParameterBindingInfoConvention>());
}
[Fact]
public void Constructor_DoesNotSpecifyDefaultErrorType_IfSuppressMapClientErrorsIsSet()
{
// Arrange
var provider = GetProvider(new ApiBehaviorOptions { SuppressMapClientErrors = true });
// Act & Assert
var convention = Assert.Single(provider.ActionModelConventions.OfType<ApiConventionApplicationModelConvention>());
Assert.Equal(typeof(void), convention.DefaultErrorResponseType.Type);
}
private static ApiBehaviorApplicationModelProvider GetProvider(
ApiBehaviorOptions options = null)
{
options = options ?? new ApiBehaviorOptions
{
InvalidModelStateResponseFactory = _ => null,
};
var optionsAccessor = Options.Create(options);
var loggerFactory = NullLoggerFactory.Instance;
return new ApiBehaviorApplicationModelProvider(
optionsAccessor,
new EmptyModelMetadataProvider(),
Mock.Of<IClientErrorFactory>(),
loggerFactory);
}
private static ApplicationModelProviderContext GetContext(
Type type,
IModelMetadataProvider modelMetadataProvider = null)
{
var context = new ApplicationModelProviderContext(new[] { type.GetTypeInfo() });
var mvcOptions = Options.Create(new MvcOptions { AllowValidatingTopLevelNodes = true });
modelMetadataProvider = modelMetadataProvider ?? new EmptyModelMetadataProvider();
var provider = new DefaultApplicationModelProvider(mvcOptions, modelMetadataProvider);
provider.OnProvidersExecuting(context);
return context;
}
private class TestApiController : ControllerBase
{
public IActionResult TestAction(object value) => null;
}
}
}