EasyPost/easyvcr-csharp

Match by absolute path and query

Closed this issue · 3 comments

kjbkjb commented

Having come from RoR world and using VCR, I was pleased to discover your library. It really is 'easy'. Thank you for this.

I have a bit of a 'special' situation where we have an API fixture that is maintained by a third party and uses Prism and OpenApi. Our internal build system has the capability of standing up this fixture in a docker container dynamically during a test run. Basically our ports are always different. So, I was hoping to have a way to match on AbsolutePath and Query, without the scheme, host and port. That isn't a feature of your matching library currently.

I'm willing to submit this in PR form if you give me push access. For now, I'm publishing a private version for my app to use.

I would love to see the following match rules:

// This is an extracted method from elsewhere in your class
        /// <summary>
        ///   Convenience method to parse and compare the query component of a uri
        /// </summary>
        /// <param name="received">Received request</param>
        /// <param name="recorded">Recorded request</param>
        /// <returns>bool</returns>
        private static bool QueryMatch(Request received, Request recorded)
        {
            var receivedQuery = new Uri(received.Uri ?? string.Empty).Query;
            var recordedQuery = new Uri(recorded.Uri ?? string.Empty).Query;
            var receivedQueryDict = HttpUtility.ParseQueryString(receivedQuery);
            var recordedQueryDict = HttpUtility.ParseQueryString(recordedQuery);
            return receivedQueryDict.Count == recordedQueryDict.Count &&
                   receivedQueryDict.AllKeys.All(key => receivedQueryDict[key] == recordedQueryDict[key]);
        }

 /// <summary>
        ///   Add a rule to compare the absolute paths of the requests.
        /// </summary>
        /// <returns>The same MatchRules object.</returns>
        public MatchRules ByAbsolutePath()
        {
            By((received, recorded) =>
            {
                var receivedUri = new Uri(received.Uri ?? string.Empty);
                var recordedUri = new Uri(recorded.Uri ?? string.Empty);
                return receivedUri.AbsolutePath.Equals(recordedUri.AbsolutePath, StringComparison.OrdinalIgnoreCase);
            });
            return this;
        }

        /// <summary>
        ///   Add a rule to compare the query parameters of the requests.
        /// </summary>
        /// <param name="exact"></param>
        /// <returns>The same MatchRules object.</returns>
        public MatchRules ByQuery(bool exact = false)
        {
            By(QueryMatch);
            return this;
        }
        
        /// <summary>
        ///   Add rules for AbsolutePath and Query
        /// </summary>
        /// <returns></returns>
        public MatchRules ByPathAndQuery()
        {
            ByAbsolutePath().ByQuery();
            return this;
        }

Hello @kjbkjb , thanks for reaching out! We're always open to pull requests on our open-source projects, so feel free to open one on our EasyVCR repository. You shouldn't need push access to our repository; you can simply open a PR merging the code from your fork into our repository. We'll review it and merge it if it looks good.

I'm going to go ahead and transfer this issue over to that repository as well.

Thanks again for your contribution!

kjbkjb commented

Once I've played around with it a bit, I'll do so!

Hey @kjbkjb, wanted to let you know we just released v0.9.0 which has new ByQuery() and ByPath() match rules, as well as a new ByCustomRule() option that will allow you to define your own match rules in the future.

I'm going to go ahead and close this ticket. Let us know if you need anything else, and thanks for using EasyVCR!