-
Notifications
You must be signed in to change notification settings - Fork 18
/
roundtripper.go
276 lines (220 loc) · 6.6 KB
/
roundtripper.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package cute
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/ozontech/allure-go/pkg/allure"
cuteErrors "github.com/ozontech/cute/errors"
"github.com/ozontech/cute/internal/utils"
"moul.io/http2curl/v2"
)
func (it *Test) makeRequest(t internalT, req *http.Request) (*http.Response, []error) {
var (
delay = defaultDelayRepeat
countRepeat = 1
resp *http.Response
err error
scope = make([]error, 0)
)
if it.Request.Retry.Delay != 0 {
delay = it.Request.Retry.Delay
}
if it.Request.Retry.Count != 0 {
countRepeat = it.Request.Retry.Count
}
for i := 1; i <= countRepeat; i++ {
it.executeWithStep(t, createTitle(i, countRepeat, req), func(t T) []error {
resp, err = it.doRequest(t, req)
if err != nil {
if it.Request.Retry.Broken {
err = wrapBrokenError(err)
}
if it.Request.Retry.Optional {
err = wrapOptionalError(err)
}
return []error{err}
}
return nil
})
if err == nil {
break
}
scope = append(scope, err)
if i != countRepeat {
time.Sleep(delay)
}
}
return resp, scope
}
func (it *Test) doRequest(t T, baseReq *http.Request) (*http.Response, error) {
// copy request, because body can be read once
req, err := copyRequest(baseReq.Context(), baseReq)
if err != nil {
return nil, cuteErrors.NewCuteError("[Internal] Could not copy request", err)
}
resp, httpErr := it.httpClient.Do(req)
// if the timeout is triggered, we properly log the timeout error on allure and in traces
if errors.Is(httpErr, context.DeadlineExceeded) {
// Add information (method, host, curl) about request to Allure step
// should be after httpClient.Do and from resp.Request, because in roundTripper request may be changed
if addErr := it.addInformationRequest(t, req); addErr != nil {
// Ignore err return, because it's connected with test logic
it.Error(t, "Could not log information about request. error %v", addErr)
}
return nil, cuteErrors.NewEmptyAssertError(
"Request timeout",
fmt.Sprintf("expected request to be completed in %v, but was not", it.Expect.ExecuteTime))
}
// http client has case wheh it return response and error in one time
// we have to check this case
if resp == nil {
if httpErr != nil {
return nil, cuteErrors.NewCuteError("[HTTP] Could not do request", httpErr)
}
// if response is nil, we can't get information about request and response
return nil, cuteErrors.NewCuteError("[HTTP] Response is nil", httpErr)
}
// BAD CODE. Need to copy body, because we can't read body again from resp.Request.Body. Problem is io.Reader
resp.Request.Body, baseReq.Body, err = utils.DrainBody(baseReq.Body)
if err != nil {
it.Error(t, "Could not drain body from baseReq.Body. error %v", err)
// Ignore err return, because it's connected with test logic
}
// Add information (method, host, curl) about request to Allure step
// should be after httpClient.Do and from resp.Request, because in roundTripper request may be changed
if addErr := it.addInformationRequest(t, resp.Request); addErr != nil {
it.Error(t, "Could not log information about request. error %v", addErr)
// Ignore err return, because it's connected with test logic
}
if httpErr != nil {
return nil, cuteErrors.NewCuteError("[HTTP] Could not do request", httpErr)
}
// Add information (code, body, headers) about response to Allure step
if addErr := it.addInformationResponse(t, resp); addErr != nil {
// Ignore err return, because it's connected with test logic
it.Error(t, "Could not log information about response. error %v", addErr)
}
if validErr := it.validateResponseCode(resp); validErr != nil {
return resp, validErr
}
return resp, nil
}
func (it *Test) validateResponseCode(resp *http.Response) error {
if it.Expect.Code != 0 && it.Expect.Code != resp.StatusCode {
return cuteErrors.NewAssertError(
"Assert response code",
fmt.Sprintf("Response code expect %v, but was %v", it.Expect.Code, resp.StatusCode),
resp.StatusCode,
it.Expect.Code)
}
return nil
}
func (it *Test) addInformationRequest(t T, req *http.Request) error {
var (
saveBody io.ReadCloser
err error
)
curl, err := http2curl.GetCurlCommand(req)
if err != nil {
return err
}
if c := curl.String(); len(c) <= 2048 {
it.Info(t, "[Request] "+c)
} else {
it.Info(t, "[Request] Do request")
}
// Do not change to JSONMarshaler
// In this case we can keep default for keep JSON, independence from JSONMarshaler
headers, err := utils.ToJSON(req.Header)
if err != nil {
return err
}
t.WithParameters(
allure.NewParameters(
"method", req.Method,
"host", req.Host,
"headers", headers,
"curl", curl.String(),
)...,
)
if req.Body != nil {
saveBody, req.Body, err = utils.DrainBody(req.Body)
if err != nil {
return err
}
body, err := utils.GetBody(saveBody)
if err != nil {
return err
}
if len(body) != 0 {
t.WithNewParameters("body", string(body))
}
}
return nil
}
func copyRequest(ctx context.Context, req *http.Request) (*http.Request, error) {
var (
err error
clone = req.Clone(ctx)
)
req.Body, clone.Body, err = utils.DrainBody(req.Body)
if err != nil {
return nil, err
}
return clone, nil
}
func (it *Test) addInformationResponse(t T, response *http.Response) error {
var (
saveBody io.ReadCloser
err error
)
headers, _ := utils.ToJSON(response.Header)
if headers != "" {
t.WithNewParameters("response_headers", headers)
}
t.WithNewParameters("response_code", fmt.Sprint(response.StatusCode))
it.Info(t, "[Response] Status: "+response.Status)
if response.Body == nil {
return nil
}
saveBody, response.Body, err = utils.DrainBody(response.Body)
// if could not get body from response, no add to allure
if err != nil {
return err
}
body, err := utils.GetBody(saveBody)
// if could not get body from response, no add to allure
if err != nil {
return err
}
// if body is empty - skip
if len(body) == 0 {
return nil
}
responseType := allure.Text
if _, ok := response.Header["Content-Type"]; ok {
if len(response.Header["Content-Type"]) > 0 {
if strings.Contains(response.Header["Content-Type"][0], "application/json") {
responseType = allure.JSON
} else {
responseType = allure.MimeType(response.Header["Content-Type"][0])
}
}
}
if responseType == allure.JSON {
body, _ = utils.PrettyJSON(body)
}
t.WithAttachments(allure.NewAttachment("response", responseType, body))
return nil
}
func createTitle(try, countRepeat int, req *http.Request) string {
title := req.Method + " " + req.URL.String()
if countRepeat == 1 {
return title
}
return fmt.Sprintf("[%v/%v] %v", try, countRepeat, title)
}