github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-grpc-middleware/validator/validator_test.go (about) 1 // Copyright 2016 Michal Witkowski. All Rights Reserved. 2 // See LICENSE for licensing terms. 3 4 package grpc_validator_test 5 6 import ( 7 "testing" 8 9 "github.com/hxx258456/ccgo/grpc" 10 "github.com/stretchr/testify/suite" 11 12 "io" 13 14 "github.com/hxx258456/ccgo/grpc/codes" 15 "github.com/stretchr/testify/assert" 16 "github.com/stretchr/testify/require" 17 18 grpc_testing "github.com/hxx258456/ccgo/go-grpc-middleware/testing" 19 pb_testproto "github.com/hxx258456/ccgo/go-grpc-middleware/testing/testproto" 20 grpc_validator "github.com/hxx258456/ccgo/go-grpc-middleware/validator" 21 ) 22 23 var ( 24 // See test.manual_validator.pb.go for the validator check of SleepTimeMs. 25 goodPing = &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 9999} 26 badPing = &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 10001} 27 ) 28 29 func TestValidatorTestSuite(t *testing.T) { 30 s := &ValidatorTestSuite{ 31 InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{ 32 ServerOpts: []grpc.ServerOption{ 33 grpc.StreamInterceptor(grpc_validator.StreamServerInterceptor()), 34 grpc.UnaryInterceptor(grpc_validator.UnaryServerInterceptor()), 35 }, 36 }, 37 } 38 suite.Run(t, s) 39 } 40 41 type ValidatorTestSuite struct { 42 *grpc_testing.InterceptorTestSuite 43 } 44 45 func (s *ValidatorTestSuite) TestValidPasses_Unary() { 46 _, err := s.Client.Ping(s.SimpleCtx(), goodPing) 47 assert.NoError(s.T(), err, "no error expected") 48 } 49 50 func (s *ValidatorTestSuite) TestInvalidErrors_Unary() { 51 _, err := s.Client.Ping(s.SimpleCtx(), badPing) 52 assert.Error(s.T(), err, "no error expected") 53 assert.Equal(s.T(), codes.InvalidArgument, grpc.Code(err), "gRPC status must be InvalidArgument") 54 } 55 56 func (s *ValidatorTestSuite) TestValidPasses_ServerStream() { 57 stream, err := s.Client.PingList(s.SimpleCtx(), goodPing) 58 require.NoError(s.T(), err, "no error on stream establishment expected") 59 for true { 60 _, err := stream.Recv() 61 if err == io.EOF { 62 break 63 } 64 assert.NoError(s.T(), err, "no error on messages sent occured") 65 } 66 } 67 68 func (s *ValidatorTestSuite) TestInvalidErrors_ServerStream() { 69 stream, err := s.Client.PingList(s.SimpleCtx(), badPing) 70 require.NoError(s.T(), err, "no error on stream establishment expected") 71 _, err = stream.Recv() 72 assert.Error(s.T(), err, "error should be received on first message") 73 assert.Equal(s.T(), codes.InvalidArgument, grpc.Code(err), "gRPC status must be InvalidArgument") 74 } 75 76 func (s *ValidatorTestSuite) TestInvalidErrors_BidiStream() { 77 stream, err := s.Client.PingStream(s.SimpleCtx()) 78 require.NoError(s.T(), err, "no error on stream establishment expected") 79 80 stream.Send(goodPing) 81 _, err = stream.Recv() 82 assert.NoError(s.T(), err, "receving a good ping should return a good pong") 83 stream.Send(goodPing) 84 _, err = stream.Recv() 85 assert.NoError(s.T(), err, "receving a good ping should return a good pong") 86 87 stream.Send(badPing) 88 _, err = stream.Recv() 89 assert.Error(s.T(), err, "receving a good ping should return a good pong") 90 assert.Equal(s.T(), codes.InvalidArgument, grpc.Code(err), "gRPC status must be InvalidArgument") 91 92 err = stream.CloseSend() 93 assert.NoError(s.T(), err, "there should be no error closing the stream on send") 94 }