public async Task Rejects_InvalidRouteQueryParameter(string name, string value, QueryParameterMatchMode mode, string error) { var routeQueryParameter = new RouteQueryParameter() { Name = name, Mode = mode, Values = value is null ? null : new[] { value }, }; var route = new RouteConfig { RouteId = "route1", Match = new RouteMatch { Path = "/", QueryParameters = new[] { routeQueryParameter }, }, ClusterId = "cluster1", }; var services = CreateServices(); var validator = services.GetRequiredService <IConfigValidator>(); var result = await validator.ValidateRouteAsync(route); var ex = Assert.Single(result); Assert.Contains(error, ex.Message); }
/// <summary> /// Creates a new instance. /// </summary> public QueryParameterMatcher(string name, IReadOnlyList <string>?values, QueryParameterMatchMode mode, bool isCaseSensitive) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("A query parameter name is required.", nameof(name)); } if (mode != QueryParameterMatchMode.Exists && (values is null || values.Count == 0)) { throw new ArgumentException("Query parameter values must have at least one value.", nameof(values)); } if (mode == QueryParameterMatchMode.Exists && values?.Count > 0) { throw new ArgumentException($"Query parameter values must not be specified when using '{nameof(QueryParameterMatchMode.Exists)}'.", nameof(values)); } if (values is not null && values.Any(string.IsNullOrEmpty)) { throw new ArgumentNullException(nameof(values), "Query parameter values must not be empty."); } Name = name; Values = values?.ToArray() ?? Array.Empty <string>(); Mode = mode; Comparison = isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; }
private static bool MatchQueryParameter(QueryParameterMatchMode matchMode, string requestQueryParameterValue, string metadataQueryParameterValue, bool isCaseSensitive) { var comparison = isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; return(matchMode switch { QueryParameterMatchMode.Exact => MemoryExtensions.Equals(requestQueryParameterValue, metadataQueryParameterValue, comparison), QueryParameterMatchMode.Prefix => requestQueryParameterValue != null && metadataQueryParameterValue != null && MemoryExtensions.StartsWith(requestQueryParameterValue, metadataQueryParameterValue, comparison), QueryParameterMatchMode.Contains => requestQueryParameterValue != null && metadataQueryParameterValue != null && MemoryExtensions.Contains(requestQueryParameterValue, metadataQueryParameterValue, comparison), QueryParameterMatchMode.NotContains => requestQueryParameterValue != null && metadataQueryParameterValue != null && !MemoryExtensions.Contains(requestQueryParameterValue, metadataQueryParameterValue, comparison), _ => throw new NotImplementedException(matchMode.ToString()), });
/// <summary> /// Creates a new instance. /// </summary> public QueryParameterMatcher(string name, IReadOnlyList <string>?values, QueryParameterMatchMode mode, bool isCaseSensitive) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("A query parameter name is required.", nameof(name)); } if (mode != QueryParameterMatchMode.Exists && (values == null || values.Count == 0)) { throw new ArgumentException("Query parameter values must have at least one value.", nameof(values)); } if (mode == QueryParameterMatchMode.Exists && values?.Count > 0) { throw new ArgumentException($"Query parameter values must not be specified when using '{nameof(QueryParameterMatchMode.Exists)}'.", nameof(values)); } Name = name; Values = values ?? Array.Empty <string>(); Mode = mode; IsCaseSensitive = isCaseSensitive; }