From ebcdbedaccab8a0ff100722336e75f9e06249c02 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Tue, 2 Jan 2024 09:21:27 +1100 Subject: [PATCH] more progress on parsing json input --- main.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index e745172..c1b9657 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,14 @@ package main import ( + "bytes" "encoding/json" "flag" "fmt" + "io" + "net/http" "os" + "strconv" "github.com/iancoleman/orderedmap" ) @@ -23,6 +27,8 @@ type TestCase struct { Method string `json:"method"` Description string `json:"description"` Expect ExpectOptions `json:"expect"` + Header map[string]string + Body map[string]string } type ExpectOptions struct { @@ -74,7 +80,34 @@ func fileExists(filename string) bool { } func PerformGet() { + // Sample code from https://golangnote.com/request/sending-post-request-in-golang-with-header + url := "https://www.example.com/api/v1/create" + contentType := "application/json" + data := []byte(`{"name": "Test User", "email": "test@example.com"}`) + client := &http.Client{} + req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) + if err != nil { + fmt.Println(err) + return + } + req.Header.Add("Content-Type", contentType) + req.Header.Add("Authorization", "Bearer YOUR_ACCESS_TOKEN") + + resp, err := client.Do(req) + if err != nil { + fmt.Println(err) + return + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println(err) + return + } + + fmt.Println(string(body)) } func PerformPost() { @@ -183,11 +216,11 @@ func main() { fmt.Printf("Test %d : %s\n", i, outerKey) + // Get the name of the test case thisTestCase := new(TestCase) thisTestCase.Name = outerKey if testCase, ok := testCasesMap.Get(outerKey); ok { - // Get the details of the test case thisTestCaseMap := testCase.(orderedmap.OrderedMap) @@ -203,6 +236,28 @@ func main() { if val, ok := thisTestCaseMap.Get("description"); ok { thisTestCase.Description = val.(string) } + // Body + if val, ok := thisTestCaseMap.Get("body"); ok { + // Create a normal string map for all the body key-value pairs + thisTestCase.Body = OrderedToStringMap(val.(orderedmap.OrderedMap)) + + /* + thisTestCase.Body = make(map[string]string) + bodyMap := val.(orderedmap.OrderedMap) + bodyMapKeys := bodyMap.Keys() + for _, bodyKey := range bodyMapKeys { + if bodyVal, ok := bodyMap.Get(bodyKey); ok { + switch bodyVal.(type) { + case string: + thisTestCase.Body[bodyKey] = bodyVal.(string) + case bool: + thisTestCase.Body[bodyKey] = strconv.FormatBool(bodyVal.(bool)) + } + + } + } + */ + } // Expect - this is more tricky since it is yet another json fragment /* @@ -220,3 +275,22 @@ func main() { // For debugging, just dump the output fmt.Printf("%+v\n", testDefinitions) } + +func OrderedToStringMap(input orderedmap.OrderedMap) map[string]string { + result := make(map[string]string) + + mapKeys := input.Keys() + for _, bodyKey := range mapKeys { + if bodyVal, ok := input.Get(bodyKey); ok { + switch bodyVal.(type) { + case string: + result[bodyKey] = bodyVal.(string) + case bool: + result[bodyKey] = strconv.FormatBool(bodyVal.(bool)) + } + + } + } + + return result +}