@@ -70,12 +70,15 @@ func TestUnaryServerInterceptor(t *testing.T) {
70
70
71
71
type server struct {
72
72
testvalidatev1.UnimplementedTestValidateServiceServer
73
+
74
+ called * bool
73
75
}
74
76
75
77
func (g * server ) SendStream (
76
78
_ * testvalidatev1.SendStreamRequest ,
77
79
stream testvalidatev1.TestValidateService_SendStreamServer ,
78
80
) error {
81
+ * g .called = true
79
82
if err := stream .Send (& testvalidatev1.SendStreamResponse {}); err != nil {
80
83
return err
81
84
}
@@ -85,7 +88,7 @@ func (g *server) SendStream(
85
88
86
89
const bufSize = 1024 * 1024
87
90
88
- func startGrpcServer (t * testing.T , ignoreMessages ... protoreflect.MessageType ) * grpc.ClientConn {
91
+ func startGrpcServer (t * testing.T , called * bool , ignoreMessages ... protoreflect.MessageType ) * grpc.ClientConn {
89
92
lis := bufconn .Listen (bufSize )
90
93
91
94
validator , err := protovalidate .New ()
@@ -98,7 +101,7 @@ func startGrpcServer(t *testing.T, ignoreMessages ...protoreflect.MessageType) *
98
101
),
99
102
),
100
103
)
101
- testvalidatev1 .RegisterTestValidateServiceServer (s , & server {})
104
+ testvalidatev1 .RegisterTestValidateServiceServer (s , & server {called : called })
102
105
go func () {
103
106
if err = s .Serve (lis ); err != nil {
104
107
log .Fatalf ("Server exited with error: %v" , err )
@@ -129,17 +132,24 @@ func startGrpcServer(t *testing.T, ignoreMessages ...protoreflect.MessageType) *
129
132
130
133
func TestStreamServerInterceptor (t * testing.T ) {
131
134
t .Run ("valid_email" , func (t * testing.T ) {
135
+ called := proto .Bool (false )
132
136
client := testvalidatev1 .NewTestValidateServiceClient (
133
- startGrpcServer (t ),
137
+ startGrpcServer (t , called ),
134
138
)
135
139
136
- _ , err := client .SendStream (context .Background (), testvalidate .GoodStreamRequest )
140
+ out , err := client .SendStream (context .Background (), testvalidate .GoodStreamRequest )
141
+ assert .Nil (t , err )
142
+
143
+ _ , err = out .Recv ()
144
+ t .Log (err )
137
145
assert .Nil (t , err )
146
+ assert .True (t , * called )
138
147
})
139
148
140
149
t .Run ("invalid_email" , func (t * testing.T ) {
150
+ called := proto .Bool (false )
141
151
client := testvalidatev1 .NewTestValidateServiceClient (
142
- startGrpcServer (t ),
152
+ startGrpcServer (t , called ),
143
153
)
144
154
145
155
out , err := client .SendStream (context .Background (), testvalidate .BadStreamRequest )
@@ -151,18 +161,21 @@ func TestStreamServerInterceptor(t *testing.T) {
151
161
ConstraintId : "string.email" ,
152
162
Message : "value must be a valid email address" ,
153
163
}, err )
164
+ assert .False (t , * called )
154
165
})
155
166
156
167
t .Run ("invalid_email_ignored" , func (t * testing.T ) {
168
+ called := proto .Bool (false )
157
169
client := testvalidatev1 .NewTestValidateServiceClient (
158
- startGrpcServer (t , testvalidate .BadStreamRequest .ProtoReflect ().Type ()),
170
+ startGrpcServer (t , called , testvalidate .BadStreamRequest .ProtoReflect ().Type ()),
159
171
)
160
172
161
173
out , err := client .SendStream (context .Background (), testvalidate .BadStreamRequest )
162
174
assert .Nil (t , err )
163
175
164
176
_ , err = out .Recv ()
165
177
assert .Nil (t , err )
178
+ assert .True (t , * called )
166
179
})
167
180
}
168
181
0 commit comments