예제 #1
0
        public void ValidateQuery_Throws_WithNullQueryOptions()
        {
            // Arrange
            EnableQueryAttribute attribute = new EnableQueryAttribute();

            // Act & Assert
            Assert.ThrowsArgumentNull(() => attribute.ValidateQuery(new HttpRequestMessage(), null), "queryOptions");
        }
예제 #2
0
        public void OrderByAllowedPropertiesWithSpaces(string allowedProperties)
        {
            EnableQueryAttribute attribute = new EnableQueryAttribute();

            attribute.AllowedOrderByProperties = allowedProperties;
            HttpRequestMessage request      = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customers/?$orderby=Id,Name");
            ODataQueryOptions  queryOptions = new ODataQueryOptions(ValidationTestHelper.CreateCustomerContext(), request);

            Assert.DoesNotThrow(() => attribute.ValidateQuery(request, queryOptions));
        }
예제 #3
0
        public void ValidateQuery_Throws_With_Null_Request()
        {
            // Arrange
            EnableQueryAttribute attribute = new EnableQueryAttribute();
            var model   = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
            var options = new ODataQueryOptions(new ODataQueryContext(model, typeof(System.Web.Http.OData.Builder.TestModels.Customer)), new HttpRequestMessage());

            // Act & Assert
            Assert.ThrowsArgumentNull(() => attribute.ValidateQuery(null, options), "request");
        }
예제 #4
0
        public void OrderByDisllowedPropertiesWithSpaces(string allowedProperties)
        {
            EnableQueryAttribute attribute = new EnableQueryAttribute();

            attribute.AllowedOrderByProperties = allowedProperties;
            HttpRequestMessage request      = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customers/?$orderby=Id,Name");
            ODataQueryOptions  queryOptions = new ODataQueryOptions(ValidationTestHelper.CreateCustomerContext(), request);

            Assert.Throws <ODataException>(() => attribute.ValidateQuery(request, queryOptions),
                                           "Order by 'Name' is not allowed. To allow it, set the 'AllowedOrderByProperties' property on EnableQueryAttribute or QueryValidationSettings.");
        }
예제 #5
0
        public void ValidateQuery_Accepts_All_Supported_QueryNames(string query)
        {
            // Arrange
            EnableQueryAttribute attribute = new EnableQueryAttribute();
            HttpRequestMessage   request   = new HttpRequestMessage(HttpMethod.Get, "http://localhost/?" + query);
            var model   = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
            var options = new ODataQueryOptions(new ODataQueryContext(model, typeof(System.Web.Http.OData.Builder.TestModels.Customer)), request);

            // Act & Assert
            Assert.DoesNotThrow(() => attribute.ValidateQuery(request, options));
        }
        public Task <IValueProvider> BindAsync(object value, ValueBindingContext context)
        {
            var request      = value as HttpRequest ?? throw new InvalidOperationException($"{nameof(value)} must be an {nameof(HttpRequest)}");
            var queryContext = new ODataQueryContext(_context.Model, _type, new Microsoft.AspNet.OData.Routing.ODataPath());
            var query        = _optionsBuilder(request, queryContext);

            _attribute.ValidateQuery(request, query);

            var provider = new ODataBindingValueProvider(query);

            return(Task.FromResult <IValueProvider>(provider));
        }
예제 #7
0
        public void ValidateQuery_Sends_BadRequest_For_Unrecognized_QueryNames()
        {
            // Arrange
            EnableQueryAttribute attribute = new EnableQueryAttribute();
            HttpRequestMessage   request   = new HttpRequestMessage(HttpMethod.Get, "http://localhost/?$xxx");
            var model   = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
            var options = new ODataQueryOptions(new ODataQueryContext(model, typeof(System.Web.Http.OData.Builder.TestModels.Customer)), request);

            // Act & Assert
            HttpResponseException responseException = Assert.Throws <HttpResponseException>(
                () => attribute.ValidateQuery(request, options));

            Assert.Equal(HttpStatusCode.BadRequest, responseException.Response.StatusCode);
        }