mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
Refusal text stays under 128 bytes, and only models the provider serves get a per-model counter. When the first 64 KiB does not settle the model, the gate reads the body up to the smaller of maxRequestBytes and the passthrough cap, inside a body-admission slot. It refuses with 402 if the model is missing, repeated, or in a malformed or unterminated object, with 413 if the object does not close within the bound, and with 400 if the body read fails. A priced model after 64 KiB passes and the body reaches the upstream unchanged. A 2xx event stream with no completion usage bills one estimated token per data event.
120 lines
3.8 KiB
Go
120 lines
3.8 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const refusalBoundBytes = 512
|
|
|
|
func (e *gateEnv) modelKeys() []string {
|
|
st := e.gw.limiter.fallback
|
|
st.mu.Lock()
|
|
defer st.mu.Unlock()
|
|
var keys []string
|
|
for k := range st.data {
|
|
if strings.HasPrefix(k, "llmgw:"+kindModel+":") {
|
|
keys = append(keys, k)
|
|
}
|
|
}
|
|
return keys
|
|
}
|
|
|
|
func (e *gateEnv) totalR402() int64 {
|
|
v, _ := e.gw.limiter.getCounter(totalScopeKind, totalScopeID, metricR402, windowDay, time.Now())
|
|
return v
|
|
}
|
|
|
|
func longModelBody(i int) string {
|
|
return `{"model":"` + strings.Repeat("m", 60_000) + fmt.Sprint(i) + `"}`
|
|
}
|
|
|
|
func testRefusalNoModelKeys(t *testing.T, key string) {
|
|
t.Helper()
|
|
e := newGateEnv(t, nil)
|
|
const n = 8
|
|
for i := 0; i < n; i++ {
|
|
rec := e.post(longModelBody(i), key)
|
|
if rec.Code != http.StatusPaymentRequired {
|
|
t.Fatalf("request %d: status = %d, want 402, body=%.200s", i, rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
if e.upstreamCalls() != 0 {
|
|
t.Errorf("upstream calls = %d, want 0", e.upstreamCalls())
|
|
}
|
|
if keys := e.modelKeys(); len(keys) != 0 {
|
|
t.Errorf("caller-named model counter keys minted: %d (first key length %d)", len(keys), len(keys[0]))
|
|
}
|
|
if got := e.totalR402(); got != n {
|
|
t.Errorf("total r402 = %d, want %d", got, n)
|
|
}
|
|
}
|
|
|
|
func TestPassthroughUnpricedRefusal_NoModelKeysForUncataloguedNames(t *testing.T) {
|
|
testRefusalNoModelKeys(t, "sk-alice")
|
|
}
|
|
|
|
func TestPassthroughUnpricedRefusal_AnonymousCaller(t *testing.T) {
|
|
testRefusalNoModelKeys(t, "")
|
|
}
|
|
|
|
func TestPassthroughUnpricedRefusal_EventLogAndBodyAreBounded(t *testing.T) {
|
|
e := newGateEnv(t, nil)
|
|
cap := captureStderrStart()
|
|
rec := e.post(longModelBody(0), "sk-alice")
|
|
logged := captureStderrStop(cap)
|
|
if rec.Code != http.StatusPaymentRequired {
|
|
t.Fatalf("status = %d, want 402", rec.Code)
|
|
}
|
|
if rec.Body.Len() > refusalBoundBytes {
|
|
t.Errorf("402 body = %d bytes, want <= %d", rec.Body.Len(), refusalBoundBytes)
|
|
}
|
|
if len(logged) > refusalBoundBytes {
|
|
t.Errorf("log output = %d bytes, want <= %d", len(logged), refusalBoundBytes)
|
|
}
|
|
events := e.gw.events.ring.snapshot(eventRingCap)
|
|
if len(events) == 0 {
|
|
t.Fatal("no refusal event recorded")
|
|
}
|
|
ev := events[0]
|
|
if ev.Kind != eventKindUnpriced || ev.Status != http.StatusPaymentRequired {
|
|
t.Fatalf("event = kind %q status %d, want unpriced/402", ev.Kind, ev.Status)
|
|
}
|
|
if len(ev.Model) > refusalBoundBytes || len(ev.Message) > refusalBoundBytes {
|
|
t.Errorf("event Model = %d bytes, Message = %d bytes, want each <= %d", len(ev.Model), len(ev.Message), refusalBoundBytes)
|
|
}
|
|
}
|
|
|
|
func TestPassthroughUnpricedRefusal_CataloguedModelKeepsModelCounter(t *testing.T) {
|
|
e := newGateEnv(t, nil)
|
|
rec := e.post(`{"model":"cat-unpriced"}`, "sk-alice")
|
|
if rec.Code != http.StatusPaymentRequired {
|
|
t.Fatalf("status = %d, want 402, body=%.200s", rec.Code, rec.Body.String())
|
|
}
|
|
if got, _ := e.gw.limiter.getCounter(kindModel, "openai/cat-unpriced", metricR402, windowDay, time.Now()); got != 1 {
|
|
t.Errorf("model r402 counter = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestUnifiedUnpricedRefusal_Unchanged(t *testing.T) {
|
|
e := newGateEnv(t, nil)
|
|
req := newUnifiedRequest(t, http.MethodPost, "/v1/chat/completions", "sk-alice",
|
|
map[string]any{"model": "cat-unpriced", "messages": []any{map[string]any{"role": "user", "content": "hi"}}})
|
|
rec := httptest.NewRecorder()
|
|
e.h.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusPaymentRequired {
|
|
t.Fatalf("status = %d, want 402, body=%.200s", rec.Code, rec.Body.String())
|
|
}
|
|
events := e.gw.events.ring.snapshot(eventRingCap)
|
|
if len(events) == 0 || events[0].Model != "openai/cat-unpriced" {
|
|
t.Fatalf("event model = %+v, want openai/cat-unpriced", events)
|
|
}
|
|
if got, _ := e.gw.limiter.getCounter(kindModel, "openai/cat-unpriced", metricR402, windowDay, time.Now()); got != 1 {
|
|
t.Errorf("model r402 counter = %d, want 1", got)
|
|
}
|
|
}
|