|
7 | 7 | using JsonApiDotNetCore.Resources.Annotations;
|
8 | 8 | using Microsoft.AspNetCore.Http;
|
9 | 9 | using Microsoft.AspNetCore.Http.Features;
|
| 10 | +using Microsoft.AspNetCore.Mvc.ApplicationModels; |
10 | 11 | using Microsoft.AspNetCore.Mvc.Controllers;
|
11 | 12 | using Microsoft.Extensions.Logging.Abstractions;
|
12 |
| -using Moq; |
13 | 13 | using TestBuildingBlocks;
|
14 | 14 | using Xunit;
|
15 | 15 |
|
16 | 16 | #pragma warning disable AV1561 // Signature contains too many parameters
|
17 | 17 |
|
18 |
| -namespace UnitTests.Middleware; |
| 18 | +namespace JsonApiDotNetCoreTests.UnitTests.Middleware; |
19 | 19 |
|
20 |
| -public sealed class JsonApiRequestTests |
| 20 | +public sealed class JsonApiMiddlewareTests |
21 | 21 | {
|
22 | 22 | // @formatter:wrap_lines false
|
23 | 23 | [Theory]
|
@@ -65,7 +65,7 @@ public async Task Sets_request_properties_correctly(string requestMethod, string
|
65 | 65 | var httpContext = new DefaultHttpContext();
|
66 | 66 | IControllerResourceMapping controllerResourceMapping = SetupRoutes(httpContext, resourceGraph, requestMethod, requestPath);
|
67 | 67 |
|
68 |
| - var middleware = new JsonApiMiddleware(_ => Task.CompletedTask, new HttpContextAccessor |
| 68 | + var middleware = new JsonApiMiddleware(null, new HttpContextAccessor |
69 | 69 | {
|
70 | 70 | HttpContext = httpContext
|
71 | 71 | });
|
@@ -153,16 +153,10 @@ private static IControllerResourceMapping SetupRoutes(HttpContext httpContext, I
|
153 | 153 | ControllerTypeInfo = (TypeInfo)typeof(object)
|
154 | 154 | };
|
155 | 155 |
|
156 |
| - var controllerResourceMappingMock = new Mock<IControllerResourceMapping>(); |
| 156 | + httpContext.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection(controllerActionDescriptor), null)); |
157 | 157 |
|
158 |
| - controllerResourceMappingMock.Setup(mapping => mapping.GetResourceTypeForController(It.IsAny<Type>())).Returns(() => |
159 |
| - { |
160 |
| - return pathSegments.Length > 0 ? resourceGraph.GetResourceTypes().FirstOrDefault(resourceType => resourceType.PublicName == pathSegments[0]) : null; |
161 |
| - }); |
162 |
| - |
163 |
| - httpContext.SetEndpoint(new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(controllerActionDescriptor), null)); |
164 |
| - |
165 |
| - return controllerResourceMappingMock.Object; |
| 158 | + string? resourceTypePublicName = pathSegments.Length > 0 ? pathSegments[0] : null; |
| 159 | + return new FakeJsonApiRoutingConvention(resourceGraph, resourceTypePublicName); |
166 | 160 | }
|
167 | 161 |
|
168 | 162 | public enum IsReadOnly
|
@@ -198,4 +192,31 @@ private sealed class TodoItem : Identifiable<int>
|
198 | 192 | [HasMany]
|
199 | 193 | public ISet<ItemTag> Tags { get; set; } = new HashSet<ItemTag>();
|
200 | 194 | }
|
| 195 | + |
| 196 | + private sealed class FakeJsonApiRoutingConvention : IJsonApiRoutingConvention |
| 197 | + { |
| 198 | + private readonly IResourceGraph _resourceGraph; |
| 199 | + private readonly string? _resourceTypePublicName; |
| 200 | + |
| 201 | + public FakeJsonApiRoutingConvention(IResourceGraph resourceGraph, string? resourceTypePublicName) |
| 202 | + { |
| 203 | + _resourceGraph = resourceGraph; |
| 204 | + _resourceTypePublicName = resourceTypePublicName; |
| 205 | + } |
| 206 | + |
| 207 | + public void Apply(ApplicationModel application) |
| 208 | + { |
| 209 | + throw new NotImplementedException(); |
| 210 | + } |
| 211 | + |
| 212 | + public ResourceType? GetResourceTypeForController(Type? controllerType) |
| 213 | + { |
| 214 | + return _resourceTypePublicName != null ? _resourceGraph.FindResourceType(_resourceTypePublicName) : null; |
| 215 | + } |
| 216 | + |
| 217 | + public string GetControllerNameForResourceType(ResourceType? resourceType) |
| 218 | + { |
| 219 | + throw new NotImplementedException(); |
| 220 | + } |
| 221 | + } |
201 | 222 | }
|
0 commit comments