extend for form type requests
This commit is contained in:
@@ -184,6 +184,35 @@ func ReadInput(data []byte, testDefinitions *TestDefinitions) {
|
|||||||
// Create a normal string map for all the body key-value pairs
|
// Create a normal string map for all the body key-value pairs
|
||||||
thisTestCase.Header = OrderedToStringMap(val.(orderedmap.OrderedMap))
|
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
|
// Expect - this is more tricky since it is yet another json fragment
|
||||||
if val, ok := thisTestCaseMap.Get("expect"); ok {
|
if val, ok := thisTestCaseMap.Get("expect"); ok {
|
||||||
@@ -205,6 +234,7 @@ func ReadInput(data []byte, testDefinitions *TestDefinitions) {
|
|||||||
thisTestCase.Expect = *expectOptions
|
thisTestCase.Expect = *expectOptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
testDefinitions.TestCases = append(testDefinitions.TestCases, *thisTestCase)
|
testDefinitions.TestCases = append(testDefinitions.TestCases, *thisTestCase)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
31
main.go
31
main.go
@@ -1,12 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/iancoleman/orderedmap"
|
"github.com/iancoleman/orderedmap"
|
||||||
)
|
)
|
||||||
@@ -154,3 +157,31 @@ func OrderedToStringSlice(input []interface{}) []string {
|
|||||||
|
|
||||||
return result
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
54
run_tests.go
54
run_tests.go
@@ -7,8 +7,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -96,8 +98,56 @@ func RunTest(testCase *TestCase) error {
|
|||||||
errMessage := fmt.Sprintf("error submitting request : '%s'\n", err)
|
errMessage := fmt.Sprintf("error submitting request : '%s'\n", err)
|
||||||
return errors.New(errMessage)
|
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 {
|
} 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)
|
req, err = http.NewRequest(requestType, requestUrl, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +165,7 @@ func RunTest(testCase *TestCase) error {
|
|||||||
key := HeaderReplaceCaptures(k)
|
key := HeaderReplaceCaptures(k)
|
||||||
val := HeaderReplaceCaptures(v)
|
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)
|
req.Header.Add(key, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -126,7 +176,7 @@ func RunTest(testCase *TestCase) error {
|
|||||||
key := HeaderReplaceCaptures(k)
|
key := HeaderReplaceCaptures(k)
|
||||||
val := HeaderReplaceCaptures(v)
|
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)
|
req.Header.Add(key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,6 +20,7 @@ type TestCase struct {
|
|||||||
Header map[string]string
|
Header map[string]string
|
||||||
//Body map[string]string
|
//Body map[string]string
|
||||||
Body []byte
|
Body []byte
|
||||||
|
Form []byte
|
||||||
|
|
||||||
// Something to store results in
|
// Something to store results in
|
||||||
ResultStatusCode int
|
ResultStatusCode int
|
||||||
|
Reference in New Issue
Block a user