refactor: unify all service error codes
This commit is contained in:
@@ -2,15 +2,16 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
@@ -30,13 +31,14 @@ func TestHTTPAuthorization(t *testing.T) {
|
||||
path string
|
||||
token string
|
||||
want int
|
||||
code int32
|
||||
}{
|
||||
{name: "grpc gateway anonymous", path: "/passport.Login/Pwd", want: http.StatusNoContent},
|
||||
{name: "dynamic rpc anonymous", path: "/rpc/passport/Login/Pwd", want: http.StatusNoContent},
|
||||
{name: "rest anonymous", path: "/rest/fts/ping", want: http.StatusNoContent},
|
||||
{name: "missing token", path: "/passport.Account/Get", want: http.StatusOK},
|
||||
{name: "missing token", path: "/passport.Account/Get", want: http.StatusOK, code: int32(status.Code(errcode.ErrHeaderAuthorization))},
|
||||
{name: "valid raw token", path: "/passport.Account/Get", token: signedToken(t, time.Now(), time.Now().Add(time.Hour)), want: http.StatusNoContent},
|
||||
{name: "bearer rejected", path: "/passport.Account/Get", token: "Bearer " + signedToken(t, time.Now(), time.Now().Add(time.Hour)), want: http.StatusOK},
|
||||
{name: "bearer rejected", path: "/passport.Account/Get", token: "Bearer " + signedToken(t, time.Now(), time.Now().Add(time.Hour)), want: http.StatusOK, code: int32(status.Code(errcode.ErrTokenAuthParseFail))},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
@@ -49,7 +51,7 @@ func TestHTTPAuthorization(t *testing.T) {
|
||||
if response.Code != test.want {
|
||||
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
if test.want == http.StatusOK && !strings.Contains(response.Body.String(), `"code":16`) {
|
||||
if test.want == http.StatusOK && !strings.Contains(response.Body.String(), `"code":`+fmt.Sprint(test.code)) {
|
||||
t.Fatalf("unexpected authorization error: %s", response.Body.String())
|
||||
}
|
||||
})
|
||||
@@ -76,7 +78,7 @@ func TestGRPCAuthorization(t *testing.T) {
|
||||
if _, err := auth.unaryInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: "/passport.Login/Pwd"}, handler); err != nil {
|
||||
t.Fatalf("anonymous method failed: %v", err)
|
||||
}
|
||||
if _, err := auth.unaryInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: "/passport.Account/Get"}, handler); status.Code(err) != codes.Unauthenticated {
|
||||
if _, err := auth.unaryInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: "/passport.Account/Get"}, handler); status.Code(err) != status.Code(errcode.ErrHeaderAuthorization) {
|
||||
t.Fatalf("expected unauthenticated, got %v", err)
|
||||
}
|
||||
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", signedToken(t, time.Now(), time.Now().Add(time.Hour))))
|
||||
|
||||
Reference in New Issue
Block a user