|
1 | 1 | package checks |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | api "github.com/bootdotdev/bootdev/client" |
7 | 9 | "github.com/bootdotdev/bootdev/messages" |
8 | 10 | tea "github.com/charmbracelet/bubbletea" |
| 11 | + "github.com/spf13/viper" |
9 | 12 | ) |
10 | 13 |
|
| 14 | +func TestCLIChecksInterpolatesResolvedBaseURLInCommands(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + defaultBaseURL string |
| 18 | + overrideBaseURL string |
| 19 | + want string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "manifest default", |
| 23 | + defaultBaseURL: "http://localhost:3000", |
| 24 | + want: "http://localhost:3000", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "manifest default with trailing slash", |
| 28 | + defaultBaseURL: "http://localhost:3000/", |
| 29 | + want: "http://localhost:3000", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "configured override", |
| 33 | + defaultBaseURL: "http://localhost:3000", |
| 34 | + overrideBaseURL: "http://localhost:4000/", |
| 35 | + want: "http://localhost:4000", |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + cliData := api.CLIData{ |
| 42 | + BaseURLDefault: tt.defaultBaseURL, |
| 43 | + Steps: []api.CLIStep{{ |
| 44 | + CLICommand: &api.CLIStepCLICommand{ |
| 45 | + Command: `echo '${baseURL}'`, |
| 46 | + }, |
| 47 | + }}, |
| 48 | + } |
| 49 | + ch := make(chan tea.Msg, 10) |
| 50 | + |
| 51 | + results := CLIChecks(cliData, tt.overrideBaseURL, ch) |
| 52 | + |
| 53 | + if got := results[0].CLICommandResult.Stdout; got != tt.want { |
| 54 | + t.Fatalf("command stdout = %q, want %q", got, tt.want) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestCLIChecksUsesOverrideParameterForHTTPRequestPreview(t *testing.T) { |
| 61 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 62 | + w.WriteHeader(http.StatusNoContent) |
| 63 | + })) |
| 64 | + defer server.Close() |
| 65 | + |
| 66 | + previousOverride := viper.GetString("override_base_url") |
| 67 | + viper.Set("override_base_url", "http://localhost:1") |
| 68 | + t.Cleanup(func() { |
| 69 | + viper.Set("override_base_url", previousOverride) |
| 70 | + }) |
| 71 | + |
| 72 | + cliData := api.CLIData{ |
| 73 | + BaseURLDefault: "http://localhost:3000", |
| 74 | + Steps: []api.CLIStep{{ |
| 75 | + HTTPRequest: &api.CLIStepHTTPRequest{ |
| 76 | + Request: api.HTTPRequest{ |
| 77 | + Method: http.MethodGet, |
| 78 | + FullURL: api.BaseURLPlaceholder + "/health", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }}, |
| 82 | + } |
| 83 | + messageChannel := make(chan tea.Msg, 10) |
| 84 | + |
| 85 | + results := CLIChecks(cliData, server.URL+"/", messageChannel) |
| 86 | + |
| 87 | + startMessage, ok := (<-messageChannel).(messages.StartStepMsg) |
| 88 | + if !ok { |
| 89 | + t.Fatal("expected start step message") |
| 90 | + } |
| 91 | + if want := server.URL + "/health"; startMessage.URL != want { |
| 92 | + t.Fatalf("preview URL = %q, want %q", startMessage.URL, want) |
| 93 | + } |
| 94 | + if got := results[0].HTTPRequestResult.StatusCode; got != http.StatusNoContent { |
| 95 | + t.Fatalf("response status = %d, want %d", got, http.StatusNoContent) |
| 96 | + } |
| 97 | +} |
| 98 | + |
11 | 99 | func TestApplySubmissionResultsMarksAllStepsAndTestsPassedWhenNoFailure(t *testing.T) { |
12 | 100 | cliData := api.CLIData{Steps: []api.CLIStep{ |
13 | 101 | {CLICommand: &api.CLIStepCLICommand{Tests: []api.CLICommandTest{{}, {}}}}, |
|
0 commit comments