public void AttributeMappingsInitialization_ThrowsInvalidOperation_IfFailsToParsePathTemplate()
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            HttpControllerDescriptor controller = new HttpControllerDescriptor(new HttpConfiguration(), "TestController",
                typeof(InvalidPathTemplateController));

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model, new[] { controller });

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => convention.SelectController(new ODataPath(), new HttpRequestMessage()),
                "The path template 'Customers/Order' on the action 'GetCustomers' in controller 'TestController' is not " +
                "a valid OData path template. Invalid action detected. 'Order' is not an action that can bind to 'Collec" +
                "tion([NS.Customer Nullable=False])'.");
        }
        public void CtorTakingHttpConfiguration_InitializesAttributeMappings_OnFirstSelectControllerCall()
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock<IODataPathTemplateHandler> pathTemplateHandler = new Mock<IODataPathTemplateHandler>();
            pathTemplateHandler.Setup(p => p.ParseTemplate(model.Model, "Customers")).Returns(pathTemplate).Verifiable();

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model, config, pathTemplateHandler.Object);
            config.EnsureInitialized();

            // Act
            convention.SelectController(new ODataPath(), new HttpRequestMessage());

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal("GetCustomers", convention.AttributeMappings[pathTemplate].ActionName);
        }
        public void AttributeMappingsIsInitialized_WithRightActionAndTemplate(Type controllerType,
            string expectedPathTemplate, string expectedActionName)
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            HttpControllerDescriptor controller = new HttpControllerDescriptor(new HttpConfiguration(), "TestController",
                controllerType);

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock<IODataPathTemplateHandler> pathTemplateHandler = new Mock<IODataPathTemplateHandler>();
            pathTemplateHandler
                .Setup(p => p.ParseTemplate(model.Model, expectedPathTemplate))
                .Returns(pathTemplate)
                .Verifiable();

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model,
                new[] { controller }, pathTemplateHandler.Object);

            // Act
            convention.SelectController(new ODataPath(), new HttpRequestMessage());

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal(expectedActionName, convention.AttributeMappings[pathTemplate].ActionName);
        }
示例#4
0
 /// <summary>
 /// Maps the OData route attributes.
 /// </summary>
 /// <param name="configuration">The http configuration used to search for all the OData controllers to map.</param>
 public void MapODataRouteAttributes(HttpConfiguration configuration)
 {
     AttributeRoutingConvention routingConvention = new AttributeRoutingConvention(PathRouteConstraint.EdmModel, configuration);
     PathRouteConstraint.RoutingConventions.Insert(0, routingConvention);
 }