public void PermissionEvaluator_ReturnsTrue_IfScopesMatchRequiredPermissions(string method, string endpoint, string userScopes) { var path = _parser.Parse(_serviceRoot, endpoint, _serviceProvider); var scopesList = userScopes.Split(','); var permissionHandler = _model.ExtractPermissionsForRequest(method, path); Assert.True(permissionHandler.AllowsScopes(scopesList)); }
public void SelectAction_ReturnsFunctionName_DollarCount() { // Arrange var model = new CustomersModelWithInheritance(); var handler = new DefaultODataPathHandler(); ODataPath odataPath = handler.Parse(model.Model, _serviceRoot, "Customers(1)/NS.GetOrders(parameter=5)/$count"); var requestContext = new HttpRequestContext(); var controllerContext = new HttpControllerContext { Request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/"), RequestContext = requestContext, RouteData = new HttpRouteData(new HttpRoute()) }; controllerContext.Request.SetRequestContext(requestContext); ILookup <string, HttpActionDescriptor> actionMap = new HttpActionDescriptor[1].ToLookup(desc => "GetOrders"); // Act string selectedAction = new FunctionRoutingConvention().SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.Equal("GetOrders", selectedAction); Assert.Equal(2, controllerContext.Request.GetRouteData().Values.Count); Assert.Equal(1, controllerContext.Request.GetRouteData().Values["key"]); Assert.Equal(5, controllerContext.Request.GetRouteData().Values["parameter"]); }
public void SelectAction_ReturnsTheActionName_ForSingletonActionBoundToEntity() { // Arrange ActionRoutingConvention actionConvention = new ActionRoutingConvention(); HttpContext context = CreateHttpContext("Post"); IEdmModel model = new CustomersModelWithInheritance().Model; //IODataPathHandler pathHandler = context.RequestServices.GetRequiredService<IODataPathHandler>(); IODataPathHandler pathHandler = new DefaultODataPathHandler(context.RequestServices); ODataPath odataPath = pathHandler.Parse(model, _serviceRoot, "VipCustomer/NS.upgrade"); context.ODataFeature().Path = odataPath; ControllerActionDescriptor descriptor = new ControllerActionDescriptor(); descriptor.ControllerName = "VipCustomer"; descriptor.ActionName = "upgrade"; RouteContext routeContext = new RouteContext(context); IEnumerable <ControllerActionDescriptor> actionDescriptors = new[] { descriptor }; // Act ActionDescriptor actionDescriptor = actionConvention.SelectAction(routeContext, actionDescriptors); // Assert Assert.Same(descriptor, actionDescriptor); Assert.Empty(routeContext.RouteData.Values); }
public void BuildQuery_ReturnsNull_IfPathHasUnsupportedSegmen(string path) { var model = ODataPathQueryModel.GetModel(); var pathHandler = new DefaultODataPathHandler(); var odataPath = pathHandler.Parse(model, "http://any/", path); IQueryable source = Array.CreateInstance(typeof(ODataPathQuery_Customer), 0).AsQueryable(); var queryBuilder = new ODataPathQueryBuilder(source, odataPath); ODataPathQueryResult result = queryBuilder.BuildQuery(); Assert.Null(result); }
internal static IDictionary <string, string> GetKeys(DefaultODataPathHandler pathHandler, IEdmModel edmModel, string serviceRoot, Uri uri) { ODataPath odataPath = pathHandler.Parse(edmModel, serviceRoot, uri.ToString()); KeyValuePathSegment segment = odataPath.Segments.OfType <KeyValuePathSegment>().Last(); if (segment == null) { throw Error.InvalidOperation(SRResources.EntityReferenceMustHasKeySegment, uri); } return(segment.Values); }
private static IEnumerable <KeyValuePair <string, object> > GetKeys(DefaultODataPathHandler pathHandler, string serviceRoot, Uri uri, IServiceProvider requestContainer) { ODataPath odataPath = pathHandler.Parse(serviceRoot, uri.ToString(), requestContainer); KeySegment segment = odataPath.Segments.OfType <KeySegment>().Last(); if (segment == null) { throw Error.InvalidOperation(SRResources.EntityReferenceMustHasKeySegment, uri); } return(segment.Keys); }
public void GetResponseStatusCode_ReturnsNoContentForProperties_AndNotFoundForEntities(string odataPath, HttpStatusCode?expected) { // Arrange IEdmModel model = BuildModel(); IODataPathHandler pathHandler = new DefaultODataPathHandler(); ODataPath path = pathHandler.Parse(model, "http://localhost/any", odataPath); // Guard Assert.NotNull(path); // Act HttpStatusCode?statusCode = ODataNullValueMessageHandler.GetUpdatedResponseStatusCodeOrNull(path); // Assert Assert.Equal(expected, statusCode); }
public void GetSingleEntityEntityType_ReturnsEntityTypeForSingleEntityResources(string odataPath, string typeName) { // Arrange IEdmModel model = SetupModel(); IODataPathHandler pathHandler = new DefaultODataPathHandler(); ODataPath path = pathHandler.Parse(model, "http://localhost/any", odataPath); // Guard Assert.NotNull(path); // Act IEdmEntityType entityType = ETagMessageHandler.GetSingleEntityEntityType(path); // Assert Assert.NotNull(entityType); Assert.Equal(typeName, entityType.FullName()); }
public void Validate_Throws_DollarCountAppliedOnNotCountableCollection(string uri, string message) { // Arrange IEdmModel model = GetEdmModel(); var pathHandler = new DefaultODataPathHandler(); string serviceRoot = "http://localhost/"; ODataPath path = pathHandler.Parse(model, serviceRoot, uri); var context = new ODataQueryContext( model, EdmCoreModel.Instance.GetInt32(false).Definition, path); var option = new CountQueryOption("true", context); var settings = new ODataValidationSettings(); // Act & Assert ExceptionAssert.Throws <InvalidOperationException>(() => _validator.Validate(option, settings), message); }
public void BuildQuery_SetValueFlagToTrue_IfPathHasValueSegment(string path, string expectedQuery) { var model = ODataPathQueryModel.GetModel(); var pathHandler = new DefaultODataPathHandler(); var odataPath = pathHandler.Parse(model, "http://any/", path); IQueryable source = Array.CreateInstance(typeof(ODataPathQuery_Customer), 0).AsQueryable(); var queryBuilder = new ODataPathQueryBuilder(source, odataPath); ODataPathQueryResult result = queryBuilder.BuildQuery(); string queryExpression = ExpressionStringBuilder.ToString(result.Result.Expression); queryExpression = RemoveNameSpace(queryExpression); Assert.Equal(expectedQuery, queryExpression); Assert.True(result.HasValueSegment); Assert.False(result.HasCountSegment); }
public void SelectAction_ReturnsFunctionName_DollarCount() { // Arrange var model = new CustomersModelWithInheritance(); var handler = new DefaultODataPathHandler(); ODataPath odataPath = handler.Parse(model.Model, _serviceRoot, "Customers(1)/NS.GetOrders(parameter=5)/$count"); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/"); var actionMap = SelectActionHelper.CreateActionMap("GetOrders"); // Act string selectedAction = SelectActionHelper.SelectAction(new FunctionRoutingConvention(), odataPath, request, actionMap); // Assert Assert.Equal("GetOrders", selectedAction); Assert.Equal(2, SelectActionHelper.GetRouteData(request).Values.Count); Assert.Equal(1, SelectActionHelper.GetRouteData(request).Values["key"]); Assert.Equal(5, SelectActionHelper.GetRouteData(request).Values["parameter"]); }
public void GetODataPayloadSerializer_ReturnsRawValueSerializer_ForDollarCountRequests(string uri, Type elementType) { // Arrange IEdmModel model = ODataCountTest.GetEdmModel(); Type type = typeof(ICollection <>).MakeGenericType(elementType); var pathHandler = new DefaultODataPathHandler(); var path = pathHandler.Parse(model, "http://localhost/", uri); var request = RequestFactory.CreateFromModel(model); request.ODataContext().Path = path; // Act var serializer = _serializerProvider.GetODataPayloadSerializer(type, request); // Assert Assert.NotNull(serializer); var rawValueSerializer = Assert.IsType <ODataRawValueSerializer>(serializer); Assert.Equal(ODataPayloadKind.Value, rawValueSerializer.ODataPayloadKind); }
public void GetODataPayloadSerializer_ReturnsRawValueSerializer_ForDollarCountRequests(string uri, Type elementType) { // Arrange IEdmModel model = ODataCountTest.GetEdmModel(); Type type = typeof(ICollection <>).MakeGenericType(elementType); var request = new HttpRequestMessage(); var pathHandler = new DefaultODataPathHandler(); var path = pathHandler.Parse(model, "http://localhost/", uri); request.ODataProperties().Path = path; request.EnableHttpDependencyInjectionSupport(model); // Act var serializer = _serializerProvider.GetODataPayloadSerializer(type, request); // Assert Assert.NotNull(serializer); var rawValueSerializer = Assert.IsType <ODataRawValueSerializer>(serializer); Assert.Equal(ODataPayloadKind.Value, rawValueSerializer.ODataPayloadKind); }
public void GetODataPayloadSerializer_ReturnsRawValueSerializer_ForDollarCountRequests(string uri, Type elementType) { // Arrange IEdmModel model = ODataCountTest.GetEdmModel(); Type type = typeof(ICollection <>).MakeGenericType(elementType); DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider(); HttpContext context = CreateHttpContext(serialierProvider); var pathHandler = new DefaultODataPathHandler(context.RequestServices); var path = pathHandler.Parse(model, "http://localhost/", uri); context.ODataFeature().Path = path; context.ODataFeature().Model = model; // Act var serializer = serialierProvider.GetODataPayloadSerializer(context, type); // Assert Assert.NotNull(serializer); var rawValueSerializer = Assert.IsType <ODataRawValueSerializer>(serializer); Assert.Equal(ODataPayloadKind.Value, rawValueSerializer.ODataPayloadKind); }
public void SelectAction_ReturnsFunctionName_DollarCount() { // Arrange var model = new CustomersModelWithInheritance(); var handler = new DefaultODataPathHandler(); ODataPath odataPath = handler.Parse(model.Model, _serviceRoot, "Customers(1)/NS.GetOrders(parameter=5)/$count"); var requestContext = new HttpRequestContext(); var controllerContext = new HttpControllerContext { Request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/"), RequestContext = requestContext, RouteData = new HttpRouteData(new HttpRoute()) }; controllerContext.Request.SetRequestContext(requestContext); ILookup<string, HttpActionDescriptor> actionMap = new HttpActionDescriptor[1].ToLookup(desc => "GetOrders"); // Act string selectedAction = new FunctionRoutingConvention().SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.Equal("GetOrders", selectedAction); Assert.Equal(2, controllerContext.Request.GetRouteData().Values.Count); Assert.Equal("1", controllerContext.Request.GetRouteData().Values["key"]); Assert.Equal(5, controllerContext.Request.GetRouteData().Values["parameter"]); }