public void ValidateQuery_Throws_WithNullQueryOptions() { // Arrange EnableQueryAttribute attribute = new EnableQueryAttribute(); // Act & Assert Assert.ThrowsArgumentNull(() => attribute.ValidateQuery(new HttpRequestMessage(), null), "queryOptions"); }
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)); }
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"); }
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."); }
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)); }
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); }