(header, validValue string)
| 99 | } |
| 100 | |
| 101 | func authInterceptor(header, validValue string) func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) { |
| 102 | return func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) { |
| 103 | md, ok := metadata.FromIncomingContext(ctx) |
| 104 | if !ok { |
| 105 | return nil, errors.New("not authorized, no metadata") |
| 106 | } |
| 107 | vals := md.Get(header) |
| 108 | if len(vals) != 1 { |
| 109 | return nil, errors.New("not authorized, no header values") |
| 110 | } |
| 111 | if vals[0] != validValue { |
| 112 | return nil, errors.New("not authorized, incorrect value") |
| 113 | } |
| 114 | return handler(ctx, req) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestAuthorizedRequest(t *testing.T) { |
| 119 | ts := client.NewTestServer( |
no test coverage detected