Skip to content

Commit

Permalink
add vars to render (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
skovranek authored Nov 18, 2024
1 parent a8bb5a5 commit d5a5ebb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions checks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func HttpTest(
cobra.CheckErr("no base URL provided")
}
finalBaseURL = strings.TrimSuffix(finalBaseURL, "/")
interpolatedPath := interpolateVariables(request.Request.Path, variables)
interpolatedPath := InterpolateVariables(request.Request.Path, variables)
completeURL := fmt.Sprintf("%s%s", finalBaseURL, interpolatedPath)

var r *http.Request
if request.Request.BodyJSON != nil {
dat, err := json.Marshal(request.Request.BodyJSON)
cobra.CheckErr(err)
interpolatedBodyJSONStr := interpolateVariables(string(dat), variables)
interpolatedBodyJSONStr := InterpolateVariables(string(dat), variables)
r, err = http.NewRequest(request.Request.Method, completeURL,
bytes.NewBuffer([]byte(interpolatedBodyJSONStr)),
)
Expand All @@ -69,7 +69,7 @@ func HttpTest(
}

for k, v := range request.Request.Headers {
r.Header.Add(k, interpolateVariables(v, variables))
r.Header.Add(k, InterpolateVariables(v, variables))
}

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

func interpolateVariables(template string, vars map[string]string) string {
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}
Expand Down
4 changes: 2 additions & 2 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ var upgradeCmd = &cobra.Command{
}
// install the latest version
command := exec.Command("go", "install", "github.com/bootdotdev/bootdev@latest")
b, err := command.Output()
_, err := command.Output()
cobra.CheckErr(err)

// Get the new version info
command = exec.Command("bootdev", "--version")
b, err = command.Output()
b, err := command.Output()
cobra.CheckErr(err)
re := regexp.MustCompile(`v\d+\.\d+\.\d+`)
version := re.FindString(string(b))
Expand Down
16 changes: 9 additions & 7 deletions render/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ func printHTTPResult(result checks.HttpTestResult) string {
if result.Err != "" {
str += fmt.Sprintf(" Err: %v\n", result.Err)
} else {
str += " Request Headers: \n"
for k, v := range result.RequestHeaders {
str += fmt.Sprintf(" - %v: %v\n", k, v[0])
if len(result.RequestHeaders) > 0 {
str += " Request Headers: \n"
for k, v := range result.RequestHeaders {
str += fmt.Sprintf(" - %v: %v\n", k, v[0])
}
}
str += fmt.Sprintf(" Response Status Code: %v\n", result.StatusCode)
str += " Response Body: \n"
Expand Down Expand Up @@ -210,7 +212,7 @@ func httpRenderer(
for i, req := range data.HttpTests.Requests {
ch <- startHttpMsg{path: req.Request.Path, method: req.Request.Method}
for _, test := range req.Tests {
ch <- startTestMsg{text: prettyPrintHTTPTest(test)}
ch <- startTestMsg{text: prettyPrintHTTPTest(test, results[i].Variables)}
}
time.Sleep(500 * time.Millisecond)
for j := range req.Tests {
Expand Down Expand Up @@ -243,7 +245,7 @@ func httpRenderer(
wg.Wait()
}

func prettyPrintHTTPTest(test api.HTTPTest) string {
func prettyPrintHTTPTest(test api.HTTPTest, variables map[string]string) string {
if test.StatusCode != nil {
return fmt.Sprintf("Expecting status code: %d", *test.StatusCode)
}
Expand All @@ -266,13 +268,13 @@ func prettyPrintHTTPTest(test api.HTTPTest) string {
} else if test.JSONValue.BoolValue != nil {
val = *test.JSONValue.BoolValue
}

if test.JSONValue.Operator == api.OpEquals {
op = "to be equal to"
} else if test.JSONValue.Operator == api.OpGreaterThan {
op = "to be greater than"
}
return fmt.Sprintf("Expecting JSON at %v %s %v", test.JSONValue.Path, op, val)
expecting := fmt.Sprintf("Expecting JSON at %v %s %v", test.JSONValue.Path, op, val)
return checks.InterpolateVariables(expecting, variables)
}
return ""
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.11.0
v1.11.1

0 comments on commit d5a5ebb

Please sign in to comment.