MCPcopy
hub / github.com/grpc/grpc-go / TestStaticPolicyEnd2End

Method TestStaticPolicyEnd2End

authz/grpc_authz_end2end_test.go:291–354  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

289}
290
291func (s) TestStaticPolicyEnd2End(t *testing.T) {
292 for name, test := range authzTests {
293 t.Run(name, func(t *testing.T) {
294 // Start a gRPC server with gRPC authz unary and stream server interceptors.
295 i, _ := authz.NewStatic(test.authzPolicy)
296
297 stub := &stubserver.StubServer{
298 UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
299 return &testpb.SimpleResponse{}, nil
300 },
301 StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
302 for {
303 _, err := stream.Recv()
304 if err == io.EOF {
305 return stream.SendAndClose(&testpb.StreamingInputCallResponse{})
306 }
307 if err != nil {
308 return err
309 }
310 }
311 },
312 S: grpc.NewServer(grpc.ChainUnaryInterceptor(i.UnaryInterceptor), grpc.ChainStreamInterceptor(i.StreamInterceptor)),
313 }
314 stubserver.StartTestService(t, stub)
315 defer stub.Stop()
316
317 // Establish a connection to the server.
318 cc, err := grpc.NewClient(stub.Address, grpc.WithTransportCredentials(insecure.NewCredentials()))
319 if err != nil {
320 t.Fatalf("grpc.NewClient(%v) failed: %v", stub.Address, err)
321 }
322 defer cc.Close()
323 client := testgrpc.NewTestServiceClient(cc)
324
325 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
326 defer cancel()
327 ctx = metadata.NewOutgoingContext(ctx, test.md)
328
329 // Verifying authorization decision for Unary RPC.
330 _, err = client.UnaryCall(ctx, &testpb.SimpleRequest{})
331 if got := status.Convert(err); got.Code() != test.wantStatus.Code() || got.Message() != test.wantStatus.Message() {
332 t.Fatalf("[UnaryCall] error want:{%v} got:{%v}", test.wantStatus.Err(), got.Err())
333 }
334
335 // Verifying authorization decision for Streaming RPC.
336 stream, err := client.StreamingInputCall(ctx)
337 if err != nil {
338 t.Fatalf("failed StreamingInputCall err: %v", err)
339 }
340 req := &testpb.StreamingInputCallRequest{
341 Payload: &testpb.Payload{
342 Body: []byte("hi"),
343 },
344 }
345 if err := stream.Send(req); err != nil && err != io.EOF {
346 t.Fatalf("failed stream.Send err: %v", err)
347 }
348 _, err = stream.CloseAndRecv()

Callers

nothing calls this directly

Calls 15

StopMethod · 0.95
UnaryCallMethod · 0.95
StreamingInputCallMethod · 0.95
NewStaticFunction · 0.92
NewServerFunction · 0.92
ChainUnaryInterceptorFunction · 0.92
ChainStreamInterceptorFunction · 0.92
StartTestServiceFunction · 0.92
NewClientFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
NewOutgoingContextFunction · 0.92

Tested by

no test coverage detected