Tests for pkg/utils (#3155)
Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
This commit is contained in:
@@ -22,6 +22,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
|
||||
"github.com/minio/console/pkg/utils"
|
||||
@@ -100,7 +102,7 @@ func ToEntry(w http.ResponseWriter, r *http.Request, reqClaims map[string]interf
|
||||
var requestID interface{}
|
||||
requestID = r.Context().Value(utils.ContextRequestID)
|
||||
if requestID == nil {
|
||||
requestID, _ = utils.NewUUID()
|
||||
requestID = uuid.NewString()
|
||||
}
|
||||
entry.RequestID = requestID.(string)
|
||||
|
||||
|
||||
@@ -19,19 +19,8 @@ package utils
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// NewUUID - get a random UUID.
|
||||
func NewUUID() (string, error) {
|
||||
u, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
// DecodeBase64 : decoded base64 input into utf-8 text
|
||||
func DecodeBase64(s string) (string, error) {
|
||||
decodedInput, err := base64.StdEncoding.DecodeString(s)
|
||||
|
||||
@@ -16,7 +16,10 @@
|
||||
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecodeInput(t *testing.T) {
|
||||
type args struct {
|
||||
@@ -90,3 +93,36 @@ func TestDecodeInput(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIPFromContext(t *testing.T) {
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "working return",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
},
|
||||
want: "127.0.0.1",
|
||||
},
|
||||
{
|
||||
name: "context contains ip",
|
||||
args: args{
|
||||
ctx: context.WithValue(context.Background(), ContextClientIP, "10.0.0.1"),
|
||||
},
|
||||
want: "10.0.0.1",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := ClientIPFromContext(tt.args.ctx); got != tt.want {
|
||||
t.Errorf("ClientIPFromContext() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/minio/console/pkg/logger"
|
||||
"github.com/minio/console/pkg/utils"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
@@ -192,11 +194,7 @@ func setupMiddlewares(handler http.Handler) http.Handler {
|
||||
|
||||
func ContextMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID, err := utils.NewUUID()
|
||||
if err != nil && err != auth.ErrNoAuthToken {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
requestID := uuid.NewString()
|
||||
ctx := context.WithValue(r.Context(), utils.ContextRequestID, requestID)
|
||||
ctx = context.WithValue(ctx, utils.ContextRequestUserAgent, r.UserAgent())
|
||||
ctx = context.WithValue(ctx, utils.ContextRequestHost, r.Host)
|
||||
|
||||
Reference in New Issue
Block a user