public override IEnumerable <string> GetTemplates <TController>(TestServerAction action, TestServerTokenCollection tokens) { var getAttributes = action.MethodInfo.GetCustomAttributes <HttpPostAttribute>() .OfType <HttpMethodAttribute>(); var postAttributes = action.MethodInfo.GetCustomAttributes <HttpGetAttribute>() .OfType <HttpMethodAttribute>(); var putAttributes = action.MethodInfo.GetCustomAttributes <HttpPutAttribute>() .OfType <HttpMethodAttribute>(); var deleteAttributes = action.MethodInfo.GetCustomAttributes <HttpDeleteAttribute>() .OfType <HttpMethodAttribute>(); var verbAttribute = getAttributes .Union(postAttributes) .Union(putAttributes) .Union(deleteAttributes) .SingleOrDefault(); if (verbAttribute?.Template != null) { return(new List <string>() { SubstituteTokens(verbAttribute.Template.ToLowerInvariant(), tokens) }); } return(Enumerable.Empty <string>()); }
public void AddTokens <TController>(TestServerAction action, TestServerTokenCollection tokens) where TController : class { var parameters = action.MethodInfo.GetParameters(); for (int i = 0; i < parameters.Length; i++) { var type = parameters[i].ParameterType; var instance = action.ArgumentValues.Any(x => x.Key == i) ? action.ArgumentValues[i].Instance : null; if (instance != null && !type.IsPrimitiveType()) { if (!IgnoreBind(parameters[i])) { foreach (var property in type.GetProperties()) { var tokenName = property.Name.ToLowerInvariant(); var value = property.GetValue(instance); if (value != null) { tokens.AddToken(tokenName, value.ToString(), isConventional: false); } } } } } }
private static TestServerAction GetTestServerAction <TController>(Expression <Func <TController, object> > actionSelector) { if (actionSelector.NodeType != ExpressionType.Lambda) { throw new InvalidOperationException($"The action selector is not a valid lambda expression"); } var methodCall = (MethodCallExpression)actionSelector.Body; var action = new TestServerAction(methodCall.Method); bool haveAttributeApiController = typeof(TController).GetTypeInfo().GetCustomAttribute(typeof(ApiControllerAttribute)) != null; bool isGetOrDelete = action.MethodInfo.GetCustomAttributes().FirstOrDefault(attr => attr.GetType() == typeof(HttpGetAttribute) || attr.GetType() == typeof(HttpDeleteAttribute)) != null; var index = 0; foreach (var item in methodCall.Arguments) { action.AddArgument(index, item, haveAttributeApiController && !isGetOrDelete); ++index; } return(action); }
public override IEnumerable <string> GetTemplates <TController>(TestServerAction action, TestServerTokenCollection tokens) { var builder = new StringBuilder(); var unusedTokens = tokens.GetUnusedTokens(); if (unusedTokens.Any()) { foreach (var token in unusedTokens) { builder.Append($"{token.Name}={token.Value}&"); } builder.Length -= 1; return(new List <string>() { $"?{builder}" }); } else { return(new List <string>() { string.Empty }); } }
public override IEnumerable <string> GetTemplates <TController>(TestServerAction action, TestServerTokenCollection tokens) { var routeDefinitions = action.MethodInfo.GetCustomAttributes <RouteAttribute>(); return(routeDefinitions.Select(route => { return SubstituteTokens(route.Template.ToLowerInvariant(), tokens); })); }
private static void AddFromHeaderArgumentsToRequestForm( RequestBuilder requestBuilder, TestServerAction action) { var fromHeaderArguments = action.ArgumentValues.Values.Where(x => x.IsFromHeader); foreach (var fromHeaderArgument in fromHeaderArguments) { requestBuilder.And(x => x.Headers.Add(fromHeaderArgument.HeaderName, fromHeaderArgument.Instance.ToString())); } }
private static void AddFromBodyArgumentsToRequestBody( RequestBuilder requestBuilder, TestServerAction action, RequestContentOptions contentOptions) { var fromBodyArgument = action.ArgumentValues.Values.SingleOrDefault(x => x.IsBody); if (fromBodyArgument != null) { requestBuilder.And(x => x.Content = contentOptions.ContentBuilder(fromBodyArgument.Instance)); } }
public override IEnumerable <string> GetTemplates <TController>(TestServerAction action, TestServerTokenCollection tokens) { if (!typeof(TController).IsDefined(typeof(RouteAttribute))) { return(Enumerable.Empty <string>()); } var routeDefinitions = typeof(TController) .GetCustomAttributes <RouteAttribute>(); return(routeDefinitions.Select(route => { return SubstituteTokens(route.Template.ToLowerInvariant(), tokens); })); }
public void AddTokens <TController>(TestServerAction action, TestServerTokenCollection tokens) where TController : class { const string ControllerTypeNameSuffix = "Controller"; const string controller_key = "controller"; if (!tokens.ContainsToken(controller_key)) { var controllerName = typeof(TController).Name .Replace(ControllerTypeNameSuffix, String.Empty); tokens.AddToken(controller_key, controllerName, isConventional: true); } }
public void AddTokens <TController>(TestServerAction action, TestServerTokenCollection tokens) where TController : class { var parameters = action.MethodInfo.GetParameters(); for (int i = 0; i < parameters.Length; i++) { if (parameters[i].ParameterType.IsPrimitive) { var tokenName = parameters[i].Name.ToLowerInvariant(); var tokenValue = action.ArgumentValues[i]; tokens.AddToken(tokenName, tokenValue.ToString(), isConventional: false); } } }
private static TestServerAction GetTestServerAction <TController>(Expression <Func <TController, object> > actionSelector) { if (actionSelector.NodeType != ExpressionType.Lambda) { throw new InvalidOperationException($"The action selector is not a valid lambda expression"); } var methodCall = (MethodCallExpression)actionSelector.Body; var action = new TestServerAction(methodCall.Method); int index = 0; foreach (var item in methodCall.Arguments) { action.AddArgument(index, item); ++index; } return(action); }
public void AddTokens <TController>(TestServerAction action, TestServerTokenCollection tokens) where TController : class { var parameters = action.MethodInfo.GetParameters(); for (int i = 0; i < parameters.Length; i++) { var type = parameters[i].ParameterType; if (!(type.IsPrimitive || type == typeof(String) || type == typeof(Decimal) || type == typeof(Guid))) { if (!IgnoreBind(parameters[i])) { foreach (var property in type.GetProperties()) { var tokenName = property.Name.ToLowerInvariant(); var value = property.GetValue(action.ArgumentValues[i].Instance); tokens.AddToken(tokenName, value.ToString(), isConventional: false); } } } } }
public void AddTokens <TController>(TestServerAction action, TestServerTokenCollection tokens) where TController : class { var parameters = action.MethodInfo.GetParameters(); for (var i = 0; i < parameters.Length; i++) { if ((parameters[i].ParameterType.IsPrimitive || parameters[i].ParameterType == typeof(string) || parameters[i].ParameterType == typeof(decimal) || parameters[i].ParameterType == typeof(Guid)) && !IgnoreHeader(parameters[i])) { var tokenName = parameters[i].Name.ToLowerInvariant(); var tokenValue = action.ArgumentValues[i].Instance; tokens.AddToken(tokenName, tokenValue.ToString(), isConventional: false); } } }
public void AddTokens <TController>(TestServerAction action, TestServerTokenCollection tokens) where TController : class { var parameters = action.MethodInfo.GetParameters(); for (var i = 0; i < parameters.Length; i++) { if (!IgnoreHeader(parameters[i])) { var tokenName = parameters[i].Name.ToLowerInvariant(); if (parameters[i].ParameterType.IsPrimitiveType()) { var tokenValue = action.ArgumentValues.Any(x => x.Key == i) ? action.ArgumentValues[i].Instance : null; if (tokenValue != null) { tokens.AddToken(tokenName, tokenValue.ToString(), isConventional: false); } } else if (parameters[i].ParameterType.IsArray && IsPrimitiveType(parameters[i].ParameterType.GetElementType())) { var arrayValues = (Array)action.ArgumentValues[i].Instance; if (arrayValues != null && arrayValues.Length != 0 ) { var tokenValue = GetTokenValue(arrayValues, tokenName); tokens.AddToken(tokenName, tokenValue, isConventional: false); } } } } }
public abstract IEnumerable <string> GetTemplates <TController>(TestServerAction action, TestServerTokenCollection tokens) where TController : class;