Example #1
0
        public static string ValueForKeyedIdentifier(this HttpResponse response, string key)
        {
            var keyType = BasicRequestDefinition.LocationForKey(key);

            switch (keyType)
            {
            case PlaceholderLocation.Body:
                return(response.Body);

            case PlaceholderLocation.HttpHeader:
                string headerTrueName = key.Substring(0, key.Length - 1);
                if (response.Headers.AllKeys.Contains(headerTrueName))
                {
                    return(response.Headers[headerTrueName]);
                }
                return(null);

            case PlaceholderLocation.Json:
                if (!response.ContentType.MatchesContentTypeIdentifier(MethodDefinition.MimeTypeJson))
                {
                    throw new NotSupportedException(string.Format("Cannot read JPath property from response with content-type: {0}", response.ContentType));
                }

                return(JsonPath.ValueFromJsonPath(response.Body, key).ToString());

            default:
                throw new NotSupportedException(string.Format("Unsupported location for keyed identifier {0}: {1}", key, keyType));
            }
        }
Example #2
0
        public void JsonPathRootObject()
        {
            var json  = this.GetJson();
            var value = JsonPath.ValueFromJsonPath(json, "$");

            var resultJson = JsonConvert.SerializeObject(value);

            Assert.AreEqual(json, resultJson);
        }
        /// <summary>
        /// Verify that the expectations in the scenario are met by the response
        /// </summary>
        /// <param name="scenario"></param>
        /// <param name="actualResponse"></param>
        /// <param name="detectedErrors"></param>
        public static void ValidateExpectations(this ScenarioDefinition scenario, HttpResponse actualResponse, IssueLogger issues)
        {
            if (scenario == null)
            {
                throw new ArgumentNullException("scenario");
            }
            if (actualResponse == null)
            {
                throw new ArgumentNullException("actualResponse");
            }
            if (issues == null)
            {
                throw new ArgumentNullException("issues");
            }

            var expectations = scenario.Expectations;

            if (null == expectations || expectations.Count == 0)
            {
                return;
            }

            foreach (string key in expectations.Keys)
            {
                string keyIndex;
                var    type           = BasicRequestDefinition.LocationForKey(key, out keyIndex);
                object expectedValues = expectations[key];
                switch (type)
                {
                case PlaceholderLocation.Body:
                    ExpectationSatisfied(key, actualResponse.Body, expectedValues, issues);
                    break;

                case PlaceholderLocation.HttpHeader:
                    ExpectationSatisfied(key, actualResponse.Headers[keyIndex], expectedValues, issues);
                    break;

                case PlaceholderLocation.Json:
                    try
                    {
                        object value = JsonPath.ValueFromJsonPath(actualResponse.Body, keyIndex);
                        ExpectationSatisfied(key, value, expectedValues, issues);
                    }
                    catch (Exception ex)
                    {
                        issues.Error(ValidationErrorCode.JsonParserException, string.Empty, ex);
                    }
                    break;

                case PlaceholderLocation.Invalid:
                case PlaceholderLocation.StoredValue:
                case PlaceholderLocation.Url:
                    issues.Warning(ValidationErrorCode.InvalidExpectationKey, $"The expectation key {key} is invalid. Supported types are Body, HttpHeader, and JsonPath.");
                    break;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Verify that the expectations in the scenario are met by the response
        /// </summary>
        /// <param name="scenario"></param>
        /// <param name="actualResponse"></param>
        /// <param name="detectedErrors"></param>
        public static void ValidateExpectations(this ScenarioDefinition scenario, HttpResponse actualResponse, List <ValidationError> detectedErrors)
        {
            if (scenario == null)
            {
                throw new ArgumentNullException("scenario");
            }
            if (actualResponse == null)
            {
                throw new ArgumentNullException("actualResponse");
            }
            if (detectedErrors == null)
            {
                throw new ArgumentNullException("detectedErrors");
            }

            var expectations = scenario.Expectations;

            if (null == expectations || expectations.Count == 0)
            {
                return;
            }

            foreach (string key in expectations.Keys)
            {
                string keyIndex;
                var    type           = BasicRequestDefinition.LocationForKey(key, out keyIndex);
                object expectedValues = expectations[key];
                switch (type)
                {
                case PlaceholderLocation.Body:
                    ExpectationSatisfied(key, actualResponse.Body, expectedValues, detectedErrors);
                    break;

                case PlaceholderLocation.HttpHeader:
                    ExpectationSatisfied(key, actualResponse.Headers[keyIndex].FirstOrDefault(), expectedValues, detectedErrors);
                    break;

                case PlaceholderLocation.Json:
                    try
                    {
                        object value = JsonPath.ValueFromJsonPath(actualResponse.Body, keyIndex);
                        ExpectationSatisfied(key, value, expectedValues, detectedErrors);
                    }
                    catch (Exception ex)
                    {
                        detectedErrors.Add(new ValidationError(ValidationErrorCode.JsonParserException, null, ex.Message));
                    }
                    break;

                case PlaceholderLocation.Invalid:
                case PlaceholderLocation.StoredValue:
                case PlaceholderLocation.Url:
                    detectedErrors.Add(new ValidationWarning(ValidationErrorCode.InvalidExpectationKey, null, "The expectation key {0} is invalid. Supported types are Body, HttpHeader, and JsonPath.", key));
                    break;
                }
            }
        }
Example #5
0
        public void JsonPathSecondLevelObjectValue()
        {
            var value = JsonPath.ValueFromJsonPath(this.GetJson(), "$.thumbnails.small");

            dynamic obj = this.GetJsonObject();
            var     smallThumbnailObject = obj["thumbnails"].small;

            var foundObjectJson   = JsonConvert.SerializeObject(value);
            var dynamicObjectJson = JsonConvert.SerializeObject(smallThumbnailObject);

            Assert.AreEqual(dynamicObjectJson, foundObjectJson);
        }
Example #6
0
        public void JsonPathArrayTest()
        {
            var value = JsonPath.ValueFromJsonPath(this.GetJson(), "$.children[0]");

            dynamic obj        = this.GetJsonObject();
            var     firstChild = obj["children"][0];

            var foundObjectJson   = JsonConvert.SerializeObject(value);
            var dynamicObjectJson = JsonConvert.SerializeObject(firstChild);

            Assert.AreEqual(dynamicObjectJson, foundObjectJson);
        }
Example #7
0
        public void JsonPathTopLevelValue()
        {
            var value = JsonPath.ValueFromJsonPath(this.GetJson(), "$.id");

            Assert.AreEqual("1234", value);
        }
Example #8
0
 public void JsonPathInvalidPath()
 {
     JsonPath.ValueFromJsonPath(this.GetJson(), "$.nothing.foo");
 }
Example #9
0
        public void JsonPathWithPeriodInPropertyNameTest()
        {
            var value = JsonPath.ValueFromJsonPath(this.GetJson(), "$.['@content.downloadUrl']");

            Assert.AreEqual("https://foobar.com/something", value);
        }
Example #10
0
        public void JsonPathArrayWithSecondLevelTest()
        {
            var value = JsonPath.ValueFromJsonPath(this.GetJson(), "$.children[0].name");

            Assert.AreEqual("first_file.txt", value);
        }
Example #11
0
        public void JsonPathThirdLevelValue()
        {
            var value = JsonPath.ValueFromJsonPath(this.GetJson(), "$.thumbnails.small.url");

            Assert.AreEqual("http://small", value);
        }
Example #12
0
 public void JsonPathInvalidPath()
 {
     Assert.Throws <JsonPathException>(() => JsonPath.ValueFromJsonPath(this.GetJson(), "$.nothing.foo"));
 }