improve body contains check
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-01-03 15:42:25 +11:00
parent af7ee7f3c1
commit b2519287ef
8 changed files with 148 additions and 62 deletions

View File

@@ -56,11 +56,11 @@ func HeaderContains(testCase *TestCase) (bool, error) {
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)
//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)
//fmt.Printf("Found match '%s' = '%s'\n", hVal[i], v)
checkFound = true
}
}
@@ -68,7 +68,7 @@ func HeaderContains(testCase *TestCase) (bool, error) {
}
if !checkFound {
fmt.Printf("Expected to find header key-value of '%s:%s' but no matching value was found\n", k, v)
fmt.Printf("Header Contains Check failed to find expected header key-value of '%s:%s'\n", k, v)
return false, nil
}
}
@@ -90,27 +90,32 @@ func HeaderEquals(testCase *TestCase) (bool, error) {
func BodyContains(testCase *TestCase) (bool, error) {
for k, v := range testCase.Expect.Body.Contains {
//fmt.Printf("Body contains check '%s'='%s'\n", k, v)
fmt.Printf("Body contains check '%s'='%s'\n", k, v)
fmt.Printf("%+v\n", testCase.ResultBodyMap)
// 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)
results := findKey(k, testCase.ResultBodyMap)
// 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)
if len(results) > 0 {
fmt.Printf("Found key '%s': %v\n", k, results)
checkFound := false
// search through all the results to see if the expected value is there
for i := range results {
if strings.Contains(results[i].(string), v) {
fmt.Printf("Expected value of '%s' matches, success\n", v)
checkFound = true
break
}
}
if !checkFound {
fmt.Printf("Body Contains check failed on key '%s', did not find expected value '%s'\n", k, v)
return false, nil
}
} else {
// body did not contain the key we were looking for so return false
return false, nil
fmt.Printf("Key '%s' not found\n", k)
}
// TODO
}
return true, nil
@@ -120,7 +125,7 @@ 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)
//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
@@ -129,3 +134,23 @@ func BodyHasKeys(testCase *TestCase) (bool, error) {
return true, nil
}
// findKey recursively searches map for specified key
func findKey(key string, data interface{}) []interface{} {
var results []interface{}
switch v := data.(type) {
case map[string]interface{}:
if val, ok := v[key]; ok {
results = append(results, val)
}
for _, value := range v {
results = append(results, findKey(key, value)...)
}
case []interface{}:
for _, value := range v {
results = append(results, findKey(key, value)...)
}
}
return results
}