diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..13063b5 Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md index e69de29..a661c71 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,2 @@ +Execute a test: +`go run *.go -input tests/permissions-test.json` \ No newline at end of file diff --git a/check_results.go b/check_results.go index 8d54c5a..5fcf431 100644 --- a/check_results.go +++ b/check_results.go @@ -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 } diff --git a/run_tests.go b/run_tests.go index b40a238..d0300e7 100644 --- a/run_tests.go +++ b/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 + } + } +}