-
Notifications
You must be signed in to change notification settings - Fork 3
/
chain_func_test.go
80 lines (63 loc) · 1.69 KB
/
chain_func_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
package chain_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/codemodus/chain/v2"
)
var (
b0 = "0"
b1 = "1"
bEnd = "_END_"
)
func TestFuncHandlerOrder(t *testing.T) {
c0 := chain.New(nestedHandler(b0), nestedHandler(b0))
c0 = c0.Append(nestedHandler(b1), nestedHandler(b1))
c0 = c0.Append(nestedHandler(b1), nestedHandler(b1))
c1 := c0.Append(nestedHandler(b1))
c0 = c0.Append(nestedHandler(b0))
cm := chain.New(nestedHandler(b0), nestedHandler(b0))
c0 = c0.Merge(cm)
c1 = c1.Merge(cm)
h0 := c0.EndFn(endHandler)
h1 := c1.EndFn(endHandler)
w, err := record(h0)
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
got := w.Body.String()
want := b0 + b0 + b1 + b1 + b1 + b1 + b0 + b0 + b0 + bEnd + b0 + b0 + b0 + b1 + b1 + b1 + b1 + b0 + b0
if got != want {
t.Fatalf("got %s, want %s", got, want)
}
w, err = record(h1)
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
got = w.Body.String()
want = b0 + b0 + b1 + b1 + b1 + b1 + b1 + b0 + b0 + bEnd + b0 + b0 + b1 + b1 + b1 + b1 + b1 + b0 + b0
if got != want {
t.Fatalf("got %s, want %s", got, want)
}
}
func nestedHandler(msg string) func(http.Handler) http.Handler {
return func(n http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(msg))
n.ServeHTTP(w, r)
_, _ = w.Write([]byte(msg))
})
}
}
func endHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(bEnd))
}
func record(h http.Handler) (*httptest.ResponseRecorder, error) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "", nil)
if err != nil {
return nil, err
}
h.ServeHTTP(w, r)
return w, nil
}