add tests to detect bug

This commit is contained in:
matthew-hull-bright 2026-09-11 11:23:05 +01:00
parent 9a715df212
commit 94a7ce695e
2 changed files with 155 additions and 0 deletions

View file

@ -1,10 +1,67 @@
package litellm
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestSendRequestAcceptsFullSuccessRange(t *testing.T) {
tests := []struct {
name string
statusCode int
wantErr bool
wantValue string
}{
{name: "200 OK", statusCode: http.StatusOK, wantErr: false, wantValue: "ok"},
{name: "201 Created", statusCode: http.StatusCreated, wantErr: false, wantValue: "created"},
{name: "202 Accepted", statusCode: http.StatusAccepted, wantErr: false, wantValue: "accepted"},
{name: "400 Bad Request", statusCode: http.StatusBadRequest, wantErr: true},
{name: "404 Not Found", statusCode: http.StatusNotFound, wantErr: true},
{name: "500 Internal Server Error", statusCode: http.StatusInternalServerError, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tt.statusCode)
w.Write([]byte(`{"value":"` + tt.wantValue + `"}`))
}))
defer srv.Close()
client := NewClient(srv.URL, "test-key", true)
result, err := client.sendRequest("POST", "/whatever", map[string]string{"foo": "bar"})
if tt.wantErr {
if err == nil {
t.Fatalf("sendRequest returned no error for status %d", tt.statusCode)
}
var apiErr *apiError
if !errors.As(err, &apiErr) {
t.Fatalf("error is not *apiError: %v", err)
}
if apiErr.StatusCode != tt.statusCode {
t.Errorf("apiErr.StatusCode = %d, want %d", apiErr.StatusCode, tt.statusCode)
}
if tt.statusCode == http.StatusNotFound && !isNotFound(err) {
t.Errorf("isNotFound(err) = false for 404, want true")
}
return
}
if err != nil {
t.Fatalf("sendRequest returned unexpected error: %v", err)
}
if result["value"] != tt.wantValue {
t.Errorf("result[value] = %v, want %q", result["value"], tt.wantValue)
}
})
}
}
func TestRedactSensitiveDataNestedCredentialValues(t *testing.T) {
c := NewClient("http://localhost:4000", "sk-test", false)

View file

@ -0,0 +1,98 @@
package litellm
import (
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
)
func TestHandleAPIResponseAcceptsFullSuccessRange(t *testing.T) {
tests := []struct {
name string
statusCode int
wantErr bool
}{
{name: "200 OK", statusCode: http.StatusOK, wantErr: false},
{name: "201 Created", statusCode: http.StatusCreated, wantErr: false},
{name: "202 Accepted", statusCode: http.StatusAccepted, wantErr: false},
{name: "400 Bad Request", statusCode: http.StatusBadRequest, wantErr: true},
{name: "404 Not Found", statusCode: http.StatusNotFound, wantErr: true},
{name: "500 Internal Server Error", statusCode: http.StatusInternalServerError, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
rec.WriteHeader(tt.statusCode)
rec.WriteString(`{"model_name":"gpt-4o"}`)
resp := rec.Result()
client := NewClient("http://localhost:4000", "test-key", true)
got, err := handleAPIResponse(resp, map[string]interface{}{"model_name": "gpt-4o"}, client)
if tt.wantErr {
if err == nil {
t.Fatalf("handleAPIResponse returned no error for status %d", tt.statusCode)
}
if !strings.Contains(err.Error(), strconv.Itoa(tt.statusCode)) {
t.Errorf("error %q does not mention status %d", err.Error(), tt.statusCode)
}
return
}
if err != nil {
t.Fatalf("handleAPIResponse returned unexpected error: %v", err)
}
if got.ModelName != "gpt-4o" {
t.Errorf("got.ModelName = %q, want gpt-4o", got.ModelName)
}
})
}
}
func TestHandleMCPAPIResponseAcceptsFullSuccessRange(t *testing.T) {
tests := []struct {
name string
statusCode int
wantErr bool
}{
{name: "200 OK", statusCode: http.StatusOK, wantErr: false},
{name: "201 Created", statusCode: http.StatusCreated, wantErr: false},
{name: "202 Accepted", statusCode: http.StatusAccepted, wantErr: false},
{name: "400 Bad Request", statusCode: http.StatusBadRequest, wantErr: true},
{name: "404 Not Found", statusCode: http.StatusNotFound, wantErr: true},
{name: "500 Internal Server Error", statusCode: http.StatusInternalServerError, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
rec.WriteHeader(tt.statusCode)
rec.WriteString(`{"server_id":"srv-1","server_name":"gh"}`)
resp := rec.Result()
client := NewClient("http://localhost:4000", "test-key", true)
var mcpResp MCPServerResponse
err := handleMCPAPIResponse(resp, &mcpResp, client)
if tt.wantErr {
if err == nil {
t.Fatalf("handleMCPAPIResponse returned no error for status %d", tt.statusCode)
}
if !strings.Contains(err.Error(), strconv.Itoa(tt.statusCode)) {
t.Errorf("error %q does not mention status %d", err.Error(), tt.statusCode)
}
return
}
if err != nil {
t.Fatalf("handleMCPAPIResponse returned unexpected error: %v", err)
}
if mcpResp.ServerID != "srv-1" {
t.Errorf("mcpResp.ServerID = %q, want srv-1", mcpResp.ServerID)
}
})
}
}