public PactnetVerificationResult Verify(Interaction interaction)
        {
            PactnetVerificationResult result = new PactnetVerificationResult();

            var logs = new PactLogBuilder();


            try
            {
                interaction = ApplyRequestFilters(interaction);

                HttpRequestMessage request = DesignRequestForInteraction(interaction);

                HttpClient client = new HttpClient {
                    BaseAddress = new Uri(_serviceUri)
                };

                var response = client.SendAsync(request).Result;

                result.Success = Compare(interaction.Response, response, logs);

                result.Exception = result.Success ? null : new Exception("Unmatched Response Exception");
            }
            catch (Exception e)
            {
                result.Success = false;

                result.Exception = e;
            }

            result.Logs = logs.ToString();

            return(result);
        }
Exemple #2
0
        public bool IsMatch(Dictionary <string, MatchingRule> ruleset,
                            Dictionary <string, object> expected,
                            Dictionary <string, object> actual,
                            PactLogBuilder log)
        {
            var finder = new RuleFinder();
            var match  = true;

            foreach (var expectedKey in expected.Keys)
            {
                if (!actual.ContainsKey(expectedKey))
                {
                    match = false;

                    log.NotFound(expectedKey);
                }
                else
                {
                    var expectedValue = expected[expectedKey];
                    var actualValue   = actual[expectedKey];
                    var rule          = finder.FindRule(expectedKey, ruleset);

                    if (!IsMatch(rule, expectedValue, actualValue))
                    {
                        match = false;

                        log.Unmatched(expectedValue, actualValue, rule);
                    }
                }
            }
            return(match);
        }
        private bool VerifyHeaders(Dictionary <string, MatchingRule> expectationsMatchingRules,
                                   Dictionary <string, string> expectations, HttpResponseHeaders actuals,
                                   PactLogBuilder log)
        {
            var expectedHeaders = new DataConvert().Normalize(expectations);
            var actualHeaders   = new DataConvert().Normalize(actuals);

            var matcher = new Matcher();

            return(matcher.IsMatch(expectationsMatchingRules, expectedHeaders, actualHeaders, log));
        }
        private bool Compare(PactResponse expectations, HttpResponseMessage actual, PactLogBuilder log)
        {
            var success = true;

            if (expectations.Status != 0)
            {
                if ((int)actual.StatusCode != expectations.Status)
                {
                    log.Unmatched(expectations.Status, actual.StatusCode);

                    success = false;
                }
            }

            var ruleSet = expectations.MatchingRules ?? new Dictionary <string, MatchingRule>();

            success &= VerifyHeaders(ruleSet, expectations.Headers, actual.Headers, log);

            success &= VerifyBodies(ruleSet, expectations.Body, actual.Content, log);

            return(success);
        }
Exemple #5
0
        public bool IsMatch(Dictionary <string, MatchingRule> ruleset,
                            Dictionary <string, List <string> > expected,
                            Dictionary <string, List <string> > actual,
                            PactLogBuilder log)
        {
            bool match = true;

            var ruleFinder = new RuleFinder();

            foreach (var keyValuePair in expected)
            {
                var corresponding = Find(actual, keyValuePair.Key);

                if (corresponding.Key == null)
                {
                    log.NotFound(corresponding.Key);

                    match = false;
                }
                else
                {
                    var path = $"$.headers.{keyValuePair.Key}";

                    var expectedMatchingRule = ruleFinder.FindRule(path, ruleset);

                    foreach (var expectedValue in keyValuePair.Value)
                    {
                        if (!ContainsMatch(expectedMatchingRule, expectedValue, corresponding.Value))
                        {
                            match = false;

                            log.NotFound(path);
                        }
                    }
                }
            }
            return(match);
        }
        private bool VerifyBodies(Dictionary <string, MatchingRule> expectationsMatchingRules,
                                  Hashtable expectationsBody, HttpContent actualContent, PactLogBuilder log)
        {
            var actualJson = actualContent.ReadAsStringAsync().Result;

            var actualHashTable = JsonConvert.DeserializeObject <Hashtable>(actualJson);

            var expectedDic = new DynamicObjectAccess(true).Flatten(expectationsBody, "$.body");

            var actualDic = new DynamicObjectAccess(true).Flatten(actualHashTable, "$.body");

            var matcher = new Matcher();

            return(matcher.IsMatch(expectationsMatchingRules, expectedDic, actualDic, log));
        }