Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(selector) add test case and example
Signed-off-by: aimuz <mr.imuz@gmail.com>
- Loading branch information
Showing
4 changed files
with
229 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package selector_test | ||
|
||
import ( | ||
"context" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/ratelimit" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
// alwaysPassLimiter is an example limiter which implements Limiter interface. | ||
// It does not limit any request because Limit function always returns false. | ||
type alwaysPassLimiter struct{} | ||
|
||
func (*alwaysPassLimiter) Limit(_ context.Context) error { | ||
return nil | ||
} | ||
|
||
func Example_ratelimit() { | ||
healthSkip := selector.Block([]string{"/ping.v1.PingService/Health"}) | ||
limiter := &alwaysPassLimiter{} | ||
_ = grpc.NewServer( | ||
grpc.ChainUnaryInterceptor( | ||
selector.UnaryServerInterceptor(ratelimit.UnaryServerInterceptor(limiter), healthSkip), | ||
), | ||
grpc.ChainStreamInterceptor( | ||
selector.StreamServerInterceptor(ratelimit.StreamServerInterceptor(limiter), healthSkip), | ||
), | ||
) | ||
} | ||
|
||
var tokenInfoKey struct{} | ||
|
||
func parseToken(token string) (struct{}, error) { | ||
return struct{}{}, nil | ||
} | ||
|
||
func userClaimFromToken(struct{}) string { | ||
return "foobar" | ||
} | ||
|
||
// exampleAuthFunc is used by a middleware to authenticate requests | ||
func exampleAuthFunc(ctx context.Context) (context.Context, error) { | ||
token, err := auth.AuthFromMD(ctx, "bearer") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tokenInfo, err := parseToken(token) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Unauthenticated, "invalid auth token: %v", err) | ||
} | ||
|
||
ctx = logging.InjectFields(ctx, logging.Fields{"auth.sub", userClaimFromToken(tokenInfo)}) | ||
|
||
// WARNING: In production define your own type to avoid context collisions. | ||
return context.WithValue(ctx, tokenInfoKey, tokenInfo), nil | ||
} | ||
|
||
func Example_login() { | ||
loginSkip := selector.Block([]string{"/auth.v1.AuthService/Login"}) | ||
_ = grpc.NewServer( | ||
grpc.ChainUnaryInterceptor( | ||
selector.UnaryServerInterceptor(auth.UnaryServerInterceptor(exampleAuthFunc), loginSkip), | ||
), | ||
grpc.ChainStreamInterceptor( | ||
selector.StreamServerInterceptor(auth.StreamServerInterceptor(exampleAuthFunc), loginSkip), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package selector | ||
|
||
import ( | ||
"context" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc" | ||
"testing" | ||
) | ||
|
||
var whiteList = []string{"/auth.v1beta1.AuthService/Login"} | ||
|
||
const errMsgFake = "fake error" | ||
|
||
var ctxKey = struct{}{} | ||
|
||
type mockGRPCServerStream struct { | ||
grpc.ServerStream | ||
|
||
ctx context.Context | ||
} | ||
|
||
func (m *mockGRPCServerStream) Context() context.Context { | ||
return m.ctx | ||
} | ||
|
||
func TestUnaryServerInterceptor(t *testing.T) { | ||
ctx := context.Background() | ||
interceptor := UnaryServerInterceptor(auth.UnaryServerInterceptor( | ||
func(ctx context.Context) (context.Context, error) { | ||
newCtx := context.WithValue(ctx, ctxKey, true) | ||
return newCtx, nil | ||
}, | ||
), Block(whiteList)) | ||
handler := func(ctx context.Context, req interface{}) (interface{}, error) { | ||
val := ctx.Value(ctxKey) | ||
if b, ok := val.(bool); ok && b { | ||
return "good", nil | ||
} | ||
return nil, errors.New(errMsgFake) | ||
} | ||
|
||
t.Run("nextStep", func(t *testing.T) { | ||
info := &grpc.UnaryServerInfo{ | ||
FullMethod: "FakeMethod", | ||
} | ||
resp, err := interceptor(ctx, nil, info, handler) | ||
assert.Nil(t, err) | ||
assert.Equal(t, resp, "good") | ||
}) | ||
|
||
t.Run("skipped", func(t *testing.T) { | ||
info := &grpc.UnaryServerInfo{ | ||
FullMethod: "/auth.v1beta1.AuthService/Login", | ||
} | ||
resp, err := interceptor(ctx, nil, info, handler) | ||
assert.Nil(t, resp) | ||
assert.EqualError(t, err, errMsgFake) | ||
}) | ||
} | ||
|
||
func TestStreamServerInterceptor(t *testing.T) { | ||
ctx := context.Background() | ||
interceptor := StreamServerInterceptor(auth.StreamServerInterceptor( | ||
func(ctx context.Context) (context.Context, error) { | ||
newCtx := context.WithValue(ctx, ctxKey, true) | ||
return newCtx, nil | ||
}, | ||
), Block(whiteList)) | ||
|
||
handler := func(srv interface{}, stream grpc.ServerStream) error { | ||
ctx := stream.Context() | ||
val := ctx.Value(ctxKey) | ||
if b, ok := val.(bool); ok && b { | ||
return nil | ||
} | ||
return errors.New(errMsgFake) | ||
} | ||
|
||
t.Run("nextStep", func(t *testing.T) { | ||
info := &grpc.StreamServerInfo{ | ||
FullMethod: "FakeMethod", | ||
} | ||
err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, info, handler) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("skipped", func(t *testing.T) { | ||
info := &grpc.StreamServerInfo{ | ||
FullMethod: "/auth.v1beta1.AuthService/Login", | ||
} | ||
err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, info, handler) | ||
assert.EqualError(t, err, errMsgFake) | ||
}) | ||
} | ||
|
||
func TestAllow(t *testing.T) { | ||
type args struct { | ||
methods []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
method string | ||
want bool | ||
}{ | ||
{ | ||
name: "false", | ||
args: args{ | ||
methods: []string{"/auth.v1beta1.AuthService/Login"}, | ||
}, | ||
method: "/testing.testpb.v1.TestService/PingList", | ||
want: false, | ||
}, | ||
{ | ||
name: "true", | ||
args: args{ | ||
methods: []string{"/auth.v1beta1.AuthService/Login"}, | ||
}, | ||
method: "/auth.v1beta1.AuthService/Login", | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
allow := Allow(tt.args.methods) | ||
want := allow(context.Background(), tt.method) | ||
assert.Equalf(t, tt.want, want, "Allow(%v)(ctx, %v)", tt.args.methods, tt.method) | ||
}) | ||
} | ||
} |