public void TestSubstitution() { var sub = new DefaultRouteValueSubstitution(); Expression <Func <ControllerSample, ModelSample, ModelSample> > lambda = (c, m) => c.ControllerMethod(m.Id, m.Name, QueryParameter.Is <string>(), QueryParameter.Is <int>()); var methodCallExpression = (MethodCallExpression)lambda.Body; var apiExplorerMoq = new Mock <IApiExplorer>(); apiExplorerMoq.Setup(_ => _.ApiDescriptions).Returns(new Collection <ApiDescription>() { new ApiDescription() { } }); var mr = new MappingRule(methodCallExpression, apiExplorerMoq.Object); var payload = new ModelSample() { Id = 1, Name = "test &?{}<>", Price = 3.2 }; var result = sub.Substitute("/Product/{id}/Details?query={query}&skip={skip}&displayname={name}", mr, payload); Assume.That(result, Is.EqualTo("/Product/1/Details?query=:query&skip=:skip&displayname=test+%26%3f%7b%7d%3c%3e")); }
public static MetadataPlainObjects.Actions Generate(IActionConfiguration actionConfiguration, Dictionary <string, List <string> > routeRelations, object originalObject) { var result = new MetadataPlainObjects.Actions(); var mappingRules = actionConfiguration.MappingRules; var routeNameSubstitution = new DefaultRouteValueSubstitution(); result.AddRange(from mappingRule in mappingRules let apiDescription = mappingRule.ApiDescriptions.OrderBy(d => d.RelativePath.Length).FirstOrDefault() let isAction = mappingRule.Type == MappingRule.RuleType.ActionRule || (mappingRule.Type == MappingRule.RuleType.Default && apiDescription.HttpMethod != HttpMethod.Get) where apiDescription != null && isAction let absolutePath = LinkHelper.MakeAbsolutePath(routeNameSubstitution.Substitute(apiDescription.RelativePath, mappingRule, originalObject)) let routeNames = routeRelations[apiDescription.ID] select new MetadataPlainObjects.Action() { Href = absolutePath, Method = apiDescription.HttpMethod.Method, Title = apiDescription.Documentation, ActionName = routeNames.FirstOrDefault(), ActionFields = ActionFieldsGenerator.Generate(mappingRule, apiDescription, originalObject), ContentType = DeduceContentType(mappingRule, apiDescription, originalObject), Class = GetClassArray(mappingRule) }); return(result); }
public void TestFromBody() { var sub = new DefaultRouteValueSubstitution(); Expression <Func <ControllerSample, ModelSample, ModelSample> > lambda = (c, m) => c.ControllerMethodPut(m.Id, m); var methodCallExpression = (MethodCallExpression)lambda.Body; var apiExplorerMoq = new Mock <IApiExplorer>(); apiExplorerMoq.Setup(_ => _.ApiDescriptions).Returns(new Collection <ApiDescription>() { new ApiDescription() { HttpMethod = HttpMethod.Get } }); var mr = new MappingRule(methodCallExpression, apiExplorerMoq.Object); var payload = _fixture.CreateAnonymous <ModelSample>(); var fields = ActionFieldsGenerator.Generate(mr, apiExplorerMoq.Object.ApiDescriptions[0], payload); Assume.That(fields, Is.Not.Null); var names = fields.ConvertAll(f => f.FieldName); Assume.That(names, Is.EquivalentTo(new[] { "Id", "Name", "Price", "email_address" })); var types = fields.ConvertAll(f => f.FieldType); Assume.That(types, Is.EqualTo(new object[] { null, null, null, "email" })); var values = fields.ConvertAll(f => f.FieldValue); Assume.That(values, Is.EqualTo(new object[] { payload.Id.ToString(), payload.Name, payload.Price.ToString(), payload.EMailAddress })); }
private static object GenerateForLinkedRule(EntityRule rule, IActionConfiguration actionConfiguration, Dictionary <string, List <string> > routeRelations, object originalObject) { var actionExecutedContext = ActionCallContext.Get <HttpActionExecutedContext>(); var entityActionConfiguration = HypermediaControllerConfiguration.Instance.GetcontrollerActionConfiguration(rule.ControllerType, rule.ControllerAction, actionExecutedContext.Request.Headers.Accept); if (entityActionConfiguration == null) { return(null); } var referencedObject = rule.GetReferencedObjectInstance(originalObject); if (referencedObject == null) { return(null); } var selfRule = entityActionConfiguration.MappingRules.FirstOrDefault(r => r.Names.Contains("self")); if (selfRule == null) { throw new Exception(string.Format("Unable to generate link to entity object from controller {0} action {1}. Can't find self rel.", actionConfiguration.ControllerType.FullName, actionConfiguration.ActionMethodInfo)); } var selfApi = selfRule.ApiDescriptions.OrderBy(d => d.RelativePath.Length).FirstOrDefault(); if (selfApi == null) { throw new Exception(string.Format("Unable to generate link to entity object from controller {0} action {1}. Can't find self API.", actionConfiguration.ControllerType.FullName, actionConfiguration.ActionMethodInfo)); } var routeNameSubstitution = new DefaultRouteValueSubstitution(); var absolutePath = LinkHelper.MakeAbsolutePath(routeNameSubstitution.Substitute(selfApi.RelativePath, selfRule, referencedObject)); return(new LinkedEntity() { Rels = rule.Rel, ClassName = entityActionConfiguration.Class, Href = absolutePath }); }
public static MetadataPlainObjects.Links Generate(IActionConfiguration actionConfiguration, Dictionary <string, List <string> > routeRelations, object originalObject) { var result = new MetadataPlainObjects.Links(); var mappingRules = actionConfiguration.MappingRules; var routeNameSubstitution = new DefaultRouteValueSubstitution(); result.AddRange(from mappingRule in mappingRules let apiDescription = mappingRule.ApiDescriptions.OrderBy(d => d.RelativePath.Length).FirstOrDefault() let isLink = mappingRule.Type == MappingRule.RuleType.LinkRule || (mappingRule.Type == MappingRule.RuleType.Default && apiDescription.HttpMethod == HttpMethod.Get) where apiDescription != null && isLink let absolutePath = LinkHelper.MakeAbsolutePath(routeNameSubstitution.Substitute(apiDescription.RelativePath, mappingRule, originalObject)) select new MetadataPlainObjects.SirenLink() { Href = absolutePath, RelList = GetRelList(mappingRule, apiDescription, routeRelations[apiDescription.ID]) }); return(result); }
public void TestNotFromBodyNotGet() { var sub = new DefaultRouteValueSubstitution(); Expression <Func <ControllerSample, ModelSample, ModelSample> > lambda = (c, m) => c.ControllerMethod(m.Id, m.Name, QueryParameter.Is <string>(), QueryParameter.Is <int>()); var methodCallExpression = (MethodCallExpression)lambda.Body; var apiExplorerMoq = new Mock <IApiExplorer>(); apiExplorerMoq.Setup(_ => _.ApiDescriptions).Returns(new Collection <ApiDescription>() { new ApiDescription() { } }); var mr = new MappingRule(methodCallExpression, apiExplorerMoq.Object); var payload = _fixture.CreateAnonymous <ModelSample>(); var fields = ActionFieldsGenerator.Generate(mr, apiExplorerMoq.Object.ApiDescriptions[0], payload); Assume.That(fields, Is.Null); }