-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpcroute_test.go
103 lines (86 loc) · 2.13 KB
/
grpcroute_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
package grpcroute
import (
"context"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"testing"
)
type AOption struct {
Handlers map[string]HandleProto
}
func (option *AOption) GetHandler(key string) (HandleProto, bool) {
handler, ok := option.Handlers[key]
if ok {
return handler, true
}
return nil, false
}
func (option *AOption) SetHandler(key string, h HandleProto) {
if key == "" || h == nil {
return
}
if option.Handlers == nil {
option.Handlers = make(map[string]HandleProto)
}
option.Handlers[key] = h
}
func (option *AOption) RemoveHandler(key string) {
delete(option.Handlers, key)
}
type A struct {
}
func (a *A) Ping(ctx context.Context, req interface{}) (interface{}, error) {
if req.(string) == "ping" {
return "pone", nil
}
return nil, status.Error(codes.InvalidArgument, "bad request")
}
func TestGrpcRoute(t *testing.T) {
a := &A{}
option := &AOption{}
chain := grpc_middleware.ChainUnaryServer(GrpcRoute(option))
key := "key"
value := "val"
ctx := context.WithValue(context.TODO(), key, value)
option.SetHandler("/pkg.Service/Ping", a.Ping)
tests := []struct {
name string
req interface{}
fullMethod string
handler func(context.Context, interface{}) (interface{}, error)
wantResp interface{}
wantCode codes.Code
}{
{
name: "TestRightRequest",
req: "ping",
fullMethod: "/pkg.Service/Ping",
wantResp: "pone",
wantCode: codes.OK,
},
{
name: "TestBadRequestForError",
req: "hello",
fullMethod: "/pkg.Service/Ping",
wantResp: nil,
wantCode: codes.InvalidArgument,
},
{
name: "TestNoFound",
req: "hello",
fullMethod: "/pkg.Service/Hello",
wantResp: nil,
wantCode: codes.NotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := chain(ctx, tt.req, &grpc.UnaryServerInfo{FullMethod: tt.fullMethod}, nil)
assert.Equal(t, tt.wantCode, status.Convert(err).Code())
assert.Equal(t, tt.wantResp, resp)
})
}
}