forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
59 lines (52 loc) · 1.29 KB
/
middleware_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
package middleware
import (
"context"
"fmt"
"reflect"
"testing"
)
var i int
func TestChain(t *testing.T) {
next := func(ctx context.Context, req interface{}) (interface{}, error) {
t.Log(req)
i += 10
return "reply", nil
}
got, err := Chain(test1Middleware, test2Middleware, test3Middleware)(next)(context.Background(), "hello kratos!")
if err != nil {
t.Errorf("expect %v, got %v", nil, err)
}
if !reflect.DeepEqual(got, "reply") {
t.Errorf("expect %v, got %v", "reply", got)
}
if !reflect.DeepEqual(i, 16) {
t.Errorf("expect %v, got %v", 16, i)
}
}
func test1Middleware(handler Handler) Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
fmt.Println("test1 before")
i++
reply, err = handler(ctx, req)
fmt.Println("test1 after")
return
}
}
func test2Middleware(handler Handler) Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
fmt.Println("test2 before")
i += 2
reply, err = handler(ctx, req)
fmt.Println("test2 after")
return
}
}
func test3Middleware(handler Handler) Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
fmt.Println("test3 before")
i += 3
reply, err = handler(ctx, req)
fmt.Println("test3 after")
return
}
}