more progress

This commit is contained in:
2024-01-02 17:46:10 +11:00
parent 2e55e27828
commit 3e371360ec
2 changed files with 70 additions and 12 deletions

78
main.go
View File

@@ -60,6 +60,10 @@ type CaptureBody struct {
Data map[string]string
}
type CaptureValues struct {
Data map[string]string
}
type ExpectOptions struct {
Header HeaderTests `json:"header"`
Body BodyTests `json:"body"`
@@ -80,6 +84,7 @@ type BodyTests struct {
}
var testDefinitions *TestDefinitions
var captureValues *CaptureValues
// fileExists returns true if the specified file exists and is not a directory
func fileExists(filename string) bool {
@@ -189,6 +194,9 @@ func RunTest(testCase *TestCase) error {
fmt.Printf("Adding global headers to request\n")
for k, v := range testDefinitions.Headers {
fmt.Printf("Add global header %s = %s\n", k, v)
// TODO : Check for capture tokens that need to be replaced before adding the value
req.Header.Add(k, v)
}
}
@@ -196,6 +204,9 @@ func RunTest(testCase *TestCase) error {
// Then any test case specific headers
for k, v := range testCase.Header {
fmt.Printf("Add Header %s = %s\n", k, v)
// TODO : Check for capture tokens that need to be replaced before adding the value
req.Header.Add(k, v)
}
@@ -219,6 +230,16 @@ func RunTest(testCase *TestCase) error {
fmt.Printf("Body response:\n%s\n", string(body))
// Iterate through testDefinitions.CaptureCases and see if there are any that match this test
// If we found one, add the appropriate replacement to captureValues
for i := range testDefinitions.CaptureCases {
if testDefinitions.CaptureCases[i].TestCaseName == testCase.Name {
fmt.Printf("Need to capture value from this test case\n")
// Capture anything needed
break
}
}
// No errors if we reached this point
return nil
}
@@ -230,8 +251,7 @@ func main() {
// Prepare struct to store settings common to all tests
testDefinitions = new(TestDefinitions)
//testCaseDefinition := new(InputData)
captureValues = new(CaptureValues)
// Command line arguments
var inputJson string
@@ -250,11 +270,13 @@ func main() {
os.Exit(1)
}
// Process the input json
ReadInput(s, testDefinitions)
// For debugging, just dump the output
fmt.Printf("%+v\n", testDefinitions)
// Perform specified test cases
for i := range testDefinitions.TestCases {
RunTest(&testDefinitions.TestCases[i])
}
@@ -332,16 +354,52 @@ func ReadInput(data []byte, testDefinitions *TestDefinitions) {
return
*/
// TODO : capture
// Process the "capture" settings that allow us to use values from one test case in subsequent test cases
// each capture node has a name that links it to the test case that captures that data
if capturecase, ok := o.Get("capture"); ok {
captureCasesMap := capturecase.(orderedmap.OrderedMap)
captureCasesKeys := captureCasesMap.Keys()
for i, outerKey := range captureCasesKeys {
fmt.Printf("[%d] : Capture from %s\n", i, outerKey)
if capture, ok := o.Get("capture"); ok {
captureMap := capture.(orderedmap.OrderedMap)
captureKeys := captureMap.Keys()
for i, outerKey := range captureKeys {
// Define a new capture case object to record these details
thisCaptureCase := new(CaptureCase)
thisCaptureCase.TestCaseName = outerKey
// Make sure we can access the capture defintiion
if captureCase, ok := captureMap.Get(outerKey); ok {
// Store the test case name that this capture will come from
thisCaptureCase.TestCaseName = outerKey
fmt.Printf("[%d] : Capture from %s\n", i, outerKey)
// Get capture data from body
thisCaptureCaseMap := captureCase.(orderedmap.OrderedMap)
if body, ok := thisCaptureCaseMap.Get("body"); ok {
//fmt.Printf("This capture case has body defined\n")
bodyMap := body.(orderedmap.OrderedMap)
bodyMapKeys := bodyMap.Keys()
bodyData := make(map[string]string)
for _, bodyKey := range bodyMapKeys {
if bodyVal, ok := bodyMap.Get(bodyKey); ok {
switch vType := bodyVal.(type) {
case string:
fmt.Printf("Capturing '%s' from test result and using '%s' for replacement token\n", bodyKey, bodyVal.(string))
// Store capture info
bodyData[bodyKey] = bodyVal.(string)
default:
fmt.Printf("received unexpected value type, %T\n", vType)
}
}
}
captureBody := new(CaptureBody)
captureBody.Data = bodyData
thisCaptureCase.CaptureData.Body = *captureBody
}
// TODO : header
}
testDefinitions.CaptureCases = append(testDefinitions.CaptureCases, *thisCaptureCase)
}

View File

@@ -27,7 +27,7 @@
"method": "GET",
"description": "List secrets",
"header": {
"Authorization": "Bearer %access_token%"
"Authorization": "Bearer %ACCESS_TOKEN%"
},
"body": {
"Hostname":"host999.cdc.home",
@@ -53,7 +53,7 @@
"capture": {
"test1": {
"body": {
"access_token": "access_token"
"access_token": "%ACCESS_TOKEN%"
}
}
}