Beispiel #1
0
        public void CtorTakingHttpConfiguration_InitializesAttributeMappings_OnFirstSelectControllerCall()
        {
            // Arrange
            var config          = RoutingConfigurationFactory.CreateWithRootContainer(RouteName);
            var serviceProvider = GetServiceProvider(config, RouteName);
            var request         = RequestFactory.Create(config, RouteName);

#if NETCORE
            request.ODataFeature().Path = new ODataPath();
            request.Method = "Get";
            ControllerDescriptorFactory.Create(config, "MetadataAndService", typeof(MetadataAndServiceController));
#endif

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock <IODataPathTemplateHandler> pathTemplateHandler = new Mock <IODataPathTemplateHandler>();
            pathTemplateHandler.Setup(p => p.ParseTemplate("$metadata", serviceProvider))
            .Returns(pathTemplate).Verifiable();

            AttributeRoutingConvention convention = CreateAttributeRoutingConvention(RouteName, config, pathTemplateHandler.Object);
            EnsureAttributeMapping(convention, config);

            // Act
            Select(convention, request);

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal("GetMetadata", convention.AttributeMappings[pathTemplate].ActionName);
        }
        public void AttributeMappingsIsInitialized_WithRightActionAndTemplate(Type controllerType,
                                                                              string expectedPathTemplate, string expectedActionName)
        {
            // Arrange
            var configuration   = RoutingConfigurationFactory.CreateWithRootContainer(RouteName);
            var serviceProvider = GetServiceProvider(configuration, RouteName);
            var request         = RequestFactory.Create(configuration, RouteName);
            var descriptors     = ControllerDescriptorFactory.Create(configuration, "TestController",
                                                                     controllerType);

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock <IODataPathTemplateHandler> pathTemplateHandler = new Mock <IODataPathTemplateHandler>();

            pathTemplateHandler
            .Setup(p => p.ParseTemplate(expectedPathTemplate, serviceProvider))
            .Returns(pathTemplate)
            .Verifiable();

            AttributeRoutingConvention convention = new AttributeRoutingConvention(RouteName, descriptors, pathTemplateHandler.Object);

            // Act
            Select(convention, request);

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal(expectedActionName, convention.AttributeMappings[pathTemplate].ActionName);
        }
Beispiel #3
0
        public void CtorTakingModelAndControllersAndPathHandler_ThrowsArgumentNull_PathTemplateHandler()
        {
            var controllers = ControllerDescriptorFactory.CreateCollection();

            ExceptionAssert.ThrowsArgumentNull(
                () => new AttributeRoutingConvention(controllers: controllers,
                                                     routeName: RouteName, pathTemplateHandler: null),
                "pathTemplateHandler");
        }
Beispiel #4
0
        public void Constructor_ThrowsInvalidOperation_IfFailsToParsePathTemplate()
        {
            // Arrange
            IEdmModel model         = new CustomersModelWithInheritance().Model;
            var       configuration = RoutingConfigurationFactory.CreateWithRootContainer(RouteName,
                                                                                          (b => b.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => model)));

            var descriptors = ControllerDescriptorFactory.Create(configuration,
                                                                 "TestController", typeof(InvalidPathTemplateController));

            // Act & Assert
            ExceptionAssert.Throws <InvalidOperationException>(
                () => new AttributeRoutingConvention(RouteName, descriptors, new DefaultODataPathHandler()),
                "The path template 'Customers/Order' on the action 'GetCustomers' in controller 'TestController' is not " +
                "a valid OData path template. Bad Request - Error in query syntax.");
        }
Beispiel #5
0
        public void AttributeRoutingConvention_ConfigEnsureInitialized_ThrowsForInvalidPathTemplate()
        {
            // Arrange
            var configuration = RoutingConfigurationFactory.CreateWithRootContainerAndTypes(RouteName, null, typeof(TestODataController));

#if NETCORE
            ControllerDescriptorFactory.Create(configuration, "TestOData", typeof(TestODataController));
#endif

            AttributeRoutingConvention convention = CreateAttributeRoutingConvention(RouteName, configuration);

            // Act & Assert
            ExceptionAssert.Throws <InvalidOperationException>(
                () => EnsureAttributeMapping(convention, configuration),
                "The path template 'Customers' on the action 'GetCustomers' in controller 'TestOData' is not a valid OData path template. " +
                "Resource not found for the segment 'Customers'.");
        }
        public void AttributeRoutingConvention_ConfigEnsureInitialized_ThrowsForInvalidPathTemplate()
        {
            // Arrange
            var configuration = RoutingConfigurationFactory.CreateWithRootContainerAndTypes(RouteName, null, typeof(TestODataController));

#if NETCORE
            ControllerDescriptorFactory.Create(configuration, "TestOData", typeof(TestODataController));
#endif

            AttributeRoutingConvention convention = CreateAttributeRoutingConvention(RouteName, configuration);

            // Act & Assert
            ExceptionAssert.Throws <InvalidOperationException>(
                () => EnsureAttributeMapping(convention, configuration),
                "The path template 'Customers' on the action 'GetCustomers' in controller 'TestOData' is not a valid OData path template. " +
                "The operation import overloads matching 'Customers' are invalid. This is most likely an error in the IEdmModel.");
        }
        protected virtual bool VerifyControllerAttributes(RouteData routes, Type controllerType, ControllerContext controllerContext)
        {
            // Get controller descriptor
            var controllerDescriptor = ControllerDescriptorFactory.Create(controllerType);

            if (controllerDescriptor == null)
            {
                return(true);
            }

            // Get action descriptor
            var actionDescriptor = this.GetActionDescriptor(routes.GetOptionalString("action"), controllerDescriptor, controllerContext);

            if (actionDescriptor == null)
            {
                return(true);
            }

            // Verify security
            var authorizeAttributes = this.GetAuthorizeAttributes(actionDescriptor, controllerContext);

            return(this.VerifyAuthorizeAttributes(authorizeAttributes, controllerContext, actionDescriptor));
        }