Skip to content

Commit 4c63966

Browse files
committed
remove url paths
- require base url override if necessary - simplify base URL handling - remove URL paths - add cli version request header
1 parent 18e6f97 commit 4c63966

File tree

5 files changed

+56
-53
lines changed

5 files changed

+56
-53
lines changed

checks/checks.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,9 @@ func runHTTPRequest(
4343
) (
4444
result api.HTTPRequestResult,
4545
) {
46-
if baseURL == "" && requestStep.Request.FullURL == "" {
47-
cobra.CheckErr("no base URL or full URL provided")
48-
}
49-
5046
finalBaseURL := strings.TrimSuffix(baseURL, "/")
51-
interpolatedPath := InterpolateVariables(requestStep.Request.Path, variables)
52-
completeURL := fmt.Sprintf("%s%s", finalBaseURL, interpolatedPath)
53-
if requestStep.Request.FullURL != "" {
54-
completeURL = InterpolateVariables(requestStep.Request.FullURL, variables)
55-
}
47+
interpolatedURL := InterpolateVariables(requestStep.Request.FullURL, variables)
48+
completeURL := strings.Replace(interpolatedURL, api.BaseURLPlaceholder, finalBaseURL, 1)
5649

5750
var req *http.Request
5851
if requestStep.Request.BodyJSON != nil {
@@ -123,17 +116,19 @@ func runHTTPRequest(
123116
return result
124117
}
125118

126-
func CLIChecks(cliData api.CLIData, overrideBaseURL *string) (results []api.CLIStepResult) {
119+
func CLIChecks(cliData api.CLIData, overrideBaseURL string) (results []api.CLIStepResult) {
127120
client := &http.Client{}
128121
variables := make(map[string]string)
129122
results = make([]api.CLIStepResult, len(cliData.Steps))
130123

131-
// use cli config url if specified or default lesson data url
132-
baseURL := ""
133-
if overrideBaseURL != nil && *overrideBaseURL != "" {
134-
baseURL = *overrideBaseURL
135-
} else if cliData.BaseURL != nil && *cliData.BaseURL != "" {
136-
baseURL = *cliData.BaseURL
124+
if cliData.BaseURLDefault == api.BaseURLOverrideRequired && overrideBaseURL == "" {
125+
cobra.CheckErr("lesson requires a base URL override - bootdev configure base_url <url>")
126+
}
127+
128+
// prefer overrideBaseURL if provided, otherwise use BaseURLDefault
129+
baseURL := overrideBaseURL
130+
if overrideBaseURL == "" {
131+
baseURL = cliData.BaseURLDefault
137132
}
138133

139134
for i, step := range cliData.Steps {

client/lessons.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ type LessonDataCLI struct {
1717
CLIData CLIData
1818
}
1919

20+
const BaseURLOverrideRequired = "override"
21+
2022
type CLIData struct {
2123
// ContainsCompleteDir bool
22-
BaseURL *string
23-
Steps []struct {
24-
CLICommand *CLIStepCLICommand
25-
HTTPRequest *CLIStepHTTPRequest
26-
}
24+
BaseURLDefault string
25+
Steps []CLIStep
26+
}
27+
28+
type CLIStep struct {
29+
CLICommand *CLIStepCLICommand
30+
HTTPRequest *CLIStepHTTPRequest
2731
}
2832

2933
type CLIStepCLICommand struct {
@@ -41,20 +45,28 @@ type CLICommandTest struct {
4145
type CLIStepHTTPRequest struct {
4246
ResponseVariables []HTTPRequestResponseVariable
4347
Tests []HTTPRequestTest
44-
Request struct {
45-
Method string
46-
Path string
47-
FullURL string // overrides BaseURL and Path if set
48-
Headers map[string]string
49-
BodyJSON map[string]interface{}
50-
BasicAuth *struct {
51-
Username string
52-
Password string
53-
}
54-
Actions struct {
55-
DelayRequestByMs *int32
56-
}
57-
}
48+
Request HTTPRequest
49+
}
50+
51+
const BaseURLPlaceholder = "${baseURL}"
52+
53+
type HTTPRequest struct {
54+
Method string
55+
FullURL string
56+
Headers map[string]string
57+
BodyJSON map[string]any
58+
59+
BasicAuth *HTTPBasicAuth
60+
Actions HTTPActions
61+
}
62+
63+
type HTTPBasicAuth struct {
64+
Username string
65+
Password string
66+
}
67+
68+
type HTTPActions struct {
69+
DelayRequestByMs *int
5870
}
5971

6072
type HTTPRequestResponseVariable struct {

cmd/submit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func submissionHandler(cmd *cobra.Command, args []string) error {
4343

4444
data := lesson.Lesson.LessonDataCLI.CLIData
4545
overrideBaseURL := viper.GetString("override_base_url")
46-
results := checks.CLIChecks(data, &overrideBaseURL)
46+
results := checks.CLIChecks(data, overrideBaseURL)
4747
if isSubmit {
4848
failure, err := api.SubmitCLILesson(lessonUUID, results)
4949
if err != nil {

render/render.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func renderTestResponseVars(respVars []api.HTTPRequestResponseVariable) string {
5252
for _, respVar := range respVars {
5353
varStr := gray.Render(fmt.Sprintf(" * Saving `%s` from `%s`", respVar.Name, respVar.Path))
5454
edges := " ├─"
55-
for i := 0; i < lipgloss.Height(varStr)-1; i++ {
55+
for range lipgloss.Height(varStr) - 1 {
5656
edges += "\n │ "
5757
}
5858
str += lipgloss.JoinHorizontal(lipgloss.Top, edges, varStr)
@@ -68,7 +68,7 @@ func renderTests(tests []testModel, spinner string) string {
6868
testStr = fmt.Sprintf(" %s", testStr)
6969

7070
edges := " ├─"
71-
for i := 0; i < lipgloss.Height(testStr)-1; i++ {
71+
for range lipgloss.Height(testStr) - 1 {
7272
edges += "\n │ "
7373
}
7474
str += lipgloss.JoinHorizontal(lipgloss.Top, edges, testStr)
@@ -314,7 +314,7 @@ func printHTTPRequestResult(result api.HTTPRequestResult) string {
314314
bytes := []byte(result.BodyString)
315315
contentType := http.DetectContentType(bytes)
316316
if contentType == "application/json" || strings.HasPrefix(contentType, "text/") {
317-
var unmarshalled interface{}
317+
var unmarshalled any
318318
err := json.Unmarshal([]byte(result.BodyString), &unmarshalled)
319319
if err == nil {
320320
pretty, err := json.MarshalIndent(unmarshalled, "", " ")
@@ -404,7 +404,7 @@ func renderer(
404404
case step.CLICommand != nil && results[i].CLICommandResult != nil:
405405
renderCLICommand(*step.CLICommand, *results[i].CLICommandResult, failure, isSubmit, ch, i)
406406
case step.HTTPRequest != nil && results[i].HTTPRequestResult != nil:
407-
renderHTTPRequest(*step.HTTPRequest, *results[i].HTTPRequestResult, failure, isSubmit, data.BaseURL, ch, i)
407+
renderHTTPRequest(*step.HTTPRequest, *results[i].HTTPRequestResult, failure, isSubmit, data.BaseURLDefault, ch, i)
408408
default:
409409
cobra.CheckErr("unable to run lesson: missing results")
410410
}
@@ -492,23 +492,19 @@ func renderHTTPRequest(
492492
result api.HTTPRequestResult,
493493
failure *api.StructuredErrCLI,
494494
isSubmit bool,
495-
baseURL *string,
495+
baseURLDefault string,
496496
ch chan tea.Msg,
497497
index int,
498498
) {
499-
url := ""
500-
overrideBaseURL := viper.GetString("override_base_url")
501-
if overrideBaseURL != "" {
502-
url = overrideBaseURL
503-
} else if baseURL != nil && *baseURL != "" {
504-
url = *baseURL
505-
}
506-
if req.Request.FullURL != "" {
507-
url = req.Request.FullURL
499+
500+
baseURL := viper.GetString("override_base_url")
501+
if baseURL == "" {
502+
baseURL = baseURLDefault
508503
}
509-
url += req.Request.Path
504+
fullURL := strings.Replace(req.Request.FullURL, api.BaseURLPlaceholder, baseURL, 1)
505+
510506
ch <- startStepMsg{
511-
url: checks.InterpolateVariables(url, result.Variables),
507+
url: checks.InterpolateVariables(fullURL, result.Variables),
512508
method: req.Request.Method,
513509
responseVariables: req.ResponseVariables,
514510
}

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.17.1
1+
v1.18.0

0 commit comments

Comments
 (0)