public void GetETagTentity_RoundTrip_ETagInHeader() { // Arrange HttpRequestMessage request = new HttpRequestMessage(); HttpConfiguration configuration = new HttpConfiguration(); request.SetConfiguration(configuration); Dictionary <string, object> properties = new Dictionary <string, object> { { "City", "Foo" } }; EntityTagHeaderValue value = new DefaultODataETagHandler().CreateETag(properties); CustomersModelWithInheritance model = new CustomersModelWithInheritance(); Mock <ODataPathSegment> mockSegment = new Mock <ODataPathSegment> { CallBase = true }; mockSegment.Setup(s => s.GetEdmType(null)).Returns(model.Customer); mockSegment.Setup(s => s.GetEntitySet(null)).Returns((IEdmEntitySet)null); ODataPath odataPath = new ODataPath(new[] { mockSegment.Object }); request.ODataProperties().Path = odataPath; // Act ETag <Customer> result = request.GetETag <Customer>(value); dynamic dynamicResult = result; // Assert Assert.Equal("Foo", result["City"]); Assert.Equal("Foo", dynamicResult.City); }
public override string SelectAction(System.Web.Http.OData.Routing.ODataPath odataPath, System.Web.Http.Controllers.HttpControllerContext controllerContext, ILookup <string, System.Web.Http.Controllers.HttpActionDescriptor> actionMap) { var action = base.SelectAction(odataPath, controllerContext, actionMap); if (action != null) { var routeValues = controllerContext.RouteData.Values; if (routeValues.ContainsKey(ODataRouteConstants.Key)) { var keyRaw = routeValues[ODataRouteConstants.Key] as string; IEnumerable <string> compoundKeyPairs = keyRaw.Split(','); if (compoundKeyPairs == null || compoundKeyPairs.Count() == 0) { return(action); } foreach (var compoundKeyPair in compoundKeyPairs) { string[] pair = compoundKeyPair.Split('='); if (pair == null || pair.Length != 2) { continue; } var keyName = pair[0].Trim(); var keyValue = pair[1].Trim(); routeValues.Add(keyName, keyValue); } } } return(action); }
public override string SelectAction(ODataPath odataPath, HttpControllerContext context, ILookup<string, HttpActionDescriptor> actionMap) { if (context.Request.Method == HttpMethod.Get && odataPath.PathTemplate == "~/entityset/key/navigation/key") { NavigationPathSegment navigationSegment = odataPath.Segments[2] as NavigationPathSegment; IEdmNavigationProperty navigationProperty = navigationSegment.NavigationProperty.Partner; IEdmEntityType declaringType = navigationProperty.DeclaringType as IEdmEntityType; string actionName = "Get" + declaringType.Name; if (actionMap.Contains(actionName)) { // Add keys to route data, so they will bind to action parameters. KeyValuePathSegment keyValueSegment = odataPath.Segments[1] as KeyValuePathSegment; context.RouteData.Values[ODataRouteConstants.Key] = keyValueSegment.Value; KeyValuePathSegment relatedKeySegment = odataPath.Segments[3] as KeyValuePathSegment; context.RouteData.Values[ODataRouteConstants.RelatedKey] = relatedKeySegment.Value; return actionName; } } // Not a match. return null; }
public override string SelectAction(System.Web.Http.OData.Routing.ODataPath odataPath, System.Web.Http.Controllers.HttpControllerContext controllerContext, ILookup <string, System.Web.Http.Controllers.HttpActionDescriptor> actionMap) { if (odataPath == null) { throw new ArgumentNullException("odataPath"); } if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } if (actionMap == null) { throw new ArgumentNullException("actionMap"); } HttpMethod requestMethod = controllerContext.Request.Method; if (odataPath.PathTemplate == "~/entityset/key/$links/navigation" && requestMethod == HttpMethod.Get) { KeyValuePathSegment keyValueSegment = odataPath.Segments[1] as KeyValuePathSegment; controllerContext.RouteData.Values[ODataRouteConstants.Key] = keyValueSegment.Value; NavigationPathSegment navigationSegment = odataPath.Segments[3] as NavigationPathSegment; IEdmNavigationProperty navigationProperty = navigationSegment.NavigationProperty; IEdmEntityType declaredType = navigationProperty.DeclaringType as IEdmEntityType; string action = requestMethod + "LinksFor" + navigationProperty.Name + "From" + declaredType.Name; return(actionMap.Contains(action) ? action : requestMethod + "LinksFor" + navigationProperty.Name); } return(base.SelectAction(odataPath, controllerContext, actionMap)); }
public string SelectController(ODataPath odataPath, HttpRequestMessage request) { var controller = delegateRoutingConvention.SelectController(odataPath, request); return string.Equals(controller, controllerAlias, StringComparison.OrdinalIgnoreCase) ? targetControllerName : controller; }
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { var action = base.SelectAction(odataPath, controllerContext, actionMap); if (action != null) { var routeValues = controllerContext.RouteData.Values; if (routeValues.ContainsKey(ODataRouteConstants.Key)) { var keyRaw = routeValues[ODataRouteConstants.Key] as string; IEnumerable<string> compoundKeyPairs = keyRaw.Split(','); if (compoundKeyPairs == null || !compoundKeyPairs.Any()) { return action; } foreach (var compoundKeyPair in compoundKeyPairs) { string[] pair = compoundKeyPair.Split('='); if (pair == null || pair.Length != 2) { continue; } var keyName = pair[0].Trim(); var keyValue = pair[1].Trim(); routeValues.Add(keyName, keyValue); } } } return action; }
public string SelectController(ODataPath odataPath, HttpRequestMessage request) { if (odataPath.PathTemplate == "~/action/$count") { return controllerName; } return null; }
public string SelectController(ODataPath odataPath, HttpRequestMessage request) { if (odataPath.PathTemplate == "~/entityset/key/property") { return _controllerName; } return null; }
// Route all non-bindable actions to a single controller. public string SelectController(ODataPath odataPath, System.Net.Http.HttpRequestMessage request) { if (odataPath.PathTemplate == "~/action") { return _controllerName; } return null; }
private static IEdmNavigationProperty GetNavigationProperty(ODataPath path) { if (path == null) { throw new SerializationException(SRResources.ODataPathMissing); } return path.GetNavigationProperty(); }
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { if (controllerContext.Request.Method != HttpMethod.Get || odataPath.PathTemplate != "~/entityset/$count") { return null; } return actionMap.Contains("GetCount") ? "GetCount" : null; }
private static void AddLinkInfoToRouteData(IHttpRouteData routeData, ODataPath odataPath) { KeyValuePathSegment keyValueSegment = odataPath.Segments.OfType<KeyValuePathSegment>().First(); routeData.Values[ODataRouteConstants.Key] = keyValueSegment.Value; KeyValuePathSegment relatedKeySegment = odataPath.Segments.Last() as KeyValuePathSegment; if (relatedKeySegment != null) { routeData.Values[ODataRouteConstants.RelatedKey] = relatedKeySegment.Value; } }
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { string result = base.SelectAction(odataPath, controllerContext, actionMap); IDictionary<string, object> conventionStore = controllerContext.Request.ODataProperties().RoutingConventionsStore; if (result != null && conventionStore != null) { conventionStore["keyAsCustomer"] = new BindCustomer { Id = int.Parse((string)controllerContext.RouteData.Values["key"]) }; } return result; }
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { if (odataPath == null) { throw new ArgumentNullException("odataPath"); } if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } if (actionMap == null) { throw new ArgumentNullException("actionMap"); } HttpMethod requestMethod = controllerContext.Request.Method; if (odataPath.PathTemplate == "~/entityset/key/$links/navigation" || odataPath.PathTemplate == "~/entityset/key/cast/$links/navigation" || odataPath.PathTemplate == "~/entityset/key/$links/navigation/key" || odataPath.PathTemplate == "~/entityset/key/cast/$links/navigation/key") { var actionName = string.Empty; if ((requestMethod == HttpMethod.Post) || (requestMethod == HttpMethod.Put)) { actionName += "CreateLinkTo"; } else if (requestMethod == HttpMethod.Delete) { actionName += "DeleteLinkTo"; } else { return null; } var navigationSegment = odataPath.Segments.OfType<NavigationPathSegment>().Last(); actionName += navigationSegment.NavigationPropertyName; var castSegment = odataPath.Segments[2] as CastPathSegment; if (castSegment != null) { var actionCastName = string.Format("{0}On{1}", actionName, castSegment.CastType.Name); if (actionMap.Contains(actionCastName)) { AddLinkInfoToRouteData(controllerContext.RouteData, odataPath); return actionCastName; } } if (actionMap.Contains(actionName)) { AddLinkInfoToRouteData(controllerContext.RouteData, odataPath); return actionName; } } return null; }
public void WriteObject_Throws_ObjectCannotBeWritten_IfGraphIsNotUri() { IEdmNavigationProperty navigationProperty = _customerSet.ElementType.NavigationProperties().First(); ODataEntityReferenceLinksSerializer serializer = new ODataEntityReferenceLinksSerializer(); ODataPath path = new ODataPath(new NavigationPathSegment(navigationProperty)); ODataSerializerContext writeContext = new ODataSerializerContext { EntitySet = _customerSet, Path = path }; Assert.Throws<SerializationException>( () => serializer.WriteObject(graph: "not uri", messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext), "ODataEntityReferenceLinksSerializer cannot write an object of type 'System.String'."); }
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { var action = base.SelectAction(odataPath, controllerContext, actionMap); if (action != null) { controllerContext.RouteData.DecomposeKey(); } return action; }
public void ToStringWithNoSegments() { // Arrange ODataPath path = new ODataPath(); // Act string value = path.ToString(); // Assert Assert.Empty(value); }
public void ToStringWithOneSegment() { // Arrange string expectedValue = "Set"; ODataPath path = new ODataPath(new EntitySetPathSegment(expectedValue)); // Act string value = path.ToString(); // Assert Assert.Equal(expectedValue, value); }
public virtual string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { var action = entityRoutingConvention.SelectAction(odataPath, controllerContext, actionMap); if (action == null) { return null; } controllerContext.RouteData.DecomposeKey(); return action; }
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { if (odataPath == null) { throw new ArgumentNullException("odataPath"); } if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } if (actionMap == null) { throw new ArgumentNullException("actionMap"); } IEdmNavigationProperty navigationProperty = GetNavigationProperty(odataPath); IEdmEntityType declaringType = navigationProperty == null ? null : navigationProperty.DeclaringType as IEdmEntityType; if (declaringType != null) { HttpMethod httpMethod = controllerContext.Request.Method; string actionName = null; if (IsNavigationPropertyValuePath(odataPath)) { if (httpMethod == HttpMethod.Get) { actionName = actionMap.FindMatchingAction("Get" + navigationProperty.Name, GetNavigationPropertyMethodName); } else if (httpMethod == HttpMethod.Post) { actionName = actionMap.FindMatchingAction("Post" + navigationProperty.Name, PostNavigationPropertyMethodName); } } // Navigation property links are already handled by LinksRoutingConvention // else if (IsNavigationPropertyLinkPath(odataPath)) {} if (actionName != null) { KeyValuePathSegment keyValueSegment = odataPath.Segments[1] as KeyValuePathSegment; if (keyValueSegment != null) { controllerContext.RouteData.Values[ODataRouteConstants.Key] = keyValueSegment.Value; } controllerContext.RouteData.Values[ODataRouteConstants.NavigationProperty] = navigationProperty.Name; return actionName; } } return null; }
public void WriteObject_Throws_NavigationPropertyMissingDuringSerialization() { // Arrange ODataEntityReferenceLinksSerializer serializer = new ODataEntityReferenceLinksSerializer(); ODataPath path = new ODataPath(new EntitySetPathSegment(_customerSet)); ODataSerializerContext writeContext = new ODataSerializerContext { Path = path }; // Act & Assert Assert.Throws<SerializationException>( () => serializer.WriteObject(graph: null, type: typeof(ODataEntityReferenceLinks), messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext), "The related navigation property could not be found from the OData path. The related navigation property is required to serialize the payload."); }
public void ToStringWithKeyValueSegment() { // Arrange string segment = "1"; ODataPath path = new ODataPath(new KeyValuePathSegment(segment)); // Act string value = path.ToString(); // Assert string expectedValue = "(" + segment + ")"; Assert.Equal(expectedValue, value); }
/// <summary> /// Selects the controller for OData requests. /// </summary> /// <param name="odataPath">The OData path.</param> /// <param name="request">The request.</param> /// <returns> /// <c>null</c> if the request isn't handled by this convention; otherwise, the name of the selected controller /// </returns> public string SelectController(ODataPath odataPath, HttpRequestMessage request) { //Contract.Requires<ArgumentNullException>(odataPath != null); //Contract.Requires<ArgumentNullException>(request != null); if (odataPath.PathTemplate == "~" || odataPath.PathTemplate == "~/$metadata") { return "EntityRepositoryMetadata"; } return null; }
public void TryMatchMediaTypeWithNonRawvalueRequestDoesntMatchRequest() { IEdmModel model = ODataTestUtil.GetEdmModel(); PropertyAccessPathSegment propertySegment = new PropertyAccessPathSegment((model.GetEdmType(typeof(FormatterPerson)) as IEdmEntityType).FindProperty("Age")); ODataPath path = new ODataPath(new EntitySetPathSegment("People"), new KeyValuePathSegment("1"), propertySegment); ODataPrimitiveValueMediaTypeMapping mapping = new ODataPrimitiveValueMediaTypeMapping(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/People(1)/Age/"); request.ODataProperties().Model = model; request.ODataProperties().Path = path; double mapResult = mapping.TryMatchMediaType(request); Assert.Equal(0, mapResult); }
public void TryMatchMediaTypeWithBinaryRawValueMatchesRequest() { IEdmModel model = GetBinaryModel(); PropertyAccessPathSegment propertySegment = new PropertyAccessPathSegment((model.GetEdmType(typeof(RawValueEntity)) as IEdmEntityType).FindProperty("BinaryProperty")); ODataPath path = new ODataPath(new EntitySetPathSegment("RawValue"), new KeyValuePathSegment("1"), propertySegment, new ValuePathSegment()); ODataBinaryValueMediaTypeMapping mapping = new ODataBinaryValueMediaTypeMapping(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/RawValue(1)/BinaryProperty/$value"); request.ODataProperties().Model = model; request.ODataProperties().Path = path; double mapResult = mapping.TryMatchMediaType(request); Assert.Equal(1.0, mapResult); }
public void ToStringWithOneTwoSegments() { // Arrange string expectedFirstSegment = "Set"; string expectedSecondSegment = "Action"; ODataPath path = new ODataPath(new EntitySetPathSegment(expectedFirstSegment), new ActionPathSegment(expectedSecondSegment)); // Act string value = path.ToString(); // Assert string expectedValue = expectedFirstSegment + "/" + expectedSecondSegment; Assert.Equal(expectedValue, value); }
/// <summary> /// Matches the current template with an OData path. /// </summary> /// <param name="path">The OData path to be matched against.</param> /// <param name="values">The dictionary of matches to be updated in case of a match.</param> /// <returns><see langword="true"/> in case of a match; otherwise, <see langword="false"/>.</returns> public bool TryMatch(ODataPath path, IDictionary<string, object> values) { if (path.Segments.Count != Segments.Count) { return false; } for (int index = 0; index < Segments.Count; index++) { if (!Segments[index].TryMatch(path.Segments[index], values)) { return false; } } return true; }
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { //if (controllerContext.Request.Method == System.Net.Http.HttpMethod.Put) controllerContext.Request.Content.ReadAsStreamAsync().ContinueWith((t) => A(t.Result)); if (odataPath == null) { throw new ArgumentNullException("odataPath"); } if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } if (actionMap == null) { throw new ArgumentNullException("actionMap"); } string prefix = GetHttpPrefix(controllerContext.Request.Method.ToString()); if (string.IsNullOrEmpty(prefix)) { return null; } //~/entityset/key/$links/navigation if ((odataPath.PathTemplate == "~/entityset/key/navigation") || (odataPath.PathTemplate == "~/entityset/key/cast/navigation") || (odataPath.PathTemplate == "~/entityset/key/$links/navigation")) { NavigationPathSegment segment = odataPath.Segments.Last() as NavigationPathSegment; IEdmNavigationProperty navigationProperty = segment.NavigationProperty; IEdmEntityType declaringType = navigationProperty.DeclaringType as IEdmEntityType; if (declaringType != null) { KeyValuePathSegment keySegment = odataPath.Segments[1] as KeyValuePathSegment; controllerContext.RouteData.Values[ODataRouteConstants.Key] = keySegment.Value; string actionName = prefix + navigationProperty.Name + "From" + declaringType.Name; return (actionMap.Contains(actionName) ? actionName : (prefix + navigationProperty.Name)); } } return null; }
public override string Link(ODataPath path) { bool firstSegment = true; StringBuilder sb = new StringBuilder(); foreach (ODataPathSegment segment in path.Segments) { if (firstSegment) { firstSegment = false; } else { sb.Append('/'); } sb.Append(segment); } return sb.ToString(); }
public void OnActionExecuted_DoesntChangeTheResponse_IfResponseIsntSuccessful() { IEdmModel model = new Mock<IEdmModel>().Object; ODataPath path = new ODataPath(new EntitySetPathSegment("FakeEntitySet"), new KeyValuePathSegment("FakeKey"), new PropertyAccessPathSegment("FakeProperty"), new ValuePathSegment()); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/"); request.ODataProperties().Model = model; request.ODataProperties().Path = path; ODataNullValueAttribute odataNullValue = new ODataNullValueAttribute(); HttpResponseMessage response = SetUpResponse(HttpStatusCode.InternalServerError, null, typeof(object)); HttpActionExecutedContext context = SetUpContext(request, response); odataNullValue.OnActionExecuted(context); Assert.Equal(response, context.Response); }
// Route the action to a method with the same name as the action. public string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { // OData actions must be invoked with HTTP POST. if (controllerContext.Request.Method == HttpMethod.Post) { if (odataPath.PathTemplate == "~/action") { ActionPathSegment actionSegment = odataPath.Segments.First() as ActionPathSegment; IEdmFunctionImport action = actionSegment.Action; if (!action.IsBindable && actionMap.Contains(action.Name)) { return action.Name; } } } return null; }
/// <summary> /// Selects the action for OData requests. /// </summary> /// <param name="odataPath">The OData path.</param> /// <param name="controllerContext">The controller context.</param> /// <param name="actionMap">The action map.</param> /// <returns> /// <c>null</c> if the request isn't handled by this convention; otherwise, the name of the selected action /// </returns> public string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { //Contract.Requires<ArgumentNullException>(odataPath != null); //Contract.Requires<ArgumentNullException>(controllerContext != null); //Contract.Requires<ArgumentNullException>(actionMap != null); if (odataPath.PathTemplate == "~") { return "GetServiceDocument"; } if (odataPath.PathTemplate == "~/$metadata") { return "GetMetadata"; } return null; }
public override string SelectAction(System.Web.Http.OData.Routing.ODataPath odataPath, System.Web.Http.Controllers.HttpControllerContext controllerContext, ILookup <string, System.Web.Http.Controllers.HttpActionDescriptor> actionMap) { if (odataPath.PathTemplate == "~/entityset/key/property" || odataPath.PathTemplate == "~/entityset/key/cast/property") { var segment = odataPath.Segments.Last() as PropertyAccessPathSegment; var property = segment.Property; var declareType = property.DeclaringType as IEdmEntityType; if (declareType != null) { var key = odataPath.Segments[1] as KeyValuePathSegment; controllerContext.RouteData.Values.Add(ODataRouteConstants.Key, key.Value); string prefix = ODataHelper.GetHttpPrefix(controllerContext.Request.Method.ToString()); if (string.IsNullOrEmpty(prefix)) { return(null); } string action = prefix + property.Name + "From" + declareType.Name; return(actionMap.Contains(action) ? action : prefix + property.Name); } } return(null); }
public string HandleUnmappedRequest(ODataPath path) { return(path.PathTemplate); }
/// <summary> /// Converts an instance of <see cref="ODataPath" /> into an OData link. /// </summary> /// <param name="path">The OData path to convert into a link.</param> /// <returns> /// The generated OData link. /// </returns> public virtual string Link(ODataPath path) { return(path.ToString()); }
public static void SetODataPath(this HttpRequestMessage request, Routing.ODataPath odataPath) { Extensions.HttpRequestMessageExtensions.ODataProperties(request).Path = odataPath; }
public void CanReadType_ForTypeless_ReturnsExpectedResult_DependingOnODataPathAndPayloadKind(ODataPath path, ODataPayloadKind payloadKind) { // Arrange IEnumerable <ODataPayloadKind> allPayloadKinds = Enum.GetValues(typeof(ODataPayloadKind)).Cast <ODataPayloadKind>(); var model = CreateModel(); var request = CreateFakeODataRequest(model); request.SetODataPath(path); var formatterWithGivenPayload = new ODataMediaTypeFormatter(new[] { payloadKind }) { Request = request }; var formatterWithoutGivenPayload = new ODataMediaTypeFormatter(allPayloadKinds.Except(new[] { payloadKind })) { Request = request }; // Act & Assert Assert.True(formatterWithGivenPayload.CanReadType(typeof(IEdmObject))); Assert.False(formatterWithoutGivenPayload.CanReadType(typeof(IEdmObject))); }
public string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { if (odataPath.PathTemplate != "~/entityset/key/property") { return null; } var entitySetPathSegment = odataPath.Segments.OfType<EntitySetPathSegment>().Single(); var keyValuePathSegment = odataPath.Segments.OfType<KeyValuePathSegment>().Single(); var propertyAccessPathSegment = odataPath.Segments.OfType<PropertyAccessPathSegment>().Single(); var actionName = string.Format(CultureInfo.InvariantCulture, "GetPropertyFrom{0}", entitySetPathSegment.EntitySetName); if (actionMap.Contains(actionName) && actionMap[actionName].Any(desc => MatchHttpMethod(desc, controllerContext.Request.Method))) { controllerContext.RouteData.Values.Add("propertyName", propertyAccessPathSegment.PropertyName); if (!CompositeODataKeyHelper.TryEnrichRouteValues(keyValuePathSegment.Value, controllerContext.RouteData.Values)) { controllerContext.RouteData.Values.Add("key", keyValuePathSegment.Value); } return actionName; } return null; }