mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-13 23:11:40 +00:00
fix/allow bodyless 2xx
This commit is contained in:
parent
98fb823dc2
commit
9d02b4546f
2 changed files with 38 additions and 0 deletions
|
|
@ -54,6 +54,10 @@ func handleAPIResponse(resp *http.Response, reqBody interface{}, client *Client)
|
|||
resp.Status, client.redactSensitiveData(string(bodyBytes)), client.redactSensitiveData(string(reqBodyBytes)))
|
||||
}
|
||||
|
||||
if len(bodyBytes) == 0 || string(bodyBytes) == "null" {
|
||||
return &ModelResponse{}, nil
|
||||
}
|
||||
|
||||
var modelResp ModelResponse
|
||||
if err := json.Unmarshal(bodyBytes, &modelResp); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse response: %v", err)
|
||||
|
|
@ -143,6 +147,10 @@ func handleMCPAPIResponse(resp *http.Response, result interface{}, client *Clien
|
|||
resp.Status, client.redactSensitiveData(string(bodyBytes)))
|
||||
}
|
||||
|
||||
if len(bodyBytes) == 0 || string(bodyBytes) == "null" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(bodyBytes, result); err != nil {
|
||||
return fmt.Errorf("failed to parse response: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,6 +52,22 @@ func TestHandleAPIResponseAcceptsFullSuccessRange(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestHandleAPIResponseAcceptsEmptyBodyOn2xx(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
rec.WriteHeader(http.StatusNoContent)
|
||||
resp := rec.Result()
|
||||
|
||||
client := NewClient("http://localhost:4000", "test-key", true)
|
||||
got, err := handleAPIResponse(resp, map[string]interface{}{"model_name": "gpt-4o"}, client)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("handleAPIResponse returned unexpected error for empty-body 204: %v", err)
|
||||
}
|
||||
if got == nil {
|
||||
t.Fatal("handleAPIResponse returned nil ModelResponse for empty-body 204")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleMCPAPIResponseAcceptsFullSuccessRange(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
@ -96,3 +112,17 @@ func TestHandleMCPAPIResponseAcceptsFullSuccessRange(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleMCPAPIResponseAcceptsEmptyBodyOn2xx(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
rec.WriteHeader(http.StatusNoContent)
|
||||
resp := rec.Result()
|
||||
|
||||
client := NewClient("http://localhost:4000", "test-key", true)
|
||||
var mcpResp MCPServerResponse
|
||||
err := handleMCPAPIResponse(resp, &mcpResp, client)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("handleMCPAPIResponse returned unexpected error for empty-body 204: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue