Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
7905823731 | |||
d658a46a3f | |||
6e17210e00 | |||
3e715f5390 | |||
8edbc5c2ed | |||
73ef3ae662 | |||
f5e67dcf99 | |||
617aa45c3d |
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1 +1,7 @@
|
||||
tests/*.json
|
||||
tests/*.json
|
||||
apitester
|
||||
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
@@ -0,0 +1,2 @@
|
||||
Execute a test:
|
||||
`go run *.go -input tests/permissions-test.json`
|
@@ -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
14
go.mod
@@ -1,17 +1,5 @@
|
||||
module apitester
|
||||
|
||||
go 1.21.0
|
||||
go 1.24.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
69
go.sum
@@ -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=
|
||||
|
@@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"reflect"
|
||||
|
||||
"github.com/iancoleman/orderedmap"
|
||||
@@ -182,6 +184,35 @@ func ReadInput(data []byte, testDefinitions *TestDefinitions) {
|
||||
// Create a normal string map for all the body key-value pairs
|
||||
thisTestCase.Header = OrderedToStringMap(val.(orderedmap.OrderedMap))
|
||||
}
|
||||
// Form
|
||||
if val, ok := thisTestCaseMap.Get("form"); ok {
|
||||
// Turn the original string into json
|
||||
bytes, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
error := fmt.Sprintf("Error mnarshalling request form 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 form for test case : '%s'\n", err)
|
||||
panic(error)
|
||||
}
|
||||
|
||||
// Search for any keys in the body that we need to replace with a dynamic random value
|
||||
results := findRandom(nil, jsonData)
|
||||
|
||||
// store the results back into the body
|
||||
newBytes, err := json.Marshal(results)
|
||||
if err != nil {
|
||||
error := fmt.Sprintf("Error turning form interface back into json for test case : '%s'\n", err)
|
||||
panic(error)
|
||||
}
|
||||
thisTestCase.Form = newBytes
|
||||
//fmt.Printf("Form marshalled:\n%v\n", string(newBytes))
|
||||
}
|
||||
|
||||
// Expect - this is more tricky since it is yet another json fragment
|
||||
if val, ok := thisTestCaseMap.Get("expect"); ok {
|
||||
@@ -203,6 +234,7 @@ func ReadInput(data []byte, testDefinitions *TestDefinitions) {
|
||||
thisTestCase.Expect = *expectOptions
|
||||
}
|
||||
}
|
||||
|
||||
testDefinitions.TestCases = append(testDefinitions.TestCases, *thisTestCase)
|
||||
}
|
||||
}
|
||||
@@ -223,6 +255,21 @@ func findRandom(key interface{}, data interface{}) interface{} {
|
||||
newInt := rand.Int()
|
||||
fmt.Printf("Generated random number '%d' to insert into JSON body\n", newInt)
|
||||
return newInt
|
||||
} else if isMac, isMacOk := randomObj["isMac"].(bool); isMacOk && isMac {
|
||||
newMac := generateRandomMAC()
|
||||
fmt.Printf("Generated random mac address '%s' to insert into JSON body for key '%s'\n", newMac, key.(string))
|
||||
return newMac
|
||||
} else if isIP, isIpOk := randomObj["isIP"].(bool); isIpOk && isIP {
|
||||
if network, networkOk := randomObj["network"].(string); networkOk {
|
||||
newIp, err := generateRandomIP(network)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to generate random IP address: '%s'\n", err)
|
||||
return ""
|
||||
} else {
|
||||
fmt.Printf("Generated random IP '%s' address in network '%s' to insert into JSON body for key '%s'\n", newIp, network, key.(string))
|
||||
return newIp
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -248,6 +295,51 @@ func generateRandomString(length int) string {
|
||||
return string(result)
|
||||
}
|
||||
|
||||
func generateRandomMAC() string {
|
||||
mac := make([]byte, 6)
|
||||
for i := range mac {
|
||||
mac[i] = byte(rand.Intn(256))
|
||||
}
|
||||
|
||||
// Set the locally administered and unicast bits
|
||||
mac[0] = (mac[0] | 2) & 0xfe
|
||||
|
||||
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])
|
||||
}
|
||||
|
||||
// generateRandomIP generates a random IP address within the given subnet in CIDR notation.
|
||||
func generateRandomIP(cidr string) (string, error) {
|
||||
ip, ipnet, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid CIDR: %w", err)
|
||||
}
|
||||
|
||||
// Convert IP to uint32
|
||||
ip4 := ip.To4()
|
||||
if ip4 == nil {
|
||||
return "", fmt.Errorf("not an IPv4 subnet")
|
||||
}
|
||||
ipInt := binary.BigEndian.Uint32(ip4)
|
||||
mask := binary.BigEndian.Uint32(ipnet.Mask)
|
||||
|
||||
// Calculate network and broadcast addresses
|
||||
network := ipInt & mask
|
||||
broadcast := network | ^mask
|
||||
|
||||
if broadcast-network <= 1 {
|
||||
return "", fmt.Errorf("subnet too small to allocate address")
|
||||
}
|
||||
|
||||
// Generate a random IP between network+1 and broadcast-1 (excluding network and broadcast)
|
||||
randomIP := network + uint32(rand.Intn(int(broadcast-network-1))) + 1
|
||||
|
||||
// Convert back to net.IP
|
||||
ipBytes := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(ipBytes, randomIP)
|
||||
return net.IP(ipBytes).String(), nil
|
||||
}
|
||||
|
||||
func ReadHeaderTestCases(input orderedmap.OrderedMap, result *HeaderTests) {
|
||||
result.Contains = make(map[string]string)
|
||||
result.Equals = make(map[string]string)
|
||||
|
31
main.go
31
main.go
@@ -1,12 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/iancoleman/orderedmap"
|
||||
)
|
||||
@@ -154,3 +157,31 @@ func OrderedToStringSlice(input []interface{}) []string {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// prettyPrint comes from https://gist.github.com/sfate/9d45f6c5405dc4c9bf63bf95fe6d1a7c
|
||||
func prettyPrint(args ...interface{}) {
|
||||
var caller string
|
||||
|
||||
timeNow := time.Now().Format("01-02-2006 15:04:05")
|
||||
prefix := fmt.Sprintf("[%s] %s -- ", "PrettyPrint", timeNow)
|
||||
_, fileName, fileLine, ok := runtime.Caller(1)
|
||||
|
||||
if ok {
|
||||
caller = fmt.Sprintf("%s:%d", fileName, fileLine)
|
||||
} else {
|
||||
caller = ""
|
||||
}
|
||||
|
||||
fmt.Printf("\n%s%s\n", prefix, caller)
|
||||
|
||||
if len(args) == 2 {
|
||||
label := args[0]
|
||||
value := args[1]
|
||||
|
||||
s, _ := json.MarshalIndent(value, "", "\t")
|
||||
fmt.Printf("%s%s: %s\n", prefix, label, string(s))
|
||||
} else {
|
||||
s, _ := json.MarshalIndent(args, "", "\t")
|
||||
fmt.Printf("%s%s\n", prefix, string(s))
|
||||
}
|
||||
}
|
||||
|
198
run_tests.go
198
run_tests.go
@@ -7,8 +7,12 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -29,11 +33,22 @@ func RunTest(testCase *TestCase) error {
|
||||
|
||||
// Determine URL
|
||||
if len(testDefinitions.BaseUrl) > 0 {
|
||||
//fmt.Printf("Joining '%s' with '%s'\n", testDefinitions.BaseUrl, testCase.Path)
|
||||
|
||||
requestUrl, err = url.JoinPath(testDefinitions.BaseUrl, testCase.Path)
|
||||
if err != nil {
|
||||
errMessage := fmt.Sprintf("error combining request URL : '%s'\n", err)
|
||||
return errors.New(errMessage)
|
||||
}
|
||||
|
||||
decoded, err := url.QueryUnescape(requestUrl)
|
||||
if err != nil {
|
||||
errMessage := fmt.Sprintf("error unescaping request URL : '%s'\n", err)
|
||||
return errors.New(errMessage)
|
||||
}
|
||||
requestUrl = decoded
|
||||
//fmt.Printf("url path decoded '%s'\n", decoded)
|
||||
|
||||
} else {
|
||||
requestUrl = testCase.Path
|
||||
}
|
||||
@@ -54,9 +69,85 @@ 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 if len(testCase.Form) > 0 {
|
||||
fmt.Printf("Sending a form request\n")
|
||||
//prettyPrint(testCase)
|
||||
|
||||
// Create buffer and multipart writer
|
||||
var requestBody bytes.Buffer
|
||||
writer := multipart.NewWriter(&requestBody)
|
||||
|
||||
// unmarshal testCase.Form so we can search for replacements
|
||||
var jsonBody map[string]interface{}
|
||||
err = json.Unmarshal(testCase.Form, &jsonBody)
|
||||
if err != nil {
|
||||
error := fmt.Sprintf("Error processing request form for test case : '%s'\n", err)
|
||||
panic(error)
|
||||
}
|
||||
|
||||
// TODO replacement searching
|
||||
|
||||
for k, v := range jsonBody {
|
||||
if k == "file" {
|
||||
// Open file
|
||||
file, err := os.Open(v.(string))
|
||||
if err != nil {
|
||||
return fmt.Errorf("open file error: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Add file
|
||||
part, err := writer.CreateFormFile("file", file.Name())
|
||||
if err != nil {
|
||||
return fmt.Errorf("create form file error: %w", err)
|
||||
}
|
||||
if _, err := io.Copy(part, file); err != nil {
|
||||
return fmt.Errorf("copy file error: %w", err)
|
||||
}
|
||||
} else {
|
||||
_ = writer.WriteField(k, v.(string))
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize multipart writer
|
||||
if err := writer.Close(); err != nil {
|
||||
return fmt.Errorf("close writer error: %w", err)
|
||||
}
|
||||
|
||||
req, err = http.NewRequest(requestType, requestUrl, &requestBody)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
} else {
|
||||
fmt.Printf("Sending neither a body nor a form request\n")
|
||||
//prettyPrint(testCase)
|
||||
req, err = http.NewRequest(requestType, requestUrl, nil)
|
||||
}
|
||||
|
||||
@@ -74,7 +165,7 @@ func RunTest(testCase *TestCase) error {
|
||||
key := HeaderReplaceCaptures(k)
|
||||
val := HeaderReplaceCaptures(v)
|
||||
|
||||
//fmt.Printf("Add global header %s = %s\n", key, val)
|
||||
fmt.Printf("Add global header %s = %s\n", key, val)
|
||||
req.Header.Add(key, val)
|
||||
}
|
||||
}
|
||||
@@ -85,7 +176,7 @@ func RunTest(testCase *TestCase) error {
|
||||
key := HeaderReplaceCaptures(k)
|
||||
val := HeaderReplaceCaptures(v)
|
||||
|
||||
//fmt.Printf("Add header %s = %s\n", key, val)
|
||||
fmt.Printf("Add header %s = %s\n", key, val)
|
||||
req.Header.Add(key, val)
|
||||
}
|
||||
|
||||
@@ -120,6 +211,7 @@ func RunTest(testCase *TestCase) error {
|
||||
json.Unmarshal(body, &b)
|
||||
if b != nil {
|
||||
testCase.ResultBodyMap = b.(map[string]interface{})
|
||||
//fmt.Printf("Body map: '%v'\n", testCase.ResultBodyMap)
|
||||
//bodyMap = b.(map[string]interface{})
|
||||
}
|
||||
}
|
||||
@@ -132,13 +224,48 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
for k, v := range captureData.Header.Data {
|
||||
fmt.Printf("Searching header response for match on capture string '%s'\n", k)
|
||||
|
||||
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 +293,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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@ type TestCase struct {
|
||||
Header map[string]string
|
||||
//Body map[string]string
|
||||
Body []byte
|
||||
Form []byte
|
||||
|
||||
// Something to store results in
|
||||
ResultStatusCode int
|
||||
|
Reference in New Issue
Block a user