Skip to content

Commit f7ae2ce

Browse files
committed
protovalidate: assert whether server is called
For completeness, the tests should assert whether the server implementation is actually called or not. Signed-off-by: Akshay Shah <[email protected]>
1 parent 77de03b commit f7ae2ce

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

interceptors/protovalidate/protovalidate_test.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,15 @@ func TestUnaryServerInterceptor(t *testing.T) {
7070

7171
type server struct {
7272
testvalidatev1.UnimplementedTestValidateServiceServer
73+
74+
called *bool
7375
}
7476

7577
func (g *server) SendStream(
7678
_ *testvalidatev1.SendStreamRequest,
7779
stream testvalidatev1.TestValidateService_SendStreamServer,
7880
) error {
81+
*g.called = true
7982
if err := stream.Send(&testvalidatev1.SendStreamResponse{}); err != nil {
8083
return err
8184
}
@@ -85,7 +88,7 @@ func (g *server) SendStream(
8588

8689
const bufSize = 1024 * 1024
8790

88-
func startGrpcServer(t *testing.T, ignoreMessages ...protoreflect.MessageType) *grpc.ClientConn {
91+
func startGrpcServer(t *testing.T, called *bool, ignoreMessages ...protoreflect.MessageType) *grpc.ClientConn {
8992
lis := bufconn.Listen(bufSize)
9093

9194
validator, err := protovalidate.New()
@@ -98,7 +101,7 @@ func startGrpcServer(t *testing.T, ignoreMessages ...protoreflect.MessageType) *
98101
),
99102
),
100103
)
101-
testvalidatev1.RegisterTestValidateServiceServer(s, &server{})
104+
testvalidatev1.RegisterTestValidateServiceServer(s, &server{called: called})
102105
go func() {
103106
if err = s.Serve(lis); err != nil {
104107
log.Fatalf("Server exited with error: %v", err)
@@ -129,17 +132,24 @@ func startGrpcServer(t *testing.T, ignoreMessages ...protoreflect.MessageType) *
129132

130133
func TestStreamServerInterceptor(t *testing.T) {
131134
t.Run("valid_email", func(t *testing.T) {
135+
called := proto.Bool(false)
132136
client := testvalidatev1.NewTestValidateServiceClient(
133-
startGrpcServer(t),
137+
startGrpcServer(t, called),
134138
)
135139

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)
137145
assert.Nil(t, err)
146+
assert.True(t, *called)
138147
})
139148

140149
t.Run("invalid_email", func(t *testing.T) {
150+
called := proto.Bool(false)
141151
client := testvalidatev1.NewTestValidateServiceClient(
142-
startGrpcServer(t),
152+
startGrpcServer(t, called),
143153
)
144154

145155
out, err := client.SendStream(context.Background(), testvalidate.BadStreamRequest)
@@ -151,18 +161,21 @@ func TestStreamServerInterceptor(t *testing.T) {
151161
ConstraintId: "string.email",
152162
Message: "value must be a valid email address",
153163
}, err)
164+
assert.False(t, *called)
154165
})
155166

156167
t.Run("invalid_email_ignored", func(t *testing.T) {
168+
called := proto.Bool(false)
157169
client := testvalidatev1.NewTestValidateServiceClient(
158-
startGrpcServer(t, testvalidate.BadStreamRequest.ProtoReflect().Type()),
170+
startGrpcServer(t, called, testvalidate.BadStreamRequest.ProtoReflect().Type()),
159171
)
160172

161173
out, err := client.SendStream(context.Background(), testvalidate.BadStreamRequest)
162174
assert.Nil(t, err)
163175

164176
_, err = out.Recv()
165177
assert.Nil(t, err)
178+
assert.True(t, *called)
166179
})
167180
}
168181

0 commit comments

Comments
 (0)