diff --git a/README.md b/README.md index 2490bd3..09da975 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,13 @@ If, for example, you want to run only your UI tests ending with `ui`, you can ac ./http-api-tester -g ".*ui$" example-suite ``` +### Comparing with greater-or-equal + +This option was introduced to cope with changing (growing) timestamps that may be part of your json answer. +```sh +./http-api-tester -o ge example-suite-ge/ +``` + ## Installation Make sure to have your test suites ready in your project directory and the requirements set up. To run the tool, just execute: diff --git a/example-suite-ge/after-all b/example-suite-ge/after-all new file mode 100755 index 0000000..faef050 --- /dev/null +++ b/example-suite-ge/after-all @@ -0,0 +1,2 @@ +#!/bin/bash +echo " - starting something needed for all test cases" diff --git a/example-suite-ge/after-each b/example-suite-ge/after-each new file mode 100755 index 0000000..1e56e46 --- /dev/null +++ b/example-suite-ge/after-each @@ -0,0 +1,2 @@ +#!/bin/bash +echo " - do something after each test" diff --git a/example-suite-ge/before-all b/example-suite-ge/before-all new file mode 100755 index 0000000..bd88372 --- /dev/null +++ b/example-suite-ge/before-all @@ -0,0 +1,2 @@ +#!/bin/bash +echo " - do something before each test" diff --git a/example-suite-ge/before-each b/example-suite-ge/before-each new file mode 100755 index 0000000..bd88372 --- /dev/null +++ b/example-suite-ge/before-each @@ -0,0 +1,2 @@ +#!/bin/bash +echo " - do something before each test" diff --git a/example-suite-ge/greater-equal-compare/curl-options b/example-suite-ge/greater-equal-compare/curl-options new file mode 100644 index 0000000..0c3a873 --- /dev/null +++ b/example-suite-ge/greater-equal-compare/curl-options @@ -0,0 +1 @@ +-k diff --git a/example-suite-ge/greater-equal-compare/expected/http-status b/example-suite-ge/greater-equal-compare/expected/http-status new file mode 100644 index 0000000..08839f6 --- /dev/null +++ b/example-suite-ge/greater-equal-compare/expected/http-status @@ -0,0 +1 @@ +200 diff --git a/example-suite-ge/greater-equal-compare/expected/response-data b/example-suite-ge/greater-equal-compare/expected/response-data new file mode 100644 index 0000000..8f5907a --- /dev/null +++ b/example-suite-ge/greater-equal-compare/expected/response-data @@ -0,0 +1,4 @@ +{ + "b" : [1, 2], + "a" : 24 +} diff --git a/example-suite-ge/greater-equal-compare/expected/response-data.json b/example-suite-ge/greater-equal-compare/expected/response-data.json new file mode 100644 index 0000000..ad8072f --- /dev/null +++ b/example-suite-ge/greater-equal-compare/expected/response-data.json @@ -0,0 +1,23 @@ +[ + { + "id": "http-server", + "description": "A simple Python HTTP request handler. This class serves files from the current directory and below, directly mapping the directory structure to HTTP requests.", + "parameters": [ + "id" + ] + }, + { + "id": "jupyter", + "description": "Open source, interactive data science and scientific computing across over 40 programming languages.", + "parameters": [ + "id" + ] + }, + { + "id": "zeppelin", + "description": "A web-based notebook that enables interactive data analytics.\nYou can make beautiful data-driven, interactive and collaborative documents with SQL, Scala and more.", + "parameters": [ + "id" + ] + } +] diff --git a/example-suite-ge/greater-equal-compare/method b/example-suite-ge/greater-equal-compare/method new file mode 100644 index 0000000..28760e6 --- /dev/null +++ b/example-suite-ge/greater-equal-compare/method @@ -0,0 +1 @@ +GET diff --git a/example-suite-ge/greater-equal-compare/request-data b/example-suite-ge/greater-equal-compare/request-data new file mode 100644 index 0000000..e69de29 diff --git a/example-suite-ge/greater-equal-compare/request-header b/example-suite-ge/greater-equal-compare/request-header new file mode 100644 index 0000000..0a92755 --- /dev/null +++ b/example-suite-ge/greater-equal-compare/request-header @@ -0,0 +1,2 @@ +X-MyHeader: 123 +X-YourHeader: 321 diff --git a/example-suite-ge/greater-equal-compare/response-data.actual b/example-suite-ge/greater-equal-compare/response-data.actual new file mode 100644 index 0000000..8dcf096 --- /dev/null +++ b/example-suite-ge/greater-equal-compare/response-data.actual @@ -0,0 +1,4 @@ +{ + "a" : 42, + "b" : [1, 2] +} diff --git a/example-suite-ge/greater-equal-compare/url b/example-suite-ge/greater-equal-compare/url new file mode 100644 index 0000000..a6c6906 --- /dev/null +++ b/example-suite-ge/greater-equal-compare/url @@ -0,0 +1 @@ +localhost:8080/example-suite-ge/greater-equal-compare/response-data.actual diff --git a/example-suite-ge/response-date.actual b/example-suite-ge/response-date.actual new file mode 100644 index 0000000..a14269e --- /dev/null +++ b/example-suite-ge/response-date.actual @@ -0,0 +1,7 @@ +{ + "b": [ + 1, + 2 + ], + "a": 42 +} diff --git a/example-suite/correct-json-content/curl-options b/example-suite/correct-json-content/curl-options new file mode 100644 index 0000000..0c3a873 --- /dev/null +++ b/example-suite/correct-json-content/curl-options @@ -0,0 +1 @@ +-k diff --git a/http-api-tester b/http-api-tester index cf0bf40..594014f 100755 --- a/http-api-tester +++ b/http-api-tester @@ -13,6 +13,8 @@ function print_usage { echo " show diff in case of test failure" echo " -g|--grep \"\"" echo " only execute test cases matching the regex" + echo " -o|--operand \"\"" + echo " match equal, greater-or-equal than, or less-or-equal than (in jq comparison of json)" if [ -z "$1" ]; then exit 1 else @@ -41,6 +43,10 @@ while [[ $# -gt 1 ]]; do grep=$2 shift 1 ;; + -o|--operand) + operand=$2 + shift 1 + ;; *) print_usage ;; @@ -92,6 +98,11 @@ function test { return fi + if [[ -z $operand ]] ; then + operand=eq + fi + echo -e " comparison uses operand $operand" + echo -e " \033[1m$run_folder\033[0m:" if [ ! -z "$clean" ]; then @@ -164,8 +175,19 @@ function test { function compare { local actual="$actual_folder/$1" local expected="$expected_folder/$1" - jq -e --argfile actual $actual --argfile expected $expected -n '$actual == $expected' > /dev/null - local status=$? + if [ $operand == "eq" ]; then + jq -e --argfile actual $actual --argfile expected $expected -n '$actual == $expected' > /dev/null + local status=$? + elif [ $operand == "ge" ]; then + jq -e --argfile actual $actual --argfile expected $expected -n '$actual >= $expected' > /dev/null + local status=$? + elif [ $operand == "le" ]; then + jq -e --argfile actual $actual --argfile expected $expected -n '$actual <= $expected' > /dev/null + local status=$? + else + echo "Don't know what to do with operand $operand, exiting"; + exit 2 + fi if [ $status -ne 0 ]; then echo -e " - \033[0;31m$actual did not match $expected\033[0m" >&2 if [ ! -z "$detail" ]; then