-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_api.go
More file actions
70 lines (62 loc) · 1.79 KB
/
run_api.go
File metadata and controls
70 lines (62 loc) · 1.79 KB
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
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
func (t *TestCase) ApiTest() (string, error) {
url := "http://" + apiSvc.ApiUrl() + "/" + t.Route + "?" + t.ApiOptions
request, err := http.NewRequest("GET", url, nil) // Default to GET request
if err != nil {
fmt.Println("Error creating request:", err)
return "", err
}
// Set the User-Agent header
request.Header.Set("User-Agent", "testRunner")
// Check if config.file exists
// TODO: NOTE THAT THIS DOES NOT WORK. IT DOES NOT
// TODO: FIND THE RIGHT _curl.txt FILE FOR THIS
// TODO: TEST CASE.
if _, err := os.Stat("config.file"); err == nil {
configData, err := os.ReadFile("config.file")
if err != nil {
fmt.Println("Error reading config file:", err)
return "", err
}
lines := strings.Split(string(configData), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "-X ") {
verb := strings.TrimSpace(strings.TrimPrefix(line, "-X "))
request.Method = verb
}
if strings.HasPrefix(line, "-H ") {
header := strings.TrimSpace(strings.TrimPrefix(line, "-H "))
headerParts := strings.SplitN(header, ": ", 2)
if len(headerParts) == 2 {
request.Header.Set(headerParts[0], headerParts[1])
}
}
if strings.HasPrefix(line, "-d ") {
data := strings.TrimSpace(strings.TrimPrefix(line, "-d "))
request.Body = io.NopCloser(strings.NewReader(data))
request.ContentLength = int64(len(data))
}
}
}
// Create an HTTP client and send the request
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println("Error making request:", err)
return "", err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return "", err
}
return string(body), nil
}