132 lines
3.1 KiB
Go
132 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func CheckResults(testCase *TestCase) (bool, error) {
|
|
var result bool = true
|
|
|
|
// Check headers
|
|
headerContains, err := HeaderContains(testCase)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
result = result && headerContains
|
|
|
|
headerEquals, err := HeaderEquals(testCase)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
result = result && headerEquals
|
|
|
|
// Check body
|
|
bodyContains, err := BodyContains(testCase)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
result = result && bodyContains
|
|
|
|
bodyHasKeys, err := BodyHasKeys(testCase)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
result = result && bodyHasKeys
|
|
|
|
return result, err
|
|
}
|
|
|
|
func HeaderContains(testCase *TestCase) (bool, error) {
|
|
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
|
|
}
|
|
|
|
func HeaderEquals(testCase *TestCase) (bool, error) {
|
|
|
|
for k, v := range testCase.Expect.Header.Equals {
|
|
fmt.Printf("Header equals check '%s'='%s'\n", k, v)
|
|
|
|
// TODO
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func BodyContains(testCase *TestCase) (bool, error) {
|
|
for k, v := range testCase.Expect.Body.Contains {
|
|
//fmt.Printf("Body contains check '%s'='%s'\n", k, v)
|
|
|
|
// If the body response was json then we stored it already when running the test
|
|
if val, ok := testCase.ResultBodyMap[k]; ok {
|
|
// TODO handle values other than string using a switch
|
|
//fmt.Printf("Found key in body with response '%s'\n", val)
|
|
|
|
// Check that the value matched
|
|
if strings.Contains(val.(string), v) {
|
|
fmt.Printf("Key matches, success\n")
|
|
} else {
|
|
fmt.Printf("Body Contains check failed on key '%s'. Expected value '%s' but found '%s'\n", k, v, val)
|
|
return false, nil
|
|
}
|
|
|
|
} else {
|
|
// body did not contain the key we were looking for so return false
|
|
return false, nil
|
|
}
|
|
|
|
// TODO
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func BodyHasKeys(testCase *TestCase) (bool, error) {
|
|
for _, key := range testCase.Expect.Body.HasKeys {
|
|
// If the body response was json then we stored it already when running the test
|
|
if _, ok := testCase.ResultBodyMap[key]; ok {
|
|
fmt.Printf("Confirmed body has key '%s'\n", key)
|
|
} else {
|
|
fmt.Printf("Body HasKeys test failed, expected key '%s' not found\n", key)
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
return true, nil
|
|
}
|