-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathhttp.go
More file actions
322 lines (287 loc) · 9.08 KB
/
Copy pathhttp.go
File metadata and controls
322 lines (287 loc) · 9.08 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package checks
import (
"bytes"
"fmt"
"io"
"maps"
"net/http"
"net/url"
"regexp"
"slices"
"strings"
"unicode/utf8"
api "github.com/bootdotdev/bootdev/client"
"github.com/goccy/go-json"
"github.com/spf13/cobra"
)
func runHTTPRequest(
client *http.Client,
baseURL string,
variables map[string]string,
requestStep api.CLIStepHTTPRequest,
) (
result api.HTTPRequestResult,
) {
finalBaseURL := strings.TrimSuffix(baseURL, "/")
interpolatedURL := InterpolateVariables(requestStep.Request.FullURL, variables)
completeURL := strings.Replace(interpolatedURL, api.BaseURLPlaceholder, finalBaseURL, 1)
var req *http.Request
if requestStep.Request.BodyJSON != nil {
dat, err := json.Marshal(requestStep.Request.BodyJSON)
cobra.CheckErr(err)
interpolatedBodyJSONStr := InterpolateVariables(string(dat), variables)
req, err = http.NewRequest(
requestStep.Request.Method, completeURL,
bytes.NewBuffer([]byte(interpolatedBodyJSONStr)),
)
if err != nil {
cobra.CheckErr("Failed to create request")
}
req.Header.Set("Content-Type", "application/json")
} else if requestStep.Request.BodyForm != nil {
formValues := url.Values{}
for key, val := range requestStep.Request.BodyForm {
interpolatedVal := InterpolateVariables(val, variables)
formValues.Add(key, interpolatedVal)
}
encodedFormStr := formValues.Encode()
var err error
req, err = http.NewRequest(
requestStep.Request.Method, completeURL,
strings.NewReader(encodedFormStr),
)
if err != nil {
cobra.CheckErr("Failed to create request")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else {
var err error
req, err = http.NewRequest(requestStep.Request.Method, completeURL, nil)
if err != nil {
cobra.CheckErr("Failed to create request")
}
}
for k, v := range requestStep.Request.Headers {
req.Header.Add(k, InterpolateVariables(v, variables))
}
if requestStep.Request.BasicAuth != nil {
req.SetBasicAuth(requestStep.Request.BasicAuth.Username, requestStep.Request.BasicAuth.Password)
}
requestClient := client
if requestStep.Request.FollowRedirects != nil && !*requestStep.Request.FollowRedirects {
clientCopy := *client
clientCopy.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
requestClient = &clientCopy
}
resp, err := requestClient.Do(req)
if err != nil {
errString := fmt.Sprintf("Failed to fetch: %s", err.Error())
result = api.HTTPRequestResult{Err: errString}
return result
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
result = api.HTTPRequestResult{Err: "Failed to read response body"}
return result
}
headers := make(map[string]string)
for k, v := range resp.Header {
headers[k] = strings.Join(v, ",")
}
trailers := make(map[string]string)
for k, v := range resp.Trailer {
trailers[k] = strings.Join(v, ",")
}
if err := parseVariables(body, requestStep.ResponseVariables, variables); err != nil {
return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to parse response variable: %s", err)}
}
if err := parseHeaderVariables(headers, requestStep.ResponseHeaderVariables, variables); err != nil {
return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to parse response header variable: %s", err)}
}
result = api.HTTPRequestResult{
StatusCode: resp.StatusCode,
ResponseHeaders: headers,
ResponseTrailers: trailers,
BodyString: truncateAndStringifyBody(body),
Variables: maps.Clone(variables),
Request: requestStep,
}
return result
}
func prettyPrintHTTPTest(test api.HTTPRequestTest, variables map[string]string) string {
if test.StatusCode != nil {
return fmt.Sprintf("Expecting status code: %d", *test.StatusCode)
}
if test.BodyContains != nil {
interpolated := InterpolateVariables(*test.BodyContains, variables)
return fmt.Sprintf("Expecting response body to contain: %s", interpolated)
}
if test.BodyContainsNone != nil {
interpolated := InterpolateVariables(*test.BodyContainsNone, variables)
return fmt.Sprintf("Expecting response body to not contain: %s", interpolated)
}
if test.HeadersContain != nil {
interpolatedKey := InterpolateVariables(test.HeadersContain.Key, variables)
interpolatedValue := InterpolateVariables(test.HeadersContain.Value, variables)
return fmt.Sprintf("Expecting headers to contain: '%s: %v'", interpolatedKey, interpolatedValue)
}
if test.TrailersContain != nil {
interpolatedKey := InterpolateVariables(test.TrailersContain.Key, variables)
interpolatedValue := InterpolateVariables(test.TrailersContain.Value, variables)
return fmt.Sprintf("Expecting trailers to contain: '%s: %v'", interpolatedKey, interpolatedValue)
}
if test.JSONValue != nil {
var val any
switch {
case test.JSONValue.IntValue != nil:
val = *test.JSONValue.IntValue
case test.JSONValue.StringValue != nil:
val = *test.JSONValue.StringValue
case test.JSONValue.BoolValue != nil:
val = *test.JSONValue.BoolValue
}
var op string
switch test.JSONValue.Operator {
case api.OpEquals:
op = "to be equal to"
case api.OpGreaterThan:
op = "to be greater than"
case api.OpContains:
op = "contains"
case api.OpNotContains:
op = "to not contain"
}
expecting := fmt.Sprintf("Expecting JSON at %v %s %v", test.JSONValue.Path, op, val)
return InterpolateVariables(expecting, variables)
}
return ""
}
// Return a capped string representation of the response body.
//
// Text-like responses are allowed up to ~1 MiB, while likely-binary responses are
// capped more aggressively (~16 KiB) to avoid large payloads when serialized to JSON.
//
// We intentionally stringify raw bytes, even for binary data, so that ASCII markers
// embedded in binary (e.g. "moov" in MP4 files) remain searchable by downstream checks.
// The result is not guaranteed to be valid UTF-8 or lossless.
func truncateAndStringifyBody(body []byte) string {
maxBodyLength := 1024 * 1024 // 1 MiB
if likelyBinary(body) {
maxBodyLength = 16 * 1024 // 16 KiB
}
if len(body) > maxBodyLength {
body = body[:maxBodyLength]
body = trimIncompleteUTF8(body)
}
return string(body)
}
func parseVariables(body []byte, vardefs []api.HTTPRequestResponseVariable, variables map[string]string) error {
bodyString := string(body)
for _, vardef := range vardefs {
switch {
case vardef.Path != "" && vardef.BodyRegex != "":
return fmt.Errorf("invalid response variable configuration")
case vardef.BodyRegex != "":
re, err := regexp.Compile(vardef.BodyRegex)
if err != nil {
return fmt.Errorf("invalid response body variable configuration")
}
if re.NumSubexp() != 1 {
return fmt.Errorf("invalid response body variable configuration")
}
matches := re.FindStringSubmatch(bodyString)
if len(matches) == 2 {
variables[vardef.Name] = matches[1]
}
case vardef.Path != "":
vals, err := valsFromJqPath(vardef.Path, bodyString)
if err != nil {
return err
}
if len(vals) == 1 && vals[0] != nil {
variables[vardef.Name] = fmt.Sprintf("%v", vals[0])
}
default:
return fmt.Errorf("invalid response variable configuration")
}
}
return nil
}
func parseHeaderVariables(headers map[string]string, vardefs []api.HTTPRequestResponseHeaderVariable, variables map[string]string) error {
for _, vardef := range vardefs {
headerValue, ok := findHeaderValue(headers, vardef.Header)
if !ok || headerValue == "" {
continue
}
value := headerValue
if vardef.Regex != "" {
re, err := regexp.Compile(vardef.Regex)
if err != nil {
return fmt.Errorf("invalid response header variable configuration")
}
if re.NumSubexp() != 1 {
return fmt.Errorf("invalid response header variable configuration")
}
matches := re.FindStringSubmatch(headerValue)
if len(matches) != 2 {
continue
}
value = matches[1]
}
variables[vardef.Name] = value
}
return nil
}
func findHeaderValue(headers map[string]string, key string) (string, bool) {
for actualKey, value := range headers {
if strings.EqualFold(actualKey, key) {
return value, true
}
}
return "", false
}
func InterpolateVariables(template string, vars map[string]string) string {
r := regexp.MustCompile(`\$\{([^}]+)\}`)
return r.ReplaceAllStringFunc(template, func(m string) string {
// Extract the key from the match, which is in the form ${key}
key := strings.TrimSuffix(strings.TrimPrefix(m, "${"), "}")
if val, ok := vars[key]; ok {
return val
}
return m
})
}
func InterpolationNames(template string) []string {
r := regexp.MustCompile(`\$\{([^}]+)\}`)
matches := r.FindAllStringSubmatch(template, -1)
names := make([]string, 0, len(matches))
for _, match := range matches {
if len(match) > 1 {
names = append(names, match[1])
}
}
return names
}
func likelyBinary(b []byte) bool {
if len(b) == 0 {
return false
}
if len(b) > 8000 {
b = b[:8000]
b = trimIncompleteUTF8(b) // in case we broke a multi-byte char
}
return slices.Contains(b, 0) || !utf8.Valid(b)
}
func trimIncompleteUTF8(b []byte) []byte {
for i := 0; i < 3 && len(b) > 0; i++ {
r, size := utf8.DecodeLastRune(b)
if r != utf8.RuneError || size != 1 {
break
}
b = b[:len(b)-1]
}
return b
}