public void ТоЖдемПокаБудетРавен(string selectTokenPath, ExpectedJToken expectation)
        {
            var timeout  = 240;
            var complete = false;

            while (!complete)
            {
                ProcessSimpleApiResponse(httpClientFixture.SendRequest(lastRequestHttpMethod, lastRequestPathWithQueryParams));
                try
                {
                    var targetToken = JTokenParser.ParseJTokenFromJObject(selectTokenPath,
                                                                          (JObject)scenarioContext.GetActualJsonResponse());

                    JTokenAssertion.AssertJTokenEquals(targetToken, expectation, selectTokenPath);

                    complete = true;
                }
                catch
                {
                    // ignored
                }
                finally
                {
                    if (timeout <= 0)
                    {
                        throw new Exception($"Не удалось дождаться результата {selectTokenPath}={expectation.JToken} от запроса {lastRequestHttpMethod} {lastRequestPathWithQueryParams}");
                    }
                }

                Thread.Sleep(1000);
                timeout--;
            }
        }
Esempio n. 2
0
        internal static List <string> Match(dynamic expectedBody, dynamic actualBody, MatchingRuleCollection matchingRules)
        {
            var differences = new List <string>();

            if (expectedBody == null)
            {
                return(differences);
            }

            if (actualBody == null)
            {
                differences.Add("Expected body or contents to be present, but was not.");
                return(differences);
            }

            JToken expectedRootToken = JTokenParser.Parse(expectedBody);
            JToken actualRootToken   = JTokenParser.Parse(actualBody);

            foreach (var token in expectedRootToken.ThisTokenAndAllItsDescendants())
            {
                differences.AddRange(ProcessToken(token, matchingRules, actualRootToken));
            }

            differences.AddRange(VerifyAdditionalActualTokensAgainstMatchingRules(expectedRootToken, actualRootToken, matchingRules));

            return(differences);
        }
Esempio n. 3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jToken = (JToken)serializer.Deserialize(reader);
            var parser = new JTokenParser(jToken);

            return(parser.Parse());
        }
Esempio n. 4
0
        private static List <string> CompareExpectedTokenWithActual <T>(JToken expectedToken, MatchingRuleCollection matchingRules, JToken actualRootToken) where T : IEquatable <T>
        {
            var expectedValue = expectedToken.Value <T>();
            var actualToken   = actualRootToken.SelectToken(expectedToken.Path);

            if (actualToken == null)
            {
                var actualRootTokenWithLowerCasePropertyNames = JTokenParser.ParseToLower(actualRootToken);
                if (actualRootTokenWithLowerCasePropertyNames.SelectToken(expectedToken.Path.ToLowerInvariant()) != null)
                {
                    return(new List <string> {
                        $"A property with a name like \'{expectedToken.Path}\' was present in the actual response, but the case did not match. Note that Pact is case sensitive."
                    });
                }
                return(new List <string> {
                    $"Property \'{expectedToken.Path}\' was not present in the actual response."
                });
            }

            if (expectedToken.Type != actualToken.Type)
            {
                return(new List <string> {
                    $"Property \'{expectedToken.Path}\' has a different type in the actual response. Expected value: {expectedToken}, actual value: {actualToken}"
                });
            }

            var actualValue = actualToken.Value <T>();

            if (expectedToken.Type != JTokenType.Null && actualValue == null)
            {
                return(new List <string> {
                    $"Expected \'{expectedValue}\' at \'{expectedToken.Path}\', but had no value."
                });
            }
            if (matchingRules != null && matchingRules.TryGetApplicableMatcherListForToken(expectedToken, out var matcherList))
            {
                return(matcherList.Match(expectedToken, actualToken));
            }
            else if (expectedToken.Type != actualToken.Type)
            {
                return(new List <string> {
                    $"Expected value of type {expectedToken.Type} (like: \'{expectedToken.ToString()}\') at \'{expectedToken.Path}\', but was value of type {actualToken.Type}."
                });
            }
            else if (!actualValue.Equals(expectedValue))
            {
                return(new List <string> {
                    $"Expected \'{expectedValue}\' at \'{expectedToken.Path}\', but was \'{actualValue}\'."
                });
            }

            return(new List <string>());
        }