Skip to content

Commit d5a5ebb

Browse files
authored
add vars to render (#61)
1 parent a8bb5a5 commit d5a5ebb

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

checks/http.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ func HttpTest(
4545
cobra.CheckErr("no base URL provided")
4646
}
4747
finalBaseURL = strings.TrimSuffix(finalBaseURL, "/")
48-
interpolatedPath := interpolateVariables(request.Request.Path, variables)
48+
interpolatedPath := InterpolateVariables(request.Request.Path, variables)
4949
completeURL := fmt.Sprintf("%s%s", finalBaseURL, interpolatedPath)
5050

5151
var r *http.Request
5252
if request.Request.BodyJSON != nil {
5353
dat, err := json.Marshal(request.Request.BodyJSON)
5454
cobra.CheckErr(err)
55-
interpolatedBodyJSONStr := interpolateVariables(string(dat), variables)
55+
interpolatedBodyJSONStr := InterpolateVariables(string(dat), variables)
5656
r, err = http.NewRequest(request.Request.Method, completeURL,
5757
bytes.NewBuffer([]byte(interpolatedBodyJSONStr)),
5858
)
@@ -69,7 +69,7 @@ func HttpTest(
6969
}
7070

7171
for k, v := range request.Request.Headers {
72-
r.Header.Add(k, interpolateVariables(v, variables))
72+
r.Header.Add(k, InterpolateVariables(v, variables))
7373
}
7474

7575
if request.Request.BasicAuth != nil {
@@ -163,7 +163,7 @@ func valsFromJQPath(path string, jsn string) ([]any, error) {
163163
return vals, nil
164164
}
165165

166-
func interpolateVariables(template string, vars map[string]string) string {
166+
func InterpolateVariables(template string, vars map[string]string) string {
167167
r := regexp.MustCompile(`\$\{([^}]+)\}`)
168168
return r.ReplaceAllStringFunc(template, func(m string) string {
169169
// Extract the key from the match, which is in the form ${key}

cmd/upgrade.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ var upgradeCmd = &cobra.Command{
2222
}
2323
// install the latest version
2424
command := exec.Command("go", "install", "github.com/bootdotdev/bootdev@latest")
25-
b, err := command.Output()
25+
_, err := command.Output()
2626
cobra.CheckErr(err)
2727

2828
// Get the new version info
2929
command = exec.Command("bootdev", "--version")
30-
b, err = command.Output()
30+
b, err := command.Output()
3131
cobra.CheckErr(err)
3232
re := regexp.MustCompile(`v\d+\.\d+\.\d+`)
3333
version := re.FindString(string(b))

render/http.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ func printHTTPResult(result checks.HttpTestResult) string {
133133
if result.Err != "" {
134134
str += fmt.Sprintf(" Err: %v\n", result.Err)
135135
} else {
136-
str += " Request Headers: \n"
137-
for k, v := range result.RequestHeaders {
138-
str += fmt.Sprintf(" - %v: %v\n", k, v[0])
136+
if len(result.RequestHeaders) > 0 {
137+
str += " Request Headers: \n"
138+
for k, v := range result.RequestHeaders {
139+
str += fmt.Sprintf(" - %v: %v\n", k, v[0])
140+
}
139141
}
140142
str += fmt.Sprintf(" Response Status Code: %v\n", result.StatusCode)
141143
str += " Response Body: \n"
@@ -210,7 +212,7 @@ func httpRenderer(
210212
for i, req := range data.HttpTests.Requests {
211213
ch <- startHttpMsg{path: req.Request.Path, method: req.Request.Method}
212214
for _, test := range req.Tests {
213-
ch <- startTestMsg{text: prettyPrintHTTPTest(test)}
215+
ch <- startTestMsg{text: prettyPrintHTTPTest(test, results[i].Variables)}
214216
}
215217
time.Sleep(500 * time.Millisecond)
216218
for j := range req.Tests {
@@ -243,7 +245,7 @@ func httpRenderer(
243245
wg.Wait()
244246
}
245247

246-
func prettyPrintHTTPTest(test api.HTTPTest) string {
248+
func prettyPrintHTTPTest(test api.HTTPTest, variables map[string]string) string {
247249
if test.StatusCode != nil {
248250
return fmt.Sprintf("Expecting status code: %d", *test.StatusCode)
249251
}
@@ -266,13 +268,13 @@ func prettyPrintHTTPTest(test api.HTTPTest) string {
266268
} else if test.JSONValue.BoolValue != nil {
267269
val = *test.JSONValue.BoolValue
268270
}
269-
270271
if test.JSONValue.Operator == api.OpEquals {
271272
op = "to be equal to"
272273
} else if test.JSONValue.Operator == api.OpGreaterThan {
273274
op = "to be greater than"
274275
}
275-
return fmt.Sprintf("Expecting JSON at %v %s %v", test.JSONValue.Path, op, val)
276+
expecting := fmt.Sprintf("Expecting JSON at %v %s %v", test.JSONValue.Path, op, val)
277+
return checks.InterpolateVariables(expecting, variables)
276278
}
277279
return ""
278280
}

version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.11.0
1+
v1.11.1

0 commit comments

Comments
 (0)