improve capture support in body
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-04-03 12:27:35 +11:00
parent ed0abae016
commit 617aa45c3d
4 changed files with 109 additions and 14 deletions

View File

@@ -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
}
}
}