-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (76 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
type configJSON struct {
APIInfo apiInfo `json:"api_info"`
}
type apiInfo struct {
URL string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
RequestBody map[string]interface{} `json:"request_body"`
SampleResponse map[string]interface{} `json:"sample_response"`
}
func main() {
if len(os.Args) < 2 {
log.Fatal("ERROR: Please provide file path.")
}
filePath := os.Args[1]
content, err := os.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
cfg := configJSON{}
if err := json.Unmarshal(content, &cfg); err != nil {
log.Fatal(err)
}
cfgRespByte, _ := json.Marshal(cfg.APIInfo.SampleResponse)
fmt.Println("Config File Content: ")
fmt.Printf("URL: %s\nMethod: %s\nHeaders: %v\nSample Response: %s\n",
cfg.APIInfo.URL, cfg.APIInfo.Method, cfg.APIInfo.Headers, string(cfgRespByte))
var body io.Reader
if len(cfg.APIInfo.RequestBody) > 0 {
reqBodyBytes, _ := json.Marshal(cfg.APIInfo.RequestBody)
body = bytes.NewBuffer(reqBodyBytes)
}
req, err := http.NewRequest(cfg.APIInfo.Method, cfg.APIInfo.URL, body)
if err != nil {
log.Fatal(err)
}
for key, value := range cfg.APIInfo.Headers {
req.Header.Add(key, value)
}
client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
// TO-DO: Making this portion dynamic
fmt.Println("Status Code:", resp.StatusCode)
if resp.StatusCode != http.StatusOK {
bdy, _ := ioutil.ReadAll(resp.Body)
log.Fatal(string(bdy))
}
respSample := map[string]interface{}{}
if err := json.NewDecoder(resp.Body).Decode(&respSample); err != nil {
log.Fatal(err)
}
respByte, _ := json.Marshal(respSample)
fmt.Println("Actual Response:", string(respByte))
if !bytes.Equal(cfgRespByte, respByte) {
fmt.Println("Test Failed!")
} else {
fmt.Println("Test Passed!")
}
}