-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheall_test.go
125 lines (110 loc) · 3.01 KB
/
cacheall_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package espresso_test
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"github.com/googollee/module"
"github.com/googollee/go-espresso"
)
func TestCacheAllMiddleware(t *testing.T) {
tests := []struct {
name string
providers []module.Provider
middlewares []espresso.HandleFunc
wantCode int
wantBody string
}{
{
name: "MiddlewareError",
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
return errors.New("error")
}},
wantCode: http.StatusInternalServerError,
wantBody: "error",
},
{
name: "MiddlewareHTTPError",
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
return espresso.Error(http.StatusGatewayTimeout, errors.New("gateway timeout"))
}},
wantCode: http.StatusGatewayTimeout,
wantBody: "gateway timeout",
},
{
name: "MiddlewarePanic",
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
panic("panic")
}},
wantCode: http.StatusInternalServerError,
wantBody: "panic",
},
{
name: "MiddlewareErrorWithCodec",
providers: []module.Provider{espresso.ProvideCodecs},
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
return errors.New("error")
}},
wantCode: http.StatusInternalServerError,
wantBody: "{\"message\":\"error\"}\n",
},
{
name: "MiddlewareHTTPErrorWithCodec",
providers: []module.Provider{espresso.ProvideCodecs},
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
return espresso.Error(http.StatusGatewayTimeout, errors.New("gateway timeout"))
}},
wantCode: http.StatusGatewayTimeout,
wantBody: "{\"message\":\"gateway timeout\"}\n",
},
{
name: "MiddlewarePanicWithCodec",
providers: []module.Provider{espresso.ProvideCodecs},
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
panic("panic")
}},
wantCode: http.StatusInternalServerError,
wantBody: "{\"message\":\"panic\"}\n",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
espo := espresso.New()
espo.Use(tc.middlewares...)
espo.AddModule(tc.providers...)
var called int32
espo.HandleFunc(func(ctx espresso.Context) error {
atomic.AddInt32(&called, 1)
if err := ctx.Endpoint(http.MethodGet, "/").End(); err != nil {
return err
}
fmt.Fprint(ctx.ResponseWriter(), "ok")
return nil
})
called = 0
svr := httptest.NewServer(espo)
defer svr.Close()
resp, err := http.Get(svr.URL)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if got := atomic.LoadInt32(&called); got != 0 {
t.Fatalf("handle func is called")
}
if got, want := resp.StatusCode, tc.wantCode; got != want {
t.Fatalf("resp.Status = %d, want: %d", got, want)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if got, want := string(respBody), tc.wantBody; got != want {
t.Errorf("resp.Body = %q, want: %q", got, want)
}
})
}
}