-
Notifications
You must be signed in to change notification settings - Fork 5
/
middleware_test.go
207 lines (171 loc) · 6.08 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package grpcutil
import (
"context"
"errors"
"io"
"net"
"testing"
"github.com/authzed/grpcutil/internal/testpb"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
)
func TestSplitMethodName(t *testing.T) {
serviceName, methodName := SplitMethodName("/authzed.api.v0.ACLService/Check")
if serviceName != "authzed.api.v0.ACLService" || methodName != "Check" {
t.Errorf("expected 'authzed.api.v0.ACLService' 'Check' , got %s %s", serviceName, methodName)
}
}
func TestWrapMethodsNoop(t *testing.T) {
lis := bufconn.Listen(1024 * 1024)
s := grpc.NewServer()
s.RegisterService(WrapMethods(testpb.HelloService_ServiceDesc, NoopUnaryInterceptor), &testServer{})
go func() {
_ = s.Serve(lis)
}()
conn, err := grpc.Dial("", grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)
client := testpb.NewHelloServiceClient(conn)
resp, err := client.HelloUnary(context.Background(), &testpb.HelloRequest{Message: "hi"})
require.NoError(t, err)
require.Equal(t, "hi", resp.Message)
}
func TestWrapMethods(t *testing.T) {
lis := bufconn.Listen(1024 * 1024)
s := grpc.NewServer()
middleware := func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
sr := req.(*testpb.HelloRequest)
sr.Message = "yep"
return handler(ctx, req)
}
s.RegisterService(WrapMethods(testpb.HelloService_ServiceDesc, middleware), &testServer{})
go func() {
_ = s.Serve(lis)
}()
conn, err := grpc.Dial("", grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)
client := testpb.NewHelloServiceClient(conn)
resp, err := client.HelloUnary(context.Background(), &testpb.HelloRequest{Message: "hi"})
require.NoError(t, err)
require.Equal(t, "yep", resp.Message, "request not intercepted")
}
func TestWrapMethodsAndServerInterceptor(t *testing.T) {
lis := bufconn.Listen(1024 * 1024)
serverMiddleware := func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
r, err := handler(ctx, req)
sr := r.(*testpb.HelloResponse)
sr.Message += ",friend"
return sr, err
}
s := grpc.NewServer(grpc.ChainUnaryInterceptor(serverMiddleware))
middleware := func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
r, err := handler(ctx, req)
sr := r.(*testpb.HelloResponse)
sr.Message += ",sup"
return sr, err
}
s.RegisterService(WrapMethods(testpb.HelloService_ServiceDesc, middleware), &testServer{})
go func() {
_ = s.Serve(lis)
}()
conn, err := grpc.Dial("", grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)
client := testpb.NewHelloServiceClient(conn)
resp, err := client.HelloUnary(context.Background(), &testpb.HelloRequest{Message: "hi"})
require.NoError(t, err)
// middleware happens before server middleware
require.Equal(t, "yep,sup,friend", resp.Message, "request not intercepted, got %s", resp.Message)
}
func TestWrapStreams(t *testing.T) {
lis := bufconn.Listen(1024 * 1024)
s := grpc.NewServer()
counter := 0
s.RegisterService(WrapStreams(testpb.HelloService_ServiceDesc, StreamMiddleware(&counter)), &testServer{})
go func() {
_ = s.Serve(lis)
}()
conn, err := grpc.Dial("", grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)
client := testpb.NewHelloServiceClient(conn)
stream, err := client.HelloStreaming(context.Background(), &testpb.HelloRequest{Message: "hi"})
require.NoError(t, err)
err = func() error {
for {
_, err := stream.Recv()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return err
}
}
}()
require.NoError(t, err)
require.Equal(t, 1, counter, "stream not intercepted")
}
func TestWrapStreamsAndServerInterceptor(t *testing.T) {
serverCounter := 0
lis := bufconn.Listen(1024 * 1024)
s := grpc.NewServer(grpc.ChainStreamInterceptor(StreamMiddleware(&serverCounter)))
counter := 0
s.RegisterService(WrapStreams(testpb.HelloService_ServiceDesc, StreamMiddleware(&counter)), &testServer{})
go func() {
_ = s.Serve(lis)
}()
conn, err := grpc.Dial("", grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)
client := testpb.NewHelloServiceClient(conn)
stream, err := client.HelloStreaming(context.Background(), &testpb.HelloRequest{Message: "hi"})
require.NoError(t, err)
err = func() error {
for {
_, err := stream.Recv()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return err
}
}
}()
require.NoError(t, err)
require.Equal(t, 1, counter, "service stream not intercepted")
require.Equal(t, 1, serverCounter, "server stream not intercepted")
}
func StreamMiddleware(counter *int) grpc.StreamServerInterceptor {
return func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
wrapper := &recvWrapper{stream, counter}
return handler(srv, wrapper)
}
}
type recvWrapper struct {
grpc.ServerStream
counter *int
}
func (s *recvWrapper) RecvMsg(m any) error {
if err := s.ServerStream.RecvMsg(m); err != nil {
return err
}
*s.counter++
return nil
}
type testServer struct {
testpb.UnimplementedHelloServiceServer
}
func (s *testServer) HelloUnary(_ context.Context, in *testpb.HelloRequest) (*testpb.HelloResponse, error) {
return &testpb.HelloResponse{Message: in.Message}, nil
}
func (s *testServer) HelloStreaming(args *testpb.HelloRequest, stream testpb.HelloService_HelloStreamingServer) error {
return stream.Send(&testpb.HelloResponse{Message: args.Message})
}