extend for form type requests
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
2025-07-11 13:46:15 +10:00
parent d658a46a3f
commit 7905823731
4 changed files with 114 additions and 2 deletions

View File

@@ -7,8 +7,10 @@ import (
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"strings"
@@ -96,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)
}
@@ -115,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)
}
}
@@ -126,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)
}