mockhttp icon indicating copy to clipboard operation
mockhttp copied to clipboard

Feature Query String Matcher Comparisons

Open pbolduc opened this issue 1 year ago • 0 comments

This is initial version of being able to add string comparisons for query strings. It is to address #134. There is still more work that will need to be done before this should be merged. Additional unit tests and examples should be added as well. I still need to add optional parameters to the extension methods. Please consider this PR as draft. I would appreciate any feedback on the design and any changes you would like to see.

Example

Proposed extension method change:

public static MockedRequest WithQueryString(this MockedRequest source, string name, string value, QueryStringMatcherOptions? options = null);

Usage

mockHttp.Expect("/users/me")
        .WithQueryString("access_token", "Old_Token", options: new QueryStringMatcherOptions(value: StringComparer.OrdinalIgnoreCase))
        .Respond(HttpStatusCode.Unauthorized);

QueryStringMatcherOptions

/// <summary>
/// Provides options on how to match query strings.
/// </summary>
public sealed class QueryStringMatcherOptions
{
    /// <summary>
    /// Constructs a new instance of QueryStringMatcherOptions using the default ordinal comparison on keys and values.
    /// </summary>
    public QueryStringMatcherOptions() : this(StringComparer.Ordinal, StringComparer.Ordinal)
    {
    }

    /// <summary>
    /// Constructs a new instance of QueryStringMatcherOptions using the default ordinal comparison on keys and values.
    /// </summary>
    public QueryStringMatcherOptions(IEqualityComparer<string>? key = null, IEqualityComparer<string>? value = null)
    {
        KeyComparer = key ?? StringComparer.Ordinal;
        ValueComparer = value ?? StringComparer.Ordinal;
    }

    /// <summary>
    /// The comparer to use for keys
    /// </summary>
    public IEqualityComparer<string> KeyComparer { get; }

    /// <summary>
    /// The comparer to use for values
    /// </summary>
    public IEqualityComparer<string> ValueComparer { get; }
}

pbolduc avatar Feb 17 '24 22:02 pbolduc