Files
traefik-llmgateway/provider_openai_stream_choices_test.go
T
lukaszraczyloandGitHub f1b198bce4 fix(security): audit run-4 fixes and run-3 leftovers (#26)
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
2026-11-29 10:50:36 +00:00

34 lines
1.1 KiB
Go

package traefikllmgateway
import (
"net/http/httptest"
"strings"
"testing"
)
func TestOpenAIForwardStream_MalformedChoicesKeepUsage(t *testing.T) {
tests := []struct {
name string
body string
}{
{"tool_calls object", `data: {"choices":[{"delta":{"tool_calls":{"function":{"arguments":"x"}}}}],"usage":{"prompt_tokens":7,"completion_tokens":9}}` + "\n\n"},
{"choices object", `data: {"choices":{"delta":{}},"usage":{"prompt_tokens":7,"completion_tokens":9}}` + "\n\n"},
{"choice not object", `data: {"choices":["x"],"usage":{"prompt_tokens":7,"completion_tokens":9}}` + "\n\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
u, err := (&openaiAdapter{}).forwardStream(rec, strings.NewReader(tt.body+"data: [DONE]\n\n"), false)
if err != nil {
t.Fatalf("forwardStream: %v", err)
}
if u.prompt != 7 || u.completion != 9 {
t.Fatalf("usage = %d/%d, want 7/9", u.prompt, u.completion)
}
if !strings.Contains(rec.Body.String(), `"choices"`) {
t.Errorf("chunk with choices was suppressed: %q", rec.Body.String())
}
})
}
}