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

     1  package requestid
     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  var pingReq = &testpb.PingRequest{Value: "ping"}
    14  
    15  type pingService struct {
    16  	testpb.TestServiceServer
    17  	T *testing.T
    18  }
    19  
    20  func (s *pingService) Ping(ctx context.Context, req *testpb.PingRequest) (*testpb.PingResponse, error) {
    21  	_, ok := FromContext(ctx)
    22  	require.True(s.T, ok)
    23  
    24  	return s.TestServiceServer.Ping(ctx, req)
    25  }
    26  
    27  func (s *pingService) PingStream(ss testpb.TestService_PingStreamServer) error {
    28  	_, ok := FromContext(ss.Context())
    29  	require.True(s.T, ok)
    30  
    31  	return s.TestServiceServer.PingStream(ss)
    32  }
    33  
    34  func TestRequestIDTestSuite(t *testing.T) {
    35  	s := &RequestIDTestSuite{
    36  		InterceptorTestSuite: &testpb.InterceptorTestSuite{
    37  			TestService: &pingService{&testpb.TestPingService{}, t},
    38  			ServerOpts: []grpc.ServerOption{
    39  				grpc.UnaryInterceptor(NewUnaryInterceptor()),
    40  				grpc.StreamInterceptor(NewStreamingInterceptor()),
    41  			},
    42  		},
    43  	}
    44  
    45  	suite.Run(t, s)
    46  }
    47  
    48  type RequestIDTestSuite struct {
    49  	*testpb.InterceptorTestSuite
    50  }
    51  
    52  func (s *RequestIDTestSuite) TestPing() {
    53  	_, err := s.Client.Ping(s.SimpleCtx(), pingReq)
    54  	s.Require().NoError(err)
    55  }
    56  
    57  func (s *RequestIDTestSuite) TestStreamingPing() {
    58  	_, err := s.Client.PingStream(s.SimpleCtx())
    59  	s.Require().NoError(err)
    60  }