-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler_test.go
57 lines (41 loc) · 1.04 KB
/
handler_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
package main
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestPing(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/blaping", nil)
Ping(w, r)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}
func TestDbError(t *testing.T) {
c := new(checker)
c.On("Ping").Times(1).Return(errors.New("someerr"))
h := HealthChecker(c)
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/bla", nil)
h.ServeHTTP(w, r)
c.AssertExpectations(t)
assert.Equal(t, 503, w.Code)
assert.Equal(t, "Service Unavailable", w.Body.String())
}
func TestDbSuccess(t *testing.T) {
c := new(checker)
c.On("Ping").Return(nil)
h := HealthChecker(c)
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/bla", nil)
h.ServeHTTP(w, r)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}
type checker struct{ mock.Mock }
func (c *checker) Ping() error {
return c.Called().Error(0)
}