-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathvariables.go
More file actions
207 lines (188 loc) · 5.82 KB
/
Copy pathvariables.go
File metadata and controls
207 lines (188 loc) · 5.82 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
package render
import (
"fmt"
"sort"
"strings"
"github.com/bootdotdev/bootdev/checks"
api "github.com/bootdotdev/bootdev/client"
"github.com/goccy/go-json"
)
type variableEntry struct {
name string
value string
description string
}
func renderVariableSection(title string, entries []variableEntry) string {
sort.Slice(entries, func(i, j int) bool {
if entries[i].name == entries[j].name {
return entries[i].description < entries[j].description
}
return entries[i].name < entries[j].name
})
var str strings.Builder
fmt.Fprintf(&str, " %s: \n", title)
for _, entry := range entries {
fmt.Fprintf(&str, " - %s: %s (%s)\n", entry.name, formatVariableValue(entry.value), entry.description)
}
return str.String()
}
func formatVariableValue(value string) string {
if value == "" {
return "[not found]"
}
return value
}
func savedVariablesForHTTPResult(result api.HTTPRequestResult) []variableEntry {
var entries []variableEntry
for _, responseVariable := range result.Request.ResponseVariables {
value := result.Variables[responseVariable.Name]
if value == "" {
continue
}
entries = append(entries, variableEntry{
name: responseVariable.Name,
value: value,
description: "JSON Body " + responseVariable.Path,
})
}
for _, responseHeaderVariable := range result.Request.ResponseHeaderVariables {
value := result.Variables[responseHeaderVariable.Name]
if value == "" {
continue
}
entries = append(entries, variableEntry{
name: responseHeaderVariable.Name,
value: value,
description: responseHeaderVariableDescription(responseHeaderVariable),
})
}
return entries
}
func missingSaveVariablesForHTTPResult(result api.HTTPRequestResult) []variableEntry {
var entries []variableEntry
for _, responseVariable := range result.Request.ResponseVariables {
if result.Variables[responseVariable.Name] != "" {
continue
}
entries = append(entries, variableEntry{
name: responseVariable.Name,
description: "JSON Body " + responseVariable.Path,
})
}
for _, responseHeaderVariable := range result.Request.ResponseHeaderVariables {
if result.Variables[responseHeaderVariable.Name] != "" {
continue
}
entries = append(entries, variableEntry{
name: responseHeaderVariable.Name,
description: responseHeaderVariableDescription(responseHeaderVariable),
})
}
return entries
}
func responseHeaderVariableDescription(v api.HTTPRequestResponseHeaderVariable) string {
if v.Regex == "" {
return "Response Header " + v.Header
}
return fmt.Sprintf("Response Header %s matching %s", v.Header, v.Regex)
}
func availableVariablesForHTTPResult(result api.HTTPRequestResult) (entries []variableEntry, expectsVariables bool) {
seen := map[string]bool{}
add := func(name, description string) {
if name == "baseURL" {
return
}
expectsVariables = true
value := result.Variables[name]
key := name + "\x00" + description
if seen[key] {
return
}
seen[key] = true
entries = append(entries, variableEntry{
name: name,
value: value,
description: description,
})
}
addInterpolationNames := func(value, description string) {
for _, name := range checks.InterpolationNames(value) {
add(name, description)
}
}
addInterpolationNames(result.Request.Request.FullURL, "Request URL")
for key, value := range result.Request.Request.Headers {
addInterpolationNames(value, fmt.Sprintf("Request Header %q", key))
}
for key, value := range result.Request.Request.BodyForm {
addInterpolationNames(value, fmt.Sprintf("Request Form Field %q", key))
}
if result.Request.Request.BodyJSON != nil {
if body, err := json.Marshal(result.Request.Request.BodyJSON); err == nil {
addInterpolationNames(string(body), "Request JSON Body")
}
}
for _, test := range result.Request.Tests {
if test.BodyContains != nil {
addInterpolationNames(*test.BodyContains, "Body Contains Test")
}
if test.BodyContainsNone != nil {
addInterpolationNames(*test.BodyContainsNone, "Body Excludes Test")
}
if test.HeadersContain != nil {
addInterpolationNames(test.HeadersContain.Key, "Header Test Key")
addInterpolationNames(test.HeadersContain.Value, "Header Test Value")
}
if test.TrailersContain != nil {
addInterpolationNames(test.TrailersContain.Key, "Trailer Test Key")
addInterpolationNames(test.TrailersContain.Value, "Trailer Test Value")
}
if test.JSONValue != nil && test.JSONValue.StringValue != nil {
addInterpolationNames(*test.JSONValue.StringValue, "JSON Value Test")
}
}
return entries, expectsVariables
}
func availableVariablesForCLIResult(result api.CLICommandResult) (entries []variableEntry, expectsVariables bool) {
seen := map[string]bool{}
add := func(name, description string) {
expectsVariables = true
value := result.Variables[name]
key := name + "\x00" + description
if seen[key] {
return
}
seen[key] = true
entries = append(entries, variableEntry{
name: name,
value: value,
description: description,
})
}
addInterpolationNames := func(value, description string) {
for _, name := range checks.InterpolationNames(value) {
add(name, description)
}
}
addInterpolationNames(result.Command.Command, "Command")
for _, test := range result.Command.Tests {
for _, contains := range test.StdoutContainsAll {
addInterpolationNames(contains, "Stdout Contains Test")
}
for _, contains := range test.StdoutContainsNone {
addInterpolationNames(contains, "Stdout Excludes Test")
}
if test.StdoutJq != nil {
addInterpolationNames(test.StdoutJq.Query, "JQ Query")
for _, expected := range test.StdoutJq.ExpectedResults {
if expected.Type != api.JqTypeString {
continue
}
if value, ok := expected.Value.(string); ok {
addInterpolationNames(value, "JQ Expected Value")
}
}
}
}
return entries, expectsVariables
}