Files
traefik-llmgateway/passthrough_ratelimit_headers_test.go
T
lukaszraczyloandGitHub 4c61d19a72 feat(router): smart routers and per-member rate limits (#9)
Add smart routers: virtual models that score each request and route it to admin-defined model tiers. A retryable failure moves the request to a higher tier. The classifier v2 extracts prose, finds the original ask in long tool loops, adds an uncertainty margin and reads tool-output failures. Sessions, outcome signals, bounded calibration, adaptive selection within a tier, gated saturation downgrade, a first-byte timeout and an opt-in timeout fallback are included. Every new feature is off by default.

Add per-member group limits, a tokensPerMinute window that counts uncached input and output, rate-limit headers in three dialects, soft pacing before the hard limit, and a limitExemptKinds list so embedding and reranking traffic never counts toward or hits a limit.

Fix yaegi named-result state that made smartContentSize grow between calls.
2026-11-25 00:40:30 +00:00

63 lines
2.7 KiB
Go

package traefikllmgateway
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func passthroughHeaderGateway(t *testing.T, limits *LimitsConfig) http.Handler {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Ratelimit-Remaining-Requests", "999")
w.Header().Set("X-Ratelimit-Limit-Tokens", "12345")
w.Header().Set("Anthropic-Ratelimit-Requests-Remaining", "888")
w.Header().Set("Ratelimit", `"up";r=1;t=1`)
w.Header().Set("Ratelimit-Policy", `"up";q=1;w=1`)
_, _ = w.Write([]byte(`{"model":"gpt-test","usage":{"prompt_tokens":1,"completion_tokens":1}}`))
}))
t.Cleanup(srv.Close)
cfg := CreateConfig()
cfg.RateLimitHeaders = true
cfg.Providers = map[string]*ProviderConfig{"openai": {Type: providerTypeOpenAI, BaseURL: srv.URL, APIKey: "k"}}
cfg.Groups = map[string]*GroupConfig{"default": {}}
cfg.Users = &UsersConfig{Inline: []*UserConfig{{Name: "alice", Group: "default", APIKey: "sk-alice", Limits: limits}}}
h, err := New(context.Background(), http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}), cfg, "llmgw")
require.NoError(t, err)
return h
}
func passthroughCall(h http.Handler) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPost, "/openai/v1/chat/completions", strings.NewReader(`{"model":"gpt-test"}`))
req.Header.Set("Authorization", "Bearer sk-alice")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
func TestPassthrough_GatewayRateLimitHeadersWinOverUpstream(t *testing.T) {
rec := passthroughCall(passthroughHeaderGateway(t, &LimitsConfig{RequestsPerMinute: 10, TokensPerMinute: 5000}))
require.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "9", rec.Header().Get("x-ratelimit-remaining-requests"))
assert.Equal(t, "5000", rec.Header().Get("x-ratelimit-limit-tokens"))
assert.Equal(t, "9", rec.Header().Get("anthropic-ratelimit-requests-remaining"))
assert.Contains(t, rec.Header().Get("RateLimit"), `"requests";r=9`)
assert.NotContains(t, rec.Header().Get("RateLimit"), `"up"`)
assert.NotContains(t, rec.Header().Get("RateLimit-Policy"), `"up"`)
}
func TestPassthrough_UpstreamRateLimitHeadersKeptWhenGatewayEmitsNone(t *testing.T) {
rec := passthroughCall(passthroughHeaderGateway(t, &LimitsConfig{RequestsPerDay: 100}))
require.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "999", rec.Header().Get("x-ratelimit-remaining-requests"))
assert.Equal(t, "12345", rec.Header().Get("x-ratelimit-limit-tokens"))
assert.Equal(t, "888", rec.Header().Get("anthropic-ratelimit-requests-remaining"))
assert.Equal(t, `"up";r=1;t=1`, rec.Header().Get("RateLimit"))
}