4 Commits

Author SHA1 Message Date
8edbc5c2ed update
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2025-03-24 17:44:10 +11:00
73ef3ae662 update golang version
All checks were successful
continuous-integration/drone/push Build is passing
2024-08-18 11:49:42 +10:00
f5e67dcf99 add test case
All checks were successful
continuous-integration/drone/push Build is passing
2024-05-08 08:50:12 +10:00
617aa45c3d improve capture support in body
All checks were successful
continuous-integration/drone/push Build is passing
2024-04-03 12:27:35 +11:00
8 changed files with 112 additions and 97 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

3
.gitignore vendored
View File

@@ -1 +1,2 @@
tests/*.json
tests/*.json
apitester

View File

@@ -0,0 +1,2 @@
Execute a test:
`go run *.go -input tests/permissions-test.json`

BIN
apitester

Binary file not shown.

View File

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

14
go.mod
View File

@@ -1,17 +1,5 @@
module apitester
go 1.21.0
go 1.23.1
require github.com/iancoleman/orderedmap v0.3.0
require (
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect
github.com/go-ldap/ldap/v3 v3.4.6 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)

69
go.sum
View File

@@ -1,71 +1,2 @@
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 h1:mFRzDkZVAjdal+s7s0MwaRv9igoPqLRdzOLzw/8Xvq8=
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/go-asn1-ber/asn1-ber v1.5.5 h1:MNHlNMBDgEKD4TcKr36vQN68BA00aDfjIt3/bD50WnA=
github.com/go-asn1-ber/asn1-ber v1.5.5/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
github.com/go-ldap/ldap/v3 v3.4.6 h1:ert95MdbiG7aWo/oPYp9btL3KJlMPKnP58r09rI8T+A=
github.com/go-ldap/ldap/v3 v3.4.6/go.mod h1:IGMQANNtxpsOzj7uUAMjpGBaOVTC4DYyIy8VsTdxmtc=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

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