Files
promptkit/internal/llm/vllm_authentication_test.go

52 lines
2.1 KiB
Go
Raw Normal View History

package llm
import (
"context"
"io"
"net/http"
"strings"
"testing"
"go.maximumdirect.net/promptkit/internal/domain"
)
func TestVLLMModelsAuthentication(t *testing.T) {
for _, tc := range []struct {
name, body, key string
status int
accepted *bool
unrecognized, overflow bool
}{
{name: "empty list", status: 200, body: `{"object":"list","data":[]}`, accepted: boolPtr(true)},
{name: "nonempty list", status: 200, body: `{"object":"list","data":[{"id":"model"}]}`, key: "key", accepted: boolPtr(true)},
{name: "wrong object", status: 200, body: `{"object":"model","data":[]}`, unrecognized: true},
{name: "null data", status: 200, body: `{"object":"list","data":null}`, unrecognized: true},
{name: "object data", status: 200, body: `{"object":"list","data":{}}`, unrecognized: true},
{name: "invalid framing", status: 200, body: `{"object":"list","data":[]} {}`, unrecognized: true},
{name: "overflow", status: 200, body: strings.Repeat(" ", maxAuthenticationResponseBytes+1), overflow: true},
{name: "unauthorized", status: 401, accepted: boolPtr(false)},
{name: "other status", status: 503},
} {
t.Run(tc.name, func(t *testing.T) {
var credential string
client := NewOpenAICompatibleClient(OpenAICompatibleConfig{HTTPClient: &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
credential = req.Header.Get("Authorization")
return &http.Response{StatusCode: tc.status, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(tc.body))}, nil
})}})
got, err := client.ProbeAuthentication(context.Background(), "vllm_models", "https://example.invalid/v1/models", 0, domain.ExecutionTarget{APIKey: tc.key})
if err != nil || got.Unrecognized != tc.unrecognized || got.Overflow != tc.overflow || (got.Accepted == nil) != (tc.accepted == nil) || (got.Accepted != nil && *got.Accepted != *tc.accepted) {
t.Fatalf("result=%+v err=%v", got, err)
}
want := ""
if tc.key != "" {
want = "Bearer " + tc.key
}
if credential != want {
t.Fatalf("authorization=%q", credential)
}
})
}
}
func boolPtr(value bool) *bool { return &value }