Skip to content

Commit d79a1ce

Browse files
committed
add response trailers to cli http requests
1 parent c5b5b9e commit d79a1ce

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

checks/checks.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,20 @@ func runHTTPRequest(
105105
headers[k] = strings.Join(v, ",")
106106
}
107107

108+
trailers := make(map[string]string)
109+
for k, v := range resp.Trailer {
110+
trailers[k] = strings.Join(v, ",")
111+
}
112+
108113
parseVariables(body, requestStep.ResponseVariables, variables)
109114

110115
result = api.HTTPRequestResult{
111-
StatusCode: resp.StatusCode,
112-
ResponseHeaders: headers,
113-
BodyString: truncateAndStringifyBody(body),
114-
Variables: variables,
115-
Request: requestStep,
116+
StatusCode: resp.StatusCode,
117+
ResponseHeaders: headers,
118+
ResponseTrailers: trailers,
119+
BodyString: truncateAndStringifyBody(body),
120+
Variables: variables,
121+
Request: requestStep,
116122
}
117123
return result
118124
}

client/lessons.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type HTTPRequestTest struct {
6868
BodyContains *string
6969
BodyContainsNone *string
7070
HeadersContain *HTTPRequestTestHeader
71+
TrailersContain *HTTPRequestTestHeader
7172
JSONValue *HTTPRequestTestJSONValue
7273
}
7374

@@ -120,12 +121,13 @@ type CLICommandResult struct {
120121
}
121122

122123
type HTTPRequestResult struct {
123-
Err string `json:"-"`
124-
StatusCode int
125-
ResponseHeaders map[string]string
126-
BodyString string
127-
Variables map[string]string
128-
Request CLIStepHTTPRequest
124+
Err string `json:"-"`
125+
StatusCode int
126+
ResponseHeaders map[string]string
127+
ResponseTrailers map[string]string
128+
BodyString string
129+
Variables map[string]string
130+
Request CLIStepHTTPRequest
129131
}
130132

131133
type lessonSubmissionCLI struct {

render/render.go

+26
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ func printHTTPRequestResult(result api.HTTPRequestResult) string {
289289
}
290290
}
291291

292+
filteredTrailers := make(map[string]string)
293+
for respK, respV := range result.ResponseTrailers {
294+
for _, test := range result.Request.Tests {
295+
if test.TrailersContain == nil {
296+
continue
297+
}
298+
299+
interpolatedTestTrailerKey := checks.InterpolateVariables(test.TrailersContain.Key, result.Variables)
300+
if strings.EqualFold(respK, interpolatedTestTrailerKey) {
301+
filteredTrailers[respK] = respV
302+
}
303+
}
304+
}
305+
292306
if len(filteredHeaders) > 0 {
293307
str += " Response Headers: \n"
294308
for k, v := range filteredHeaders {
@@ -317,6 +331,13 @@ func printHTTPRequestResult(result api.HTTPRequestResult) string {
317331
}
318332
str += "\n"
319333

334+
if len(filteredTrailers) > 0 {
335+
str += " Response Trailers: \n"
336+
for k, v := range filteredTrailers {
337+
str += fmt.Sprintf(" - %v: %v\n", k, v)
338+
}
339+
}
340+
320341
if len(result.Variables) > 0 {
321342
str += " Variables available: \n"
322343
for k, v := range result.Variables {
@@ -554,6 +575,11 @@ func prettyPrintHTTPTest(test api.HTTPRequestTest, variables map[string]string)
554575
interpolatedValue := checks.InterpolateVariables(test.HeadersContain.Value, variables)
555576
return fmt.Sprintf("Expecting headers to contain: '%s: %v'", interpolatedKey, interpolatedValue)
556577
}
578+
if test.TrailersContain != nil {
579+
interpolatedKey := checks.InterpolateVariables(test.TrailersContain.Key, variables)
580+
interpolatedValue := checks.InterpolateVariables(test.TrailersContain.Value, variables)
581+
return fmt.Sprintf("Expecting trailers to contain: '%s: %v'", interpolatedKey, interpolatedValue)
582+
}
557583
if test.JSONValue != nil {
558584
var val any
559585
var op any

0 commit comments

Comments
 (0)