-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_test.go
100 lines (85 loc) · 1.81 KB
/
transport_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package verticacheckd
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
type stubCheckService struct {
resp bool
err error
}
func (c stubCheckService) HostState() (bool, error) {
return c.resp, c.err
}
func (c stubCheckService) DBHostState(db string) (bool, error) {
return c.resp, c.err
}
func TestTransport_StateHandler(t *testing.T) {
cases := []struct {
expectedStatus int
svc *stubCheckService
}{
{
http.StatusOK,
&stubCheckService{true, nil},
},
{
http.StatusInternalServerError,
&stubCheckService{false, nil},
},
{
http.StatusInternalServerError,
&stubCheckService{false, errors.New("some error")},
},
}
req, err := http.NewRequest("GET", "/state", nil)
if err != nil {
t.Fatal(err)
}
for _, c := range cases {
rr := httptest.NewRecorder()
handler := StateHandler(c.svc)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != c.expectedStatus {
t.Errorf("expected %v to be %v", status, c.expectedStatus)
}
}
}
func TestTransport_DBStateHandler(t *testing.T) {
cases := []struct {
expectedStatus int
name string
svc *stubCheckService
}{
{
http.StatusOK,
"somedb",
&stubCheckService{true, nil},
},
{
http.StatusInternalServerError,
"otherdb",
&stubCheckService{false, nil},
},
{
http.StatusInternalServerError,
"someotherdb",
&stubCheckService{false, errors.New("some error")},
},
}
for _, c := range cases {
path := fmt.Sprintf("/dbs/%s/state", c.name)
req, err := http.NewRequest("GET", path, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := DBStateHandler(c.svc)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != c.expectedStatus {
t.Errorf("expected %v to be %v", status, c.expectedStatus)
}
}
}