private InvokeConfiguration GetConfiguration(string path, string resourceName = "users", string action = "", string id = null, Type relType = null) { if (path.First() != '/') { throw new ArgumentException("Path should start with a '/'"); } var middleware = new JsonApiMiddleware(httpContext => { return(Task.Run(() => Console.WriteLine("finished"))); }); var forcedNamespace = "api/v1"; var mockMapping = new Mock <IControllerResourceMapping>(); Mock <IJsonApiOptions> mockOptions = CreateMockOptions(forcedNamespace); var mockGraph = CreateMockResourceGraph(resourceName, includeRelationship: relType != null); var currentRequest = new CurrentRequest(); if (relType != null) { currentRequest.RequestRelationship = new HasManyAttribute { RightType = relType }; } var context = CreateHttpContext(path, isRelationship: relType != null, action: action, id: id); return(new InvokeConfiguration { MiddleWare = middleware, ControllerResourceMapping = mockMapping, Options = mockOptions, CurrentRequest = currentRequest, HttpContext = context, ResourceGraph = mockGraph }); }
public async Task Sets_request_properties_correctly(string requestMethod, string requestPath, bool expectIsCollection, EndpointKind expectKind, bool expectIsReadOnly) { // Arrange var options = new JsonApiOptions { UseRelativeLinks = true }; var graphBuilder = new ResourceGraphBuilder(options, NullLoggerFactory.Instance); graphBuilder.Add <Article>(); graphBuilder.Add <Author>(); graphBuilder.Add <Revision>(); var resourceGraph = graphBuilder.Build(); var controllerResourceMappingMock = new Mock <IControllerResourceMapping>(); controllerResourceMappingMock .Setup(x => x.GetResourceTypeForController(It.IsAny <string>())) .Returns(typeof(Article)); var httpContext = new DefaultHttpContext(); SetupRoutes(httpContext, requestMethod, requestPath); var request = new JsonApiRequest(); var middleware = new JsonApiMiddleware(_ => Task.CompletedTask); // Act await middleware.Invoke(httpContext, controllerResourceMappingMock.Object, options, request, resourceGraph); // Assert request.IsCollection.Should().Be(expectIsCollection); request.Kind.Should().Be(expectKind); request.IsReadOnly.Should().Be(expectIsReadOnly); request.BasePath.Should().BeEmpty(); request.PrimaryResource.Should().NotBeNull(); request.PrimaryResource.PublicName.Should().Be("articles"); }
private InvokeConfiguration GetConfiguration(string path, string resourceName = "users", string action = "", string id = null, Type relType = null) { if (path.First() != '/') { throw new ArgumentException("Path should start with a '/'"); } var middleware = new JsonApiMiddleware(_ => { return(Task.Run(() => Console.WriteLine("finished"))); }); const string forcedNamespace = "api/v1"; var mockMapping = new Mock <IControllerResourceMapping>(); mockMapping.Setup(mapping => mapping.GetResourceTypeForController(It.IsAny <Type>())).Returns(typeof(string)); Mock <IJsonApiOptions> mockOptions = CreateMockOptions(forcedNamespace); Mock <IResourceGraph> mockGraph = CreateMockResourceGraph(resourceName, relType != null); var request = new JsonApiRequest(); if (relType != null) { request.Relationship = new HasManyAttribute { RightType = relType }; } DefaultHttpContext context = CreateHttpContext(path, relType != null, action, id); return(new InvokeConfiguration { MiddleWare = middleware, ControllerResourceMapping = mockMapping, Options = mockOptions, Request = request, HttpContext = context, ResourceGraph = mockGraph }); }