|
| 1 | +// Copyright (c) The go-grpc-middleware Authors. |
| 2 | +// Licensed under the Apache License 2.0. |
| 3 | + |
| 4 | +package validator_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "io" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" |
| 11 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/validator" |
| 12 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "github.com/stretchr/testify/suite" |
| 16 | + "google.golang.org/grpc" |
| 17 | + "google.golang.org/grpc/codes" |
| 18 | + "google.golang.org/grpc/status" |
| 19 | +) |
| 20 | + |
| 21 | +type TestLogger struct{} |
| 22 | + |
| 23 | +func (l *TestLogger) Log(lvl logging.Level, msg string) {} |
| 24 | + |
| 25 | +func (l *TestLogger) With(fields ...string) logging.Logger { |
| 26 | + return &TestLogger{} |
| 27 | +} |
| 28 | + |
| 29 | +type ValidatorTestSuite struct { |
| 30 | + *testpb.InterceptorTestSuite |
| 31 | +} |
| 32 | + |
| 33 | +func (s *ValidatorTestSuite) TestValidPasses_Unary() { |
| 34 | + _, err := s.Client.Ping(s.SimpleCtx(), testpb.GoodPing) |
| 35 | + assert.NoError(s.T(), err, "no error expected") |
| 36 | +} |
| 37 | + |
| 38 | +func (s *ValidatorTestSuite) TestInvalidErrors_Unary() { |
| 39 | + _, err := s.Client.Ping(s.SimpleCtx(), testpb.BadPing) |
| 40 | + assert.Error(s.T(), err, "no error expected") |
| 41 | + assert.Equal(s.T(), codes.InvalidArgument, status.Code(err), "gRPC status must be InvalidArgument") |
| 42 | +} |
| 43 | + |
| 44 | +func (s *ValidatorTestSuite) TestValidPasses_ServerStream() { |
| 45 | + stream, err := s.Client.PingList(s.SimpleCtx(), testpb.GoodPingList) |
| 46 | + require.NoError(s.T(), err, "no error on stream establishment expected") |
| 47 | + for { |
| 48 | + _, err := stream.Recv() |
| 49 | + if err == io.EOF { |
| 50 | + break |
| 51 | + } |
| 52 | + assert.NoError(s.T(), err, "no error on messages sent occurred") |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +type ClientValidatorTestSuite struct { |
| 57 | + *testpb.InterceptorTestSuite |
| 58 | +} |
| 59 | + |
| 60 | +func (s *ClientValidatorTestSuite) TestValidPasses_Unary() { |
| 61 | + _, err := s.Client.Ping(s.SimpleCtx(), testpb.GoodPing) |
| 62 | + assert.NoError(s.T(), err, "no error expected") |
| 63 | +} |
| 64 | + |
| 65 | +func (s *ClientValidatorTestSuite) TestInvalidErrors_Unary() { |
| 66 | + _, err := s.Client.Ping(s.SimpleCtx(), testpb.BadPing) |
| 67 | + assert.Error(s.T(), err, "error expected") |
| 68 | + assert.Equal(s.T(), codes.InvalidArgument, status.Code(err), "gRPC status must be InvalidArgument") |
| 69 | +} |
| 70 | + |
| 71 | +func (s *ValidatorTestSuite) TestInvalidErrors_ServerStream() { |
| 72 | + stream, err := s.Client.PingList(s.SimpleCtx(), testpb.BadPingList) |
| 73 | + require.NoError(s.T(), err, "no error on stream establishment expected") |
| 74 | + _, err = stream.Recv() |
| 75 | + assert.Error(s.T(), err, "error should be received on first message") |
| 76 | + assert.Equal(s.T(), codes.InvalidArgument, status.Code(err), "gRPC status must be InvalidArgument") |
| 77 | +} |
| 78 | + |
| 79 | +func (s *ValidatorTestSuite) TestInvalidErrors_BidiStream() { |
| 80 | + stream, err := s.Client.PingStream(s.SimpleCtx()) |
| 81 | + require.NoError(s.T(), err, "no error on stream establishment expected") |
| 82 | + |
| 83 | + require.NoError(s.T(), stream.Send(testpb.GoodPingStream)) |
| 84 | + _, err = stream.Recv() |
| 85 | + assert.NoError(s.T(), err, "receiving a good ping should return a good pong") |
| 86 | + require.NoError(s.T(), stream.Send(testpb.GoodPingStream)) |
| 87 | + _, err = stream.Recv() |
| 88 | + assert.NoError(s.T(), err, "receiving a good ping should return a good pong") |
| 89 | + |
| 90 | + require.NoError(s.T(), stream.Send(testpb.BadPingStream)) |
| 91 | + _, err = stream.Recv() |
| 92 | + assert.Error(s.T(), err, "receiving a bad ping should return a bad pong") |
| 93 | + assert.Equal(s.T(), codes.InvalidArgument, status.Code(err), "gRPC status must be InvalidArgument") |
| 94 | + |
| 95 | + err = stream.CloseSend() |
| 96 | + assert.NoError(s.T(), err, "there should be no error closing the stream on send") |
| 97 | +} |
| 98 | + |
| 99 | +func TestValidatorTestSuite(t *testing.T) { |
| 100 | + sWithNoArgs := &ValidatorTestSuite{ |
| 101 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 102 | + ServerOpts: []grpc.ServerOption{ |
| 103 | + grpc.StreamInterceptor(validator.StreamServerInterceptor()), |
| 104 | + grpc.UnaryInterceptor(validator.UnaryServerInterceptor()), |
| 105 | + }, |
| 106 | + }, |
| 107 | + } |
| 108 | + suite.Run(t, sWithNoArgs) |
| 109 | + |
| 110 | + sWithWithFailFastArgs := &ValidatorTestSuite{ |
| 111 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 112 | + ServerOpts: []grpc.ServerOption{ |
| 113 | + grpc.StreamInterceptor(validator.StreamServerInterceptor(validator.WithFailFast())), |
| 114 | + grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithFailFast())), |
| 115 | + }, |
| 116 | + }, |
| 117 | + } |
| 118 | + suite.Run(t, sWithWithFailFastArgs) |
| 119 | + |
| 120 | + sWithWithLoggerArgs := &ValidatorTestSuite{ |
| 121 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 122 | + ServerOpts: []grpc.ServerOption{ |
| 123 | + grpc.StreamInterceptor(validator.StreamServerInterceptor(validator.WithLogger(logging.DEBUG, &TestLogger{}))), |
| 124 | + grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithLogger(logging.DEBUG, &TestLogger{}))), |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | + suite.Run(t, sWithWithLoggerArgs) |
| 129 | + |
| 130 | + sAll := &ValidatorTestSuite{ |
| 131 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 132 | + ServerOpts: []grpc.ServerOption{ |
| 133 | + grpc.StreamInterceptor(validator.StreamServerInterceptor(validator.WithFailFast(), validator.WithLogger(logging.DEBUG, &TestLogger{}))), |
| 134 | + grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithFailFast(), validator.WithLogger(logging.DEBUG, &TestLogger{}))), |
| 135 | + }, |
| 136 | + }, |
| 137 | + } |
| 138 | + suite.Run(t, sAll) |
| 139 | + |
| 140 | + csWithNoArgs := &ClientValidatorTestSuite{ |
| 141 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 142 | + ClientOpts: []grpc.DialOption{ |
| 143 | + grpc.WithUnaryInterceptor(validator.UnaryClientInterceptor()), |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | + suite.Run(t, csWithNoArgs) |
| 148 | + |
| 149 | + csWithWithFailFastArgs := &ClientValidatorTestSuite{ |
| 150 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 151 | + ServerOpts: []grpc.ServerOption{ |
| 152 | + grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithFailFast())), |
| 153 | + }, |
| 154 | + }, |
| 155 | + } |
| 156 | + suite.Run(t, csWithWithFailFastArgs) |
| 157 | + |
| 158 | + csWithWithLoggerArgs := &ClientValidatorTestSuite{ |
| 159 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 160 | + ServerOpts: []grpc.ServerOption{ |
| 161 | + grpc.UnaryInterceptor(validator.UnaryServerInterceptor(validator.WithLogger(logging.DEBUG, &TestLogger{}))), |
| 162 | + }, |
| 163 | + }, |
| 164 | + } |
| 165 | + suite.Run(t, csWithWithLoggerArgs) |
| 166 | + |
| 167 | + csAll := &ClientValidatorTestSuite{ |
| 168 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 169 | + ClientOpts: []grpc.DialOption{ |
| 170 | + grpc.WithUnaryInterceptor(validator.UnaryClientInterceptor(validator.WithFailFast())), |
| 171 | + }, |
| 172 | + }, |
| 173 | + } |
| 174 | + suite.Run(t, csAll) |
| 175 | +} |
0 commit comments