public virtual string SubstituteTokens(string template, TestServerTokenCollection tokens) { const string regex_pattern = "{[a-zA-Z0-9]*:??[a-zA-Z0-9]*}"; template = template.ToLowerInvariant(); foreach (var token in tokens.GetConventionalTokens()) { var conventional_token = $"[{token.Name}]"; if (template.Contains(conventional_token)) { template = template.Replace(conventional_token, token.Value); token.SetAsUsed(); } } var matches = Regex.Matches(template, regex_pattern, RegexOptions.Compiled); foreach (Match match in matches) { string parameter = null; if (match.Value.Contains(":")) { //has constraints parameter = match.Value.Split(':')[0] .Remove(0, 1); // remove first bracket } else { parameter = match.Value.Replace("{", string.Empty) .Replace("}", string.Empty); } var token = tokens.Find(parameter); if (token != default(TestServerToken)) { template = template.Replace(match.Value, token.Value); token.SetAsUsed(); } } return(template.ToLowerInvariant()); }
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); } } }
public virtual string SubstituteTokens(string template, TestServerTokenCollection tokens) { var regex_pattern = "{[a-zA-Z0-9?]*:??[a-zA-Z0-9]*}"; template = template.ToLowerInvariant(); foreach (var token in tokens.GetConventionalTokens()) { var conventional_token = $"[{token.Name}]"; if (template.Contains(conventional_token)) { template = template.Replace(conventional_token, token.Value); token.SetAsUsed(); } } var matches = Regex.Matches(template, regex_pattern, RegexOptions.Compiled); foreach (Match match in matches) { string parameter = null; regex_pattern = @"{([^\?:}]*)"; parameter = Regex.Match(match.Value, regex_pattern) .Groups .Values .LastOrDefault()? .Value; var token = tokens.Find(parameter); if (token != default(TestServerToken)) { template = template.Replace(match.Value, token.Value); token.SetAsUsed(); } } regex_pattern = @"\/{[^{}]*\?}"; template = Regex.Replace(template, regex_pattern, string.Empty); return(template.ToLowerInvariant()); }
static TestServerTokenCollection AddTokens <TController>(TestServerAction action, object tokenValues) where TController : class { var dictionaryTokenValues = new Dictionary <string, string>(); if (tokenValues != null) { dictionaryTokenValues = tokenValues.GetType() .GetProperties() .ToDictionary(p => p.Name.ToLowerInvariant(), p => p.GetValue(tokenValues).ToString()); } var testServerTokens = TestServerTokenCollection.FromDictionary(dictionaryTokenValues); foreach (var tokeniker in _tokenizers) { tokeniker.AddTokens <TController>(action, testServerTokens); } return(testServerTokens); }
public abstract IEnumerable <string> GetTemplates <TController>(TestServerAction action, TestServerTokenCollection tokens) where TController : class;
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 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 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); })); }
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 }); } }