-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
ActionEndpointDataSourceBaseTest.cs
171 lines (142 loc) · 6.74 KB
/
ActionEndpointDataSourceBaseTest.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
// 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.Collections.Generic;
using System.Threading;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Routing
{
// There are some basic sanity tests here for the details of how actions
// are turned into endpoints. See ActionEndpointFactoryTest for detailed tests.
public abstract class ActionEndpointDataSourceBaseTest
{
[Fact]
public void Endpoints_AccessParameters_InitializedFromProvider()
{
// Arrange
var actions = new Mock<IActionDescriptorCollectionProvider>();
actions.Setup(m => m.ActionDescriptors).Returns(new ActionDescriptorCollection(new List<ActionDescriptor>
{
CreateActionDescriptor(new { Name = "Value", }, "/Template!"),
}, 0));
var dataSource = CreateDataSource(actions.Object);
// Act
var endpoints = dataSource.Endpoints;
// Assert
var endpoint = Assert.IsType<RouteEndpoint>(Assert.Single(endpoints));
Assert.Equal("Value", endpoint.RoutePattern.RequiredValues["Name"]);
Assert.Equal("/Template!", endpoint.RoutePattern.RawText);
}
[Fact]
public void Endpoints_CalledMultipleTimes_ReturnsSameInstance()
{
// Arrange
var actionDescriptorCollectionProviderMock = new Mock<IActionDescriptorCollectionProvider>();
actionDescriptorCollectionProviderMock
.Setup(m => m.ActionDescriptors)
.Returns(new ActionDescriptorCollection(new[]
{
CreateActionDescriptor(new { controller = "TestController", action = "TestAction" }, "/test"),
}, version: 0));
var dataSource = CreateDataSource(actionDescriptorCollectionProviderMock.Object);
// Act
var endpoints1 = dataSource.Endpoints;
var endpoints2 = dataSource.Endpoints;
// Assert
Assert.Collection(
endpoints1,
(e) => Assert.Equal("/test", Assert.IsType<RouteEndpoint>(e).RoutePattern.RawText));
Assert.Same(endpoints1, endpoints2);
actionDescriptorCollectionProviderMock.VerifyGet(m => m.ActionDescriptors, Times.Once);
}
[Fact]
public void Endpoints_ChangeTokenTriggered_EndpointsRecreated()
{
// Arrange
var actionDescriptorCollectionProviderMock = new Mock<ActionDescriptorCollectionProvider>();
actionDescriptorCollectionProviderMock
.Setup(m => m.ActionDescriptors)
.Returns(new ActionDescriptorCollection(new[]
{
CreateActionDescriptor(new { controller = "TestController", action = "TestAction" }, "/test")
}, version: 0));
CancellationTokenSource cts = null;
actionDescriptorCollectionProviderMock
.Setup(m => m.GetChangeToken())
.Returns(() =>
{
cts = new CancellationTokenSource();
var changeToken = new CancellationChangeToken(cts.Token);
return changeToken;
});
var dataSource = CreateDataSource(actionDescriptorCollectionProviderMock.Object);
// Act
var endpoints = dataSource.Endpoints;
Assert.Collection(endpoints,
(e) =>
{
var routePattern = Assert.IsType<RouteEndpoint>(e).RoutePattern;
Assert.Equal("/test", routePattern.RawText);
Assert.Equal("TestController", routePattern.RequiredValues["controller"]);
Assert.Equal("TestAction", routePattern.RequiredValues["action"]);
});
actionDescriptorCollectionProviderMock
.Setup(m => m.ActionDescriptors)
.Returns(new ActionDescriptorCollection(new[]
{
CreateActionDescriptor(new { controller = "NewTestController", action = "NewTestAction" }, "/test")
}, version: 1));
cts.Cancel();
// Assert
var newEndpoints = dataSource.Endpoints;
Assert.NotSame(endpoints, newEndpoints);
Assert.Collection(newEndpoints,
(e) =>
{
var routePattern = Assert.IsType<RouteEndpoint>(e).RoutePattern;
Assert.Equal("/test", routePattern.RawText);
Assert.Equal("NewTestController", routePattern.RequiredValues["controller"]);
Assert.Equal("NewTestAction", routePattern.RequiredValues["action"]);
});
}
protected private ActionEndpointDataSourceBase CreateDataSource(IActionDescriptorCollectionProvider actions = null)
{
if (actions == null)
{
actions = new DefaultActionDescriptorCollectionProvider(
Array.Empty<IActionDescriptorProvider>(),
Array.Empty<IActionDescriptorChangeProvider>());
}
var services = new ServiceCollection();
services.AddSingleton(actions);
var routeOptionsSetup = new MvcCoreRouteOptionsSetup();
services.Configure<RouteOptions>(routeOptionsSetup.Configure);
services.AddRouting(options =>
{
options.ConstraintMap["upper-case"] = typeof(UpperCaseParameterTransform);
});
var serviceProvider = services.BuildServiceProvider();
var endpointFactory = new ActionEndpointFactory(serviceProvider.GetRequiredService<RoutePatternTransformer>());
return CreateDataSource(actions, endpointFactory);
}
protected private abstract ActionEndpointDataSourceBase CreateDataSource(IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory);
private class UpperCaseParameterTransform : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
return value?.ToString().ToUpperInvariant();
}
}
protected abstract ActionDescriptor CreateActionDescriptor(
object values,
string pattern = null,
IList<object> metadata = null);
}
}