42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type connectionTransport func(*http.Request) (*http.Response, error)
|
|
|
|
func (f connectionTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
|
func TestConnectionDiscoveryHTTPStatus(t *testing.T) {
|
|
for _, code := range []int{401, 403, 429, 500} {
|
|
for _, name := range []string{"gemini", "ollama"} {
|
|
t.Run(fmt.Sprintf("%s/%d", name, code), func(t *testing.T) {
|
|
client := &http.Client{Transport: connectionTransport(func(*http.Request) (*http.Response, error) {
|
|
return &http.Response{StatusCode: code, Body: io.NopCloser(strings.NewReader(`{"secret":"private response"}`)), Header: make(http.Header)}, nil
|
|
})}
|
|
var p Provider
|
|
if name == "gemini" {
|
|
g := newGemini("secret", "")
|
|
g.client = client
|
|
p = g
|
|
} else {
|
|
o := newOllama("https://private.invalid", "")
|
|
o.client = client
|
|
p = o
|
|
}
|
|
_, err := p.ListModels(context.Background())
|
|
var status *summaryHTTPError
|
|
if !errors.As(err, &status) || status.status != code || strings.Contains(err.Error(), "private") {
|
|
t.Fatalf("unsafe or unclassified error: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|