-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
144 lines (114 loc) · 3.53 KB
/
requests.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package servicetest
import (
"bytes"
"encoding/json"
"fmt"
"github.com/PixoVR/pixo-golang-clients/pixo-platform/urlfinder"
"github.com/cucumber/godog"
"github.com/go-resty/resty/v2"
. "github.com/onsi/gomega"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"io"
"net/http"
"strings"
)
func (s *ServerTestFeature) MakeRequest(method string, endpoint string, body *godog.DocString) {
var bodyContent []byte
if body != nil {
bodyContent = []byte(body.Content)
}
bodyContent = s.performSubstitutions(bodyContent)
s.PerformRequest(method, endpoint, bodyContent)
var responseBody map[string]interface{}
if err := json.Unmarshal([]byte(s.Recorder.Body.String()), &responseBody); err != nil {
log.Debug().Err(err).Msg("Error parsing the response body")
}
if value, ok := responseBody["message"]; ok {
s.Message = value.(string)
}
Expect(s.Err).NotTo(HaveOccurred())
}
func (s *ServerTestFeature) PerformRequest(method, endpoint string, body []byte) {
body = s.performSubstitutions(body)
currentLifecycle := strings.ToLower(viper.GetString("lifecycle"))
if currentLifecycle == "" || currentLifecycle == "local" {
url := fmt.Sprintf("/%s%s", s.ServiceClient.Path(), endpoint)
req, err := http.NewRequest(method, url, bytes.NewReader(body))
Expect(err).NotTo(HaveOccurred())
if s.Token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.Token))
}
if method == "POST" {
req.Header.Set("Content-Type", "application/json")
req.Body = io.NopCloser(bytes.NewReader(body))
}
s.Engine.ServeHTTP(s.Recorder, req)
s.Response = s.Recorder.Result()
s.ResponseString = s.Recorder.Body.String()
s.StatusCode = s.Recorder.Code
s.Err = nil
} else {
req := s.Client.R()
if s.Token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.Token))
}
url := fmt.Sprintf("%s%s", s.ServiceClient.GetURL(), endpoint)
var res *resty.Response
var err error
switch method {
case "GET":
res, err = req.Get(url)
case "POST":
req.SetBody(body)
res, err = req.Post(url)
}
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())
log.Debug().
Str("endpoint", endpoint).
Str("response", string(res.Body())).
Int("status_code", res.StatusCode()).
Msg("HTTP RESPONSE")
s.Response = res.RawResponse
s.ResponseString = string(res.Body())
s.StatusCode = res.StatusCode()
s.Err = err
}
}
func (s *ServerTestFeature) makeGraphQLRequest(endpoint, serviceName, body string) error {
req := s.Client.R()
body = string(s.performSubstitutions([]byte(body)))
if s.SendFileKey != "" && s.SendFile != "" {
req.FormData.Add("operations", body)
req.FormData.Add("map", fmt.Sprintf(`{"0": ["variables.%s"]}`, s.SendFileKey))
req.SetFiles(map[string]string{"0": s.SendFile})
} else {
req.SetHeader("Content-Type", "application/json").
SetBody(body)
}
if s.Token != "" {
req.SetAuthToken(s.Token)
}
method := "POST"
serviceConfig := urlfinder.ServiceConfig{
ServiceName: serviceName,
Region: viper.GetString("region"),
Lifecycle: viper.GetString("lifecycle"),
}
url := serviceConfig.FormatURL() + endpoint
url = string(s.performSubstitutions([]byte(url)))
log.Info().Msgf("URL: %s: - Method %s", url, method)
response, err := req.Post(url)
if err != nil {
return err
}
log.Debug().Msgf("RESPONSE: %v", response)
s.Response = response.RawResponse
s.ResponseString = response.String()
s.StatusCode = response.StatusCode()
// reset so it is not used automatically for the next request
s.SendFileKey = ""
s.SendFileKey = ""
return nil
}