Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
7905823731 | |||
d658a46a3f | |||
6e17210e00 | |||
3e715f5390 |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,2 +1,7 @@
|
||||
tests/*.json
|
||||
apitester
|
||||
apitester
|
||||
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
2
go.mod
2
go.mod
@@ -1,5 +1,5 @@
|
||||
module apitester
|
||||
|
||||
go 1.23.1
|
||||
go 1.24.1
|
||||
|
||||
require github.com/iancoleman/orderedmap v0.3.0
|
||||
|
@@ -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))
|
||||
}
|
||||
}
|
||||
|
89
run_tests.go
89
run_tests.go
@@ -7,8 +7,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -31,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
|
||||
}
|
||||
@@ -85,8 +98,56 @@ func RunTest(testCase *TestCase) error {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -104,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)
|
||||
}
|
||||
}
|
||||
@@ -115,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)
|
||||
}
|
||||
|
||||
@@ -150,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{})
|
||||
}
|
||||
}
|
||||
@@ -184,6 +246,29 @@ func RunTest(testCase *TestCase) error {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// Capture anything needed
|
||||
break
|
||||
}
|
||||
|
@@ -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