-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_test.go
68 lines (50 loc) · 1.89 KB
/
app_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
package summer
import (
"context"
"errors"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"
)
func TestApp(t *testing.T) {
bad := true
a := Basic(WithReadinessCascade(1), WithConcurrency(1))
a.Component("test-1").Check(func(ctx context.Context) (err error) {
if bad {
return errors.New("test-failed")
} else {
return
}
})
a.HandleFunc("/test", func(ctx Context) {
ctx.Text("OK")
})
rw, req := httptest.NewRecorder(), httptest.NewRequest("GET", "https://exmaple.com/debug/alive", nil)
a.ServeHTTP(rw, req)
require.Equal(t, http.StatusOK, rw.Code)
require.Equal(t, "OK", rw.Body.String())
rw, req = httptest.NewRecorder(), httptest.NewRequest("GET", "https://exmaple.com/debug/ready", nil)
a.ServeHTTP(rw, req)
require.Equal(t, http.StatusInternalServerError, rw.Code)
require.Equal(t, "test-1: test-failed", rw.Body.String())
rw, req = httptest.NewRecorder(), httptest.NewRequest("GET", "https://exmaple.com/debug/ready", nil)
a.ServeHTTP(rw, req)
require.Equal(t, http.StatusInternalServerError, rw.Code)
require.Equal(t, "test-1: test-failed", rw.Body.String())
rw, req = httptest.NewRecorder(), httptest.NewRequest("GET", "https://exmaple.com/debug/alive", nil)
a.ServeHTTP(rw, req)
require.Equal(t, http.StatusInternalServerError, rw.Code)
require.Equal(t, "CASCADED", rw.Body.String())
bad = false
rw, req = httptest.NewRecorder(), httptest.NewRequest("GET", "https://exmaple.com/debug/ready", nil)
a.ServeHTTP(rw, req)
require.Equal(t, http.StatusOK, rw.Code)
require.Equal(t, "test-1: OK", rw.Body.String())
rw, req = httptest.NewRecorder(), httptest.NewRequest("GET", "https://exmaple.com/debug/alive", nil)
a.ServeHTTP(rw, req)
require.Equal(t, http.StatusOK, rw.Code)
require.Equal(t, "OK", rw.Body.String())
rw, req = httptest.NewRecorder(), httptest.NewRequest("GET", "https://exmaple.com/test", nil)
a.ServeHTTP(rw, req)
}