mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-28 10:24:27 +08:00
When /v1/responses streaming fails after headers are sent, we now emit a type=error chunk instead of an HTTP-style {error:{...}} payload, preventing AI SDK chunk validation errors.
49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildOpenAIResponsesStreamErrorChunk(t *testing.T) {
|
|
chunk := BuildOpenAIResponsesStreamErrorChunk(http.StatusInternalServerError, "unexpected EOF", 0)
|
|
var payload map[string]any
|
|
if err := json.Unmarshal(chunk, &payload); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if payload["type"] != "error" {
|
|
t.Fatalf("type = %v, want %q", payload["type"], "error")
|
|
}
|
|
if payload["code"] != "internal_server_error" {
|
|
t.Fatalf("code = %v, want %q", payload["code"], "internal_server_error")
|
|
}
|
|
if payload["message"] != "unexpected EOF" {
|
|
t.Fatalf("message = %v, want %q", payload["message"], "unexpected EOF")
|
|
}
|
|
if payload["sequence_number"] != float64(0) {
|
|
t.Fatalf("sequence_number = %v, want %v", payload["sequence_number"], 0)
|
|
}
|
|
}
|
|
|
|
func TestBuildOpenAIResponsesStreamErrorChunkExtractsHTTPErrorBody(t *testing.T) {
|
|
chunk := BuildOpenAIResponsesStreamErrorChunk(
|
|
http.StatusInternalServerError,
|
|
`{"error":{"message":"oops","type":"server_error","code":"internal_server_error"}}`,
|
|
0,
|
|
)
|
|
var payload map[string]any
|
|
if err := json.Unmarshal(chunk, &payload); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if payload["type"] != "error" {
|
|
t.Fatalf("type = %v, want %q", payload["type"], "error")
|
|
}
|
|
if payload["code"] != "internal_server_error" {
|
|
t.Fatalf("code = %v, want %q", payload["code"], "internal_server_error")
|
|
}
|
|
if payload["message"] != "oops" {
|
|
t.Fatalf("message = %v, want %q", payload["message"], "oops")
|
|
}
|
|
}
|