-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathendpoint_test.go
107 lines (101 loc) · 2.25 KB
/
endpoint_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
101
102
103
104
105
106
107
package detective
import (
"errors"
dm "github.com/sohamkamani/detective/mock"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)
func TestEndpoint(t *testing.T) {
tests := []struct {
name string
httpStatus int
httpError error
jsonResponse string
expectedState State
}{
{
name: "success",
httpStatus: http.StatusOK,
jsonResponse: `{
"name":"sample",
"active": true,
"status":"Ok",
"dependencies":[
{
"name":"dep1",
"active": true,
"status":"Ok"
}
]
}`,
expectedState: State{
Name: "sample",
Ok: true,
Status: "Ok",
Dependencies: []State{
{
Name: "dep1",
Ok: true,
Status: "Ok",
},
},
},
},
{
name: "http error status",
httpStatus: http.StatusInternalServerError,
expectedState: State{
Name: "sample",
Ok: false,
Status: "Error: service sample returned http status: 500 Internal Server Error",
},
},
{
name: "empty body",
httpStatus: http.StatusOK,
expectedState: State{
Name: "sample",
Ok: false,
Status: "Error: service sample returned no response body",
},
},
{
name: "random response",
httpStatus: http.StatusOK,
jsonResponse: `{
"some":"random",
"response": 0
}`,
expectedState: State{Name: "", Ok: false, Status: ""},
},
{
name: "incorrect json",
httpStatus: http.StatusOK,
jsonResponse: `{
"some":"random"`,
expectedState: State{Name: "sample", Ok: false, Status: "Error: unexpected EOF"},
},
{
name: "http failure",
httpError: errors.New("failed"),
expectedState: State{Name: "sample", Ok: false, Status: "Error: failed"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := &dm.MockClient{}
mockClient.On("Do", mock.Anything).Return(dm.MockJSONResponse(tt.jsonResponse, tt.httpStatus), tt.httpError)
req, err := http.NewRequest(http.MethodGet, "http://mock.com/", nil)
require.NoError(t, err)
e := &endpoint{
name: "sample",
client: mockClient,
req: *req,
}
s := e.getState("")
assertStatesEqual(t, tt.expectedState, s)
})
}
}