@@ -9,16 +9,19 @@ import (
9
9
"net"
10
10
"testing"
11
11
12
+ "buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
12
13
"github.com/bufbuild/protovalidate-go"
13
14
protovalidate_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
14
15
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate"
15
16
testvalidatev1 "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate/v1"
16
17
"github.com/stretchr/testify/assert"
18
+ "github.com/stretchr/testify/require"
17
19
"google.golang.org/grpc"
18
20
"google.golang.org/grpc/codes"
19
21
"google.golang.org/grpc/credentials/insecure"
20
22
"google.golang.org/grpc/status"
21
23
"google.golang.org/grpc/test/bufconn"
24
+ "google.golang.org/protobuf/proto"
22
25
"google.golang.org/protobuf/reflect/protoreflect"
23
26
)
24
27
@@ -31,36 +34,34 @@ func TestUnaryServerInterceptor(t *testing.T) {
31
34
handler := func (ctx context.Context , req any ) (any , error ) {
32
35
return "good" , nil
33
36
}
37
+ info := & grpc.UnaryServerInfo {FullMethod : "FakeMethod" }
34
38
35
39
t .Run ("valid_email" , func (t * testing.T ) {
36
- info := & grpc.UnaryServerInfo {
37
- FullMethod : "FakeMethod" ,
38
- }
39
-
40
40
resp , err := interceptor (context .TODO (), testvalidate .GoodUnaryRequest , info , handler )
41
41
assert .Nil (t , err )
42
42
assert .Equal (t , resp , "good" )
43
43
})
44
44
45
45
t .Run ("invalid_email" , func (t * testing.T ) {
46
- info := & grpc.UnaryServerInfo {
47
- FullMethod : "FakeMethod" ,
48
- }
49
-
50
46
_ , err = interceptor (context .TODO (), testvalidate .BadUnaryRequest , info , handler )
47
+ assertEqualViolation (t , & validate.Violation {
48
+ FieldPath : "message" ,
49
+ ConstraintId : "string.email" ,
50
+ Message : "value must be a valid email address" ,
51
+ }, err )
52
+ })
53
+
54
+ t .Run ("not_protobuf" , func (t * testing.T ) {
55
+ _ , err = interceptor (context .Background (), "not protobuf" , info , handler )
51
56
assert .Error (t , err )
52
- assert .Equal (t , codes .InvalidArgument , status .Code (err ))
57
+ assert .Equal (t , codes .Unknown , status .Code (err ))
53
58
})
54
59
55
60
interceptor = protovalidate_middleware .UnaryServerInterceptor (validator ,
56
61
protovalidate_middleware .WithIgnoreMessages (testvalidate .BadUnaryRequest .ProtoReflect ().Type ()),
57
62
)
58
63
59
64
t .Run ("invalid_email_ignored" , func (t * testing.T ) {
60
- info := & grpc.UnaryServerInfo {
61
- FullMethod : "FakeMethod" ,
62
- }
63
-
64
65
resp , err := interceptor (context .TODO (), testvalidate .BadUnaryRequest , info , handler )
65
66
assert .Nil (t , err )
66
67
assert .Equal (t , resp , "good" )
@@ -145,8 +146,11 @@ func TestStreamServerInterceptor(t *testing.T) {
145
146
assert .Nil (t , err )
146
147
147
148
_ , err = out .Recv ()
148
- assert .Error (t , err )
149
- assert .Equal (t , codes .InvalidArgument , status .Code (err ))
149
+ assertEqualViolation (t , & validate.Violation {
150
+ FieldPath : "message" ,
151
+ ConstraintId : "string.email" ,
152
+ Message : "value must be a valid email address" ,
153
+ }, err )
150
154
})
151
155
152
156
t .Run ("invalid_email_ignored" , func (t * testing.T ) {
@@ -161,3 +165,19 @@ func TestStreamServerInterceptor(t *testing.T) {
161
165
assert .Nil (t , err )
162
166
})
163
167
}
168
+
169
+ func assertEqualViolation (tb testing.TB , want * validate.Violation , got error ) bool {
170
+ require .Error (tb , got )
171
+ st := status .Convert (got )
172
+ assert .Equal (tb , codes .InvalidArgument , st .Code ())
173
+ details := st .Proto ().GetDetails ()
174
+ require .Len (tb , details , 1 )
175
+ gotpb , unwrapErr := details [0 ].UnmarshalNew ()
176
+ require .Nil (tb , unwrapErr )
177
+ violations := & validate.Violations {
178
+ Violations : []* validate.Violation {want },
179
+ }
180
+ tb .Logf ("got: %v" , gotpb )
181
+ tb .Logf ("want: %v" , violations )
182
+ return assert .True (tb , proto .Equal (gotpb , violations ))
183
+ }
0 commit comments