implement feature to generate random test data
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:
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
|
||||
"github.com/iancoleman/orderedmap"
|
||||
@@ -145,20 +146,36 @@ func ReadInput(data []byte, testDefinitions *TestDefinitions) {
|
||||
}
|
||||
// Body
|
||||
if val, ok := thisTestCaseMap.Get("body"); ok {
|
||||
// Create a normal string map for all the body key-value pairs
|
||||
|
||||
// TODO : Support nested json instead of single level flat request
|
||||
|
||||
//thisTestCase.Body = OrderedToStringMap(val.(orderedmap.OrderedMap))
|
||||
|
||||
// Turn the original string into json
|
||||
bytes, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
error := fmt.Sprintf("Error mnarshalling request body for test case : '%s'\n", err)
|
||||
panic(error)
|
||||
}
|
||||
|
||||
// Now that we have json, convert it to an interface that we can play with
|
||||
var jsonData interface{}
|
||||
err = json.Unmarshal(bytes, &jsonData)
|
||||
if err != nil {
|
||||
error := fmt.Sprintf("Error processing request body for test case : '%s'\n", err)
|
||||
panic(error)
|
||||
} else {
|
||||
thisTestCase.Body = bytes
|
||||
//fmt.Printf("Body marshalled:\n%v\n", string(bytes))
|
||||
}
|
||||
|
||||
// Search for any keys in the body that we need to replace with a dynamic random value
|
||||
results := findRandom(nil, jsonData)
|
||||
|
||||
// for debugging
|
||||
//resultJSON, _ := json.MarshalIndent(results, "", " ")
|
||||
//fmt.Println(string(resultJSON))
|
||||
|
||||
// store the results back into the body
|
||||
newBytes, err := json.Marshal(results)
|
||||
if err != nil {
|
||||
error := fmt.Sprintf("Error turning body interface back into json for test case : '%s'\n", err)
|
||||
panic(error)
|
||||
}
|
||||
thisTestCase.Body = newBytes
|
||||
//fmt.Printf("Body marshalled:\n%v\n", string(newBytes))
|
||||
}
|
||||
// Header
|
||||
if val, ok := thisTestCaseMap.Get("header"); ok {
|
||||
@@ -190,6 +207,47 @@ func ReadInput(data []byte, testDefinitions *TestDefinitions) {
|
||||
}
|
||||
}
|
||||
|
||||
// Use this function to find and generate any random values we want to use during testing
|
||||
func findRandom(key interface{}, data interface{}) interface{} {
|
||||
switch v := data.(type) {
|
||||
case map[string]interface{}:
|
||||
if randomValue, ok := v["random"]; ok {
|
||||
if randomObj, ok := randomValue.(map[string]interface{}); ok {
|
||||
if isString, isStringOk := randomObj["isString"].(bool); isStringOk && isString {
|
||||
if length, lengthOk := randomObj["length"].(float64); lengthOk {
|
||||
newString := generateRandomString(int(length))
|
||||
fmt.Printf("Generated random string '%s' to insert into JSON body for key '%s'\n", newString, key.(string))
|
||||
return newString
|
||||
}
|
||||
} else if isInt, isIntOk := randomObj["isInt"].(bool); isIntOk && isInt {
|
||||
newInt := rand.Int()
|
||||
fmt.Printf("Generated random number '%d' to insert into JSON body\n", newInt)
|
||||
return newInt
|
||||
}
|
||||
}
|
||||
}
|
||||
for key, value := range v {
|
||||
v[key] = findRandom(key, value)
|
||||
}
|
||||
case []interface{}:
|
||||
for i, value := range v {
|
||||
v[i] = findRandom(i, value)
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func generateRandomString(length int) string {
|
||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
result := make([]byte, length)
|
||||
for i := range result {
|
||||
result[i] = charset[rand.Intn(len(charset))]
|
||||
}
|
||||
|
||||
return string(result)
|
||||
}
|
||||
|
||||
func ReadHeaderTestCases(input orderedmap.OrderedMap, result *HeaderTests) {
|
||||
result.Contains = make(map[string]string)
|
||||
result.Equals = make(map[string]string)
|
||||
|
Reference in New Issue
Block a user