github.com/openfga/openfga@v1.5.4-rc1/pkg/middleware/validator/validator_test.go (about)

     1  package validator
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
     8  	"github.com/stretchr/testify/require"
     9  	"github.com/stretchr/testify/suite"
    10  	"google.golang.org/grpc"
    11  )
    12  
    13  type pingService struct {
    14  	testpb.TestServiceServer
    15  	T *testing.T
    16  }
    17  
    18  func (s *pingService) Ping(ctx context.Context, req *testpb.PingRequest) (*testpb.PingResponse, error) {
    19  	require.True(s.T, RequestIsValidatedFromContext(ctx))
    20  	return s.TestServiceServer.Ping(ctx, req)
    21  }
    22  
    23  func (s *pingService) PingStream(ss testpb.TestService_PingStreamServer) error {
    24  	require.True(s.T, RequestIsValidatedFromContext(ss.Context()))
    25  	return s.TestServiceServer.PingStream(ss)
    26  }
    27  
    28  func TestValidator(t *testing.T) {
    29  	s := &ValidatorTestSuite{
    30  		InterceptorTestSuite: &testpb.InterceptorTestSuite{
    31  			TestService: &pingService{&testpb.TestPingService{}, t},
    32  			ServerOpts: []grpc.ServerOption{
    33  				grpc.UnaryInterceptor(UnaryServerInterceptor()),
    34  				grpc.StreamInterceptor(StreamServerInterceptor()),
    35  			},
    36  		},
    37  	}
    38  
    39  	suite.Run(t, s)
    40  }
    41  
    42  type ValidatorTestSuite struct {
    43  	*testpb.InterceptorTestSuite
    44  }
    45  
    46  func (s *ValidatorTestSuite) TestPing() {
    47  	_, err := s.Client.Ping(s.SimpleCtx(), &testpb.PingRequest{Value: "ping"})
    48  	s.Require().NoError(err)
    49  }
    50  
    51  func (s *ValidatorTestSuite) TestStreamingPing() {
    52  	_, err := s.Client.PingStream(s.SimpleCtx())
    53  	s.Require().NoError(err)
    54  }