HsacExamples.SlimTests.HttpTests.JsonHttpTest


Example usage of making HTTP calls to get results in JSON notation and using JsonPath to validate the response's content.

First a simple GET request.

script json http test
get from http://echo.jsontest.com/key/value/one/two
show response
{
    "one": "two",
    "key": "value"
}
check json path $.key value
check json path $.one two


Then a POST, showing the multiple notations of JsonPath expressions supported (notice that adding the '$.' prefix is not required).

variable defined: BAD_BODY= {{{ json={"key":"value" }}}

script json http test
post
json={"key":"value"
to http://validate.jsontest.com/
show response
{
    "error_info": "This error came from the org.json reference parser.",
    "error": "Expected a ',' or '}' at 15 [character 16 line 1]",
    "object_or_array": "object",
    "validate": false
}
check json path $.validate false
check json path validate false
check json path .validate false
check json path $['validate'] false
check json path ['validate'] false


Next another POST, showing that expressions resulting in a boolean or integer value are recognized and can be used accordingly by the script table. Furthermore expressions returning more than a single member are supported.

variable defined: GOOD_BODY= {{{ json={"key":"value"} }}}

script json http test
post
json={"key":"value"}
to http://validate.jsontest.com/
show response
{
    "size": 1,
    "parse_time_nanoseconds": 20721,
    "object_or_array": "object",
    "validate": true,
    "empty": false
}
check json path validate true
ensure json path validate
check json path size 1>0
check json path count $.* 5>=5
show all json path matches *
  • 1
  • 20721
  • object
  • true
  • false
check element 4 of json path $.* false


Next another POST, posting of url-enoded values defined in the wiki.

script json http test
set value {"key":"value 2"} for json
post values to http://validate.jsontest.com/
show response
{
    "size": 1,
    "parse_time_nanoseconds": 18966,
    "object_or_array": "object",
    "validate": true,
    "empty": false
}
check json path validate true

Next a PUT example
variable defined: PUT_BODY= {{{ json={"id": 1, "title": "foo", "body": "bar", "userId": 1} }}}

script json http test
put
json={"id": 1,
    "title": "foo",
    "body": "bar",
    "userId": 1}
to http://jsonplaceholder.typicode.com/posts/1/
show response <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="utf-8"> <title>Application Error</title> <style media="screen"> html,body,iframe { margin: 0; padding: 0; } html,body { height: 100%; overflow: hidden; } iframe { width: 100%; height: 100%; border: 0; } </style> </head> <body> <iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe> </body> </html>
check json path id [null] expected [>0]
check json path count $.* 0>=2
show all json path matches * null