diff --git a/check_results.go b/check_results.go index 678930e..10241c0 100644 --- a/check_results.go +++ b/check_results.go @@ -1,6 +1,48 @@ package main +import ( + "errors" + "fmt" + "strconv" + "strings" +) + func CheckResults(testCase *TestCase) (bool, error) { + // Check headers + for k, v := range testCase.Expect.Header.Contains { + fmt.Printf("Header contains check '%s'='%s'\n", k, v) + + if k == "http_status" { // http status is a special case + statusCode, err := strconv.Atoi(v) + if err != nil { + error := fmt.Sprintf("unable to convert http_status expected value '%s' to int : %s", v, err) + return false, errors.New(error) + } + if statusCode != testCase.ResultStatusCode { + return false, nil + } + } else { // any other contains check + checkFound := false + for hKey, hVal := range testCase.ResultHeaders { + if k == hKey { + fmt.Printf("Found header key '%s', checking value '%s' matches test definition '%s'\n", hKey, hVal, v) + + for i := range hVal { + if strings.Contains(hVal[i], v) { + fmt.Printf("Found match '%s' = '%s'\n", hVal[i], v) + checkFound = true + } + } + } + } + + if !checkFound { + fmt.Printf("Expected to find header key-value of '%s:%s' but no matching value was found\n", k, v) + return false, nil + } + } + } + return true, nil } diff --git a/main.go b/main.go index c8dcd7e..4f52d93 100644 --- a/main.go +++ b/main.go @@ -45,11 +45,16 @@ func main() { // Perform specified test cases for i := range testDefinitions.TestCases { RunTest(&testDefinitions.TestCases[i]) - success, _ := CheckResults(&testDefinitions.TestCases[i]) + success, err := CheckResults(&testDefinitions.TestCases[i]) + if err != nil { + fmt.Printf("Error running test case : %s\n", err) + os.Exit(1) + } if !success { fmt.Printf("Test result failed\n") os.Exit(1) } + } // For debugging, just dump the output diff --git a/tests.json b/tests.json index 318f9cf..f995c9a 100644 --- a/tests.json +++ b/tests.json @@ -11,8 +11,9 @@ }, "expect": { "header": { - "equals": { - "http_status": "200" + "contains": { + "http_status": "200", + "Content-Type": "application/json" } }, "body": { @@ -39,7 +40,7 @@ "expect": { "header": { "contains": { - "http_status": "201" + "http_status": "200" } } }