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