43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package models
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type captureRoundTripper struct {
|
|
authorization string
|
|
}
|
|
|
|
func (t *captureRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
|
|
t.authorization = request.Header.Get("Authorization")
|
|
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(`{"data":[]}`)), Header: make(http.Header), Request: request}, nil
|
|
}
|
|
|
|
func TestOpenAICompatibleLocalAdapterUsesResolvedAPIKey(t *testing.T) {
|
|
transport := &captureRoundTripper{}
|
|
adapter, err := NewOpenAICompatibleLocalAdapterWithAPIKey("http://gateway/v1", "resolved-key", transport)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = adapter.Discover(context.Background())
|
|
if transport.authorization != "Bearer resolved-key" {
|
|
t.Fatalf("unexpected authorization header: %q", transport.authorization)
|
|
}
|
|
}
|
|
|
|
func TestOpenAICompatibleLocalAdapterDoesNotSendAuthorizationWithoutKey(t *testing.T) {
|
|
transport := &captureRoundTripper{}
|
|
adapter, err := NewOpenAICompatibleLocalAdapter("http://gateway/v1", transport)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = adapter.Discover(context.Background())
|
|
if transport.authorization != "" {
|
|
t.Fatalf("unexpected authorization header")
|
|
}
|
|
}
|