capture feature starting to work
This commit is contained in:
59
main.go
59
main.go
@@ -12,6 +12,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/iancoleman/orderedmap"
|
||||
)
|
||||
@@ -143,8 +144,7 @@ func RunTest(testCase *TestCase) error {
|
||||
var requestUrl string
|
||||
var requestType string
|
||||
var req *http.Request
|
||||
//var requestHeaders map[string]string
|
||||
//var requestBody map[string]string
|
||||
var bodyMap map[string]interface{}
|
||||
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: testDefinitions.Insecure},
|
||||
@@ -193,21 +193,23 @@ func RunTest(testCase *TestCase) error {
|
||||
if len(testDefinitions.Headers) > 0 {
|
||||
fmt.Printf("Adding global headers to request\n")
|
||||
for k, v := range testDefinitions.Headers {
|
||||
fmt.Printf("Add global header %s = %s\n", k, v)
|
||||
// Check for capture tokens that need to be replaced before adding the value
|
||||
key := HeaderReplaceCaptures(k)
|
||||
val := HeaderReplaceCaptures(v)
|
||||
|
||||
// TODO : Check for capture tokens that need to be replaced before adding the value
|
||||
|
||||
req.Header.Add(k, v)
|
||||
fmt.Printf("Add global header %s = %s\n", key, val)
|
||||
req.Header.Add(key, val)
|
||||
}
|
||||
}
|
||||
|
||||
// Then any test case specific headers
|
||||
for k, v := range testCase.Header {
|
||||
fmt.Printf("Add Header %s = %s\n", k, v)
|
||||
// Check for capture tokens that need to be replaced before adding the value
|
||||
key := HeaderReplaceCaptures(k)
|
||||
val := HeaderReplaceCaptures(v)
|
||||
|
||||
// TODO : Check for capture tokens that need to be replaced before adding the value
|
||||
|
||||
req.Header.Add(k, v)
|
||||
fmt.Printf("Add header %s = %s\n", key, val)
|
||||
req.Header.Add(key, val)
|
||||
}
|
||||
|
||||
// Perform request
|
||||
@@ -230,11 +232,34 @@ func RunTest(testCase *TestCase) error {
|
||||
|
||||
fmt.Printf("Body response:\n%s\n", string(body))
|
||||
|
||||
// We have no idea what the response will look like so use an interface and unmarshal manually as needed
|
||||
if len(body) > 0 {
|
||||
var b interface{}
|
||||
json.Unmarshal(body, &b)
|
||||
if b != nil {
|
||||
bodyMap = b.(map[string]interface{})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 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")
|
||||
|
||||
captureData := testDefinitions.CaptureCases[i].CaptureData
|
||||
|
||||
for k, v := range captureData.Body.Data {
|
||||
fmt.Printf("Body capture %s = %s\n", k, v)
|
||||
|
||||
if val, ok := bodyMap[k]; ok {
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
// Capture anything needed
|
||||
break
|
||||
}
|
||||
@@ -244,6 +269,19 @@ func RunTest(testCase *TestCase) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// HeaderReplaceCaptures iterates through captureValues to apply any text replacements necessary for input string
|
||||
func HeaderReplaceCaptures(input string) string {
|
||||
|
||||
for k, v := range captureValues.Data {
|
||||
if strings.Contains(input, k) {
|
||||
fmt.Printf("Found key '%s' in input string '%s', replacing with '%s'\n", k, input, v)
|
||||
input = strings.Replace(input, k, v, -1)
|
||||
}
|
||||
}
|
||||
|
||||
return input
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s []byte
|
||||
var err error
|
||||
@@ -252,6 +290,7 @@ func main() {
|
||||
// Prepare struct to store settings common to all tests
|
||||
testDefinitions = new(TestDefinitions)
|
||||
captureValues = new(CaptureValues)
|
||||
captureValues.Data = make(map[string]string)
|
||||
|
||||
// Command line arguments
|
||||
var inputJson string
|
||||
|
Reference in New Issue
Block a user