@@ -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
@@ -41,8 +44,11 @@ func TestUnaryServerInterceptor(t *testing.T) {
41
44
42
45
t .Run ("invalid_email" , func (t * testing.T ) {
43
46
_ , err = interceptor (context .TODO (), testvalidate .BadUnaryRequest , info , handler )
44
- assert .Error (t , err )
45
- assert .Equal (t , codes .InvalidArgument , status .Code (err ))
47
+ assertEqualViolation (t , & validate.Violation {
48
+ FieldPath : "message" ,
49
+ ConstraintId : "string.email" ,
50
+ Message : "value must be a valid email address" ,
51
+ }, err )
46
52
})
47
53
48
54
t .Run ("not_protobuf" , func (t * testing.T ) {
@@ -140,8 +146,11 @@ func TestStreamServerInterceptor(t *testing.T) {
140
146
assert .Nil (t , err )
141
147
142
148
_ , err = out .Recv ()
143
- assert .Error (t , err )
144
- 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 )
145
154
})
146
155
147
156
t .Run ("invalid_email_ignored" , func (t * testing.T ) {
@@ -156,3 +165,19 @@ func TestStreamServerInterceptor(t *testing.T) {
156
165
assert .Nil (t , err )
157
166
})
158
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