Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/servertiming"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func TestServerTimingRedisHookRecordsCommands(t *testing.T) {
|
||||
collector := servertiming.New(time.Now())
|
||||
ctx := servertiming.WithCollector(context.Background(), collector)
|
||||
hook := serverTimingRedisHook{}
|
||||
|
||||
process := hook.ProcessHook(func(context.Context, redis.Cmder) error {
|
||||
time.Sleep(time.Millisecond)
|
||||
return errors.New("redis failure")
|
||||
})
|
||||
if err := process(ctx, redis.NewStringCmd(ctx, "get", "sensitive-key")); err == nil {
|
||||
t.Fatal("ProcessHook did not return the underlying error")
|
||||
}
|
||||
|
||||
pipeline := hook.ProcessPipelineHook(func(context.Context, []redis.Cmder) error {
|
||||
time.Sleep(time.Millisecond)
|
||||
return nil
|
||||
})
|
||||
commands := []redis.Cmder{
|
||||
redis.NewStringCmd(ctx, "get", "first-secret"),
|
||||
redis.NewStringCmd(ctx, "get", "second-secret"),
|
||||
redis.NewStatusCmd(ctx, "set", "third-secret", "value"),
|
||||
}
|
||||
if err := pipeline(ctx, commands); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
header := collector.HeaderValue(time.Now(), "bypass")
|
||||
if !strings.Contains(header, `commands=4`) {
|
||||
t.Fatalf("header %q does not report one command and a three-command pipeline", header)
|
||||
}
|
||||
if strings.Contains(header, "secret") || strings.Contains(header, "get") {
|
||||
t.Fatalf("Redis command details leaked into header: %q", header)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerTimingRedisHookSkipsInactiveContext(t *testing.T) {
|
||||
called := false
|
||||
hook := serverTimingRedisHook{}
|
||||
process := hook.ProcessHook(func(context.Context, redis.Cmder) error {
|
||||
called = true
|
||||
return nil
|
||||
})
|
||||
ctx := context.Background()
|
||||
if err := process(ctx, redis.NewStringCmd(ctx, "ping")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !called {
|
||||
t.Fatal("inactive Redis command did not reach the next hook")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user