forked from grpc-ecosystem/go-grpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptors_test.go
178 lines (155 loc) · 6.27 KB
/
interceptors_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
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
package validator_test
import (
"context"
"io"
"testing"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/validator"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type ValidatorTestSuite struct {
*testpb.InterceptorTestSuite
}
func (s *ValidatorTestSuite) TestValidPasses_Unary() {
_, err := s.Client.Ping(s.SimpleCtx(), testpb.GoodPing)
assert.NoError(s.T(), err, "no error expected")
}
func (s *ValidatorTestSuite) TestInvalidErrors_Unary() {
_, err := s.Client.Ping(s.SimpleCtx(), testpb.BadPing)
assert.Error(s.T(), err, "no error expected")
assert.Equal(s.T(), codes.InvalidArgument, status.Code(err), "gRPC status must be InvalidArgument")
}
func (s *ValidatorTestSuite) TestValidPasses_ServerStream() {
stream, err := s.Client.PingList(s.SimpleCtx(), testpb.GoodPingList)
require.NoError(s.T(), err, "no error on stream establishment expected")
for {
_, err := stream.Recv()
if err == io.EOF {
break
}
assert.NoError(s.T(), err, "no error on messages sent occurred")
}
}
type ClientValidatorTestSuite struct {
*testpb.InterceptorTestSuite
}
func (s *ClientValidatorTestSuite) TestValidPasses_Unary() {
_, err := s.Client.Ping(s.SimpleCtx(), testpb.GoodPing)
assert.NoError(s.T(), err, "no error expected")
}
func (s *ClientValidatorTestSuite) TestInvalidErrors_Unary() {
_, err := s.Client.Ping(s.SimpleCtx(), testpb.BadPing)
assert.Error(s.T(), err, "error expected")
assert.Equal(s.T(), codes.InvalidArgument, status.Code(err), "gRPC status must be InvalidArgument")
}
func (s *ValidatorTestSuite) TestInvalidErrors_ServerStream() {
stream, err := s.Client.PingList(s.SimpleCtx(), testpb.BadPingList)
require.NoError(s.T(), err, "no error on stream establishment expected")
_, err = stream.Recv()
assert.Error(s.T(), err, "error should be received on first message")
assert.Equal(s.T(), codes.InvalidArgument, status.Code(err), "gRPC status must be InvalidArgument")
}
func (s *ValidatorTestSuite) TestInvalidErrors_BidiStream() {
stream, err := s.Client.PingStream(s.SimpleCtx())
require.NoError(s.T(), err, "no error on stream establishment expected")
require.NoError(s.T(), stream.Send(testpb.GoodPingStream))
_, err = stream.Recv()
assert.NoError(s.T(), err, "receiving a good ping should return a good pong")
require.NoError(s.T(), stream.Send(testpb.GoodPingStream))
_, err = stream.Recv()
assert.NoError(s.T(), err, "receiving a good ping should return a good pong")
require.NoError(s.T(), stream.Send(testpb.BadPingStream))
_, err = stream.Recv()
assert.Error(s.T(), err, "receiving a bad ping should return a bad pong")
assert.Equal(s.T(), codes.InvalidArgument, status.Code(err), "gRPC status must be InvalidArgument")
err = stream.CloseSend()
assert.NoError(s.T(), err, "there should be no error closing the stream on send")
}
func TestValidatorTestSuite(t *testing.T) {
sWithNoArgs := &ValidatorTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
ServerOpts: []grpc.ServerOption{
grpc.StreamInterceptor(validator.StreamServerInterceptor()),
grpc.UnaryInterceptor(validator.UnaryServerInterceptor()),
},
},
}
suite.Run(t, sWithNoArgs)
sWithFailFastArgs := &ValidatorTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
ServerOpts: []grpc.ServerOption{
grpc.StreamInterceptor(validator.StreamServerInterceptor(validator.WithFailFast())),
grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithFailFast())),
},
},
}
suite.Run(t, sWithFailFastArgs)
var gotErrMsgs []string
onErr := func(ctx context.Context, err error) {
gotErrMsgs = append(gotErrMsgs, err.Error())
}
sWithOnErrFuncArgs := &ValidatorTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
ServerOpts: []grpc.ServerOption{
grpc.StreamInterceptor(validator.StreamServerInterceptor(validator.WithOnValidationErrCallback(onErr))),
grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithOnValidationErrCallback(onErr))),
},
},
}
suite.Run(t, sWithOnErrFuncArgs)
require.Equal(t, []string{"cannot sleep for more than 10s", "cannot sleep for more than 10s", "cannot sleep for more than 10s"}, gotErrMsgs)
gotErrMsgs = gotErrMsgs[:0]
sAll := &ValidatorTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
ServerOpts: []grpc.ServerOption{
grpc.StreamInterceptor(validator.StreamServerInterceptor(validator.WithFailFast(), validator.WithOnValidationErrCallback(onErr))),
grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithFailFast(), validator.WithOnValidationErrCallback(onErr))),
},
},
}
suite.Run(t, sAll)
require.Equal(t, []string{"cannot sleep for more than 10s", "cannot sleep for more than 10s", "cannot sleep for more than 10s"}, gotErrMsgs)
csWithNoArgs := &ClientValidatorTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
ClientOpts: []grpc.DialOption{
grpc.WithUnaryInterceptor(validator.UnaryClientInterceptor()),
},
},
}
suite.Run(t, csWithNoArgs)
csWithFailFastArgs := &ClientValidatorTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
ServerOpts: []grpc.ServerOption{
grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithFailFast())),
},
},
}
suite.Run(t, csWithFailFastArgs)
gotErrMsgs = gotErrMsgs[:0]
csWithOnErrFuncArgs := &ClientValidatorTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
ServerOpts: []grpc.ServerOption{
grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithOnValidationErrCallback(onErr))),
},
},
}
suite.Run(t, csWithOnErrFuncArgs)
require.Equal(t, []string{"cannot sleep for more than 10s"}, gotErrMsgs)
gotErrMsgs = gotErrMsgs[:0]
csAll := &ClientValidatorTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
ClientOpts: []grpc.DialOption{
grpc.WithUnaryInterceptor(validator.UnaryClientInterceptor(validator.WithFailFast(), validator.WithOnValidationErrCallback(onErr))),
},
},
}
suite.Run(t, csAll)
require.Equal(t, []string{"cannot sleep for more than 10s"}, gotErrMsgs)
}