|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "os/exec" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestMain_ConnectionRefused(t *testing.T) { |
| 12 | + cmd := exec.Command("go", "run", "./...") |
| 13 | + out, _ := cmd.CombinedOutput() |
| 14 | + |
| 15 | + actual := string(out) |
| 16 | + expected := "UNKNOWN - url and token are required" |
| 17 | + |
| 18 | + if !strings.Contains(actual, expected) { |
| 19 | + t.Error("\nActual: ", actual, "\nExpected: ", expected) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +type IntegrationTest struct { |
| 24 | + name string |
| 25 | + server *httptest.Server |
| 26 | + args []string |
| 27 | + expected string |
| 28 | +} |
| 29 | + |
| 30 | +func TestMainCmd(t *testing.T) { |
| 31 | + tests := []IntegrationTest{ |
| 32 | + { |
| 33 | + name: "invalid-json", |
| 34 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 35 | + w.WriteHeader(http.StatusOK) |
| 36 | + w.Write([]byte(`{"foo}`)) |
| 37 | + })), |
| 38 | + args: []string{"run", "./...", "-T", "test", "--url"}, |
| 39 | + expected: "UNKNOWN - could not decode JSON from body", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "empty-response", |
| 43 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 44 | + w.WriteHeader(http.StatusOK) |
| 45 | + w.Write([]byte(`{"data" : [ {"id": "c86dc437", "name": "test1"}], "pagination": { "nextCursor": null, "totalItems": 2}}`)) |
| 46 | + })), |
| 47 | + args: []string{"run", "./...", "-T", "test", "--url"}, |
| 48 | + expected: "WARNING -", |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + for _, test := range tests { |
| 53 | + t.Run(test.name, func(t *testing.T) { |
| 54 | + defer test.server.Close() |
| 55 | + |
| 56 | + cmd := exec.Command("go", append(test.args, test.server.URL)...) |
| 57 | + out, _ := cmd.CombinedOutput() |
| 58 | + |
| 59 | + actual := string(out) |
| 60 | + |
| 61 | + if !strings.Contains(actual, test.expected) { |
| 62 | + t.Error("\nActual: ", actual, "\nExpected: ", test.expected) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
0 commit comments