improve capture support in body
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
Execute a test:
|
||||
`go run *.go -input tests/permissions-test.json`
|
@@ -90,13 +90,13 @@ 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("%+v\n", testCase.ResultBodyMap)
|
||||
//fmt.Printf("Body contains check '%s'='%s'\n", k, v)
|
||||
//fmt.Printf("%+v\n", testCase.ResultBodyMap)
|
||||
|
||||
results := findKey(k, testCase.ResultBodyMap)
|
||||
|
||||
if len(results) > 0 {
|
||||
fmt.Printf("Found key '%s': %v\n", k, results)
|
||||
fmt.Printf("Test Body.Contains found key '%s'\n", k)
|
||||
|
||||
checkFound := false
|
||||
// search through all the results to see if the expected value is there
|
||||
@@ -114,7 +114,7 @@ func BodyContains(testCase *TestCase) (bool, error) {
|
||||
}
|
||||
|
||||
} else {
|
||||
fmt.Printf("Key '%s' not found\n", k)
|
||||
fmt.Printf("Test Body.Contains failed, expected key '%s' not found\n", k)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,9 +127,9 @@ func BodyHasKeys(testCase *TestCase) (bool, error) {
|
||||
results := findKey(key, testCase.ResultBodyMap)
|
||||
|
||||
if len(results) > 0 {
|
||||
fmt.Printf("Found key '%s' with values %v\n", key, results)
|
||||
fmt.Printf("Test Body.HasKeys found expected key '%s'\n", key)
|
||||
} else {
|
||||
fmt.Printf("Body HasKeys test failed, expected key '%s' not found\n", key)
|
||||
fmt.Printf("Test Body.HasKeys failed, expected key '%s' not found\n", key)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
|
109
run_tests.go
109
run_tests.go
@@ -9,6 +9,8 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -54,8 +56,36 @@ func RunTest(testCase *TestCase) error {
|
||||
|
||||
// Create request
|
||||
if len(testCase.Body) > 0 {
|
||||
fmt.Printf("Adding body to request : %s\n", testCase.Body)
|
||||
req, err = http.NewRequest(requestType, requestUrl, bytes.NewBuffer(testCase.Body))
|
||||
|
||||
// unmarshal testCase.Body so we can search for replacements
|
||||
var jsonBody map[string]interface{}
|
||||
err = json.Unmarshal(testCase.Body, &jsonBody)
|
||||
if err != nil {
|
||||
error := fmt.Sprintf("Error processing request body for test case : '%s'\n", err)
|
||||
panic(error)
|
||||
}
|
||||
|
||||
// iterate through the key-value pairs and check for replacements
|
||||
//fmt.Printf("Capture data : '%+v'\n", captureValues.Data)
|
||||
BodyReplaceCapture(jsonBody, captureValues.Data)
|
||||
|
||||
// turn the unmarshalled object back into a []byte so that we can send it
|
||||
modifiedJsonData, err := json.Marshal(jsonBody)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Check for capture tokens that need to be replaced before adding the value
|
||||
//newBody := ReplaceCaptures(string(testCase.Body[:]))
|
||||
|
||||
fmt.Printf("Adding body to request : %s\n", modifiedJsonData)
|
||||
req, err = http.NewRequest(requestType, requestUrl, bytes.NewBuffer(modifiedJsonData))
|
||||
|
||||
if err != nil {
|
||||
errMessage := fmt.Sprintf("error submitting request : '%s'\n", err)
|
||||
return errors.New(errMessage)
|
||||
}
|
||||
|
||||
} else {
|
||||
req, err = http.NewRequest(requestType, requestUrl, nil)
|
||||
}
|
||||
@@ -132,13 +162,25 @@ func RunTest(testCase *TestCase) error {
|
||||
captureData := testDefinitions.CaptureCases[i].CaptureData
|
||||
|
||||
for k, v := range captureData.Body.Data {
|
||||
//fmt.Printf("Body capture %s = %s\n", k, v)
|
||||
fmt.Printf("Searching body response for match on capture string '%s'\n", k)
|
||||
|
||||
if val, ok := testCase.ResultBodyMap[k]; ok {
|
||||
fmt.Printf("Found capture %s\n", k)
|
||||
// TODO handle values other than string using a switch
|
||||
//fmt.Printf("Found matching capture in body with value : '%v', storing replacement with key '%s'\n", val, v)
|
||||
captureValues.Data[v] = val.(string)
|
||||
results := findKey(k, testCase.ResultBodyMap)
|
||||
//fmt.Printf("Results : '%v'\n", results)
|
||||
|
||||
if len(results) > 0 {
|
||||
//fmt.Printf("Found %d results but only storing the first one\n", len(results))
|
||||
|
||||
// Get the type of the first element
|
||||
valueType := reflect.TypeOf(results[0])
|
||||
|
||||
// Check if the type is not string
|
||||
if valueType.Kind() != reflect.String {
|
||||
// Convert the value to string
|
||||
results[0] = fmt.Sprintf("%v", results[0])
|
||||
}
|
||||
|
||||
fmt.Printf("Storing capture '%s' = '%s'\n", k, results[0].(string))
|
||||
captureValues.Data[v] = results[0].(string)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,3 +208,54 @@ func HeaderReplaceCaptures(input string) string {
|
||||
|
||||
return input
|
||||
}
|
||||
|
||||
// Search unmarshalled json data for any replacements to make from data already captured by previous tests
|
||||
func BodyReplaceCapture(data interface{}, captureValues map[string]string) {
|
||||
switch v := data.(type) {
|
||||
case map[string]interface{}:
|
||||
for key, val := range v {
|
||||
//fmt.Printf("NewReplaceCapture checking '%v' = '%v'\n", key, val)
|
||||
|
||||
// Check if key needs to be replaced
|
||||
if replaceKey, ok := captureValues[key]; ok {
|
||||
//fmt.Printf("Replacing key '%s'\n", key)
|
||||
delete(v, key)
|
||||
v[replaceKey] = val
|
||||
key = replaceKey
|
||||
}
|
||||
|
||||
// Check if value needs to be replaced
|
||||
if strVal, ok := val.(string); ok {
|
||||
//fmt.Printf("Checking value '%s'\n", strVal)
|
||||
if replaceVal, ok := captureValues[strVal]; ok {
|
||||
|
||||
if strings.HasPrefix(strVal, "int:") {
|
||||
fmt.Printf("Replacing capture token '%s' with int version of '%s'\n", strVal, replaceVal)
|
||||
i, err := strconv.Atoi(replaceVal)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
v[key] = i
|
||||
} else {
|
||||
fmt.Printf("Replacing capture token '%s' with '%s'\n", strVal, replaceVal)
|
||||
v[key] = replaceVal
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if replaceVal, ok := captureValues[key]; ok {
|
||||
//fmt.Printf("Replacing value '%s'\n", captureValues[key])
|
||||
v[key] = replaceVal
|
||||
}
|
||||
|
||||
BodyReplaceCapture(val, captureValues)
|
||||
}
|
||||
case []interface{}:
|
||||
for i, val := range v {
|
||||
fmt.Printf("i val : '%v' = '%v'\n", i, val)
|
||||
BodyReplaceCapture(val, captureValues)
|
||||
v[i] = val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user