mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-04-23 21:42:39 +00:00
fix: tighten responses SSE review follow-up
This commit is contained in:
@@ -33,9 +33,13 @@ func writeResponsesSSEChunk(w io.Writer, chunk []byte) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if bytes.HasSuffix(chunk, []byte("\n")) {
|
if bytes.HasSuffix(chunk, []byte("\n")) {
|
||||||
_, _ = w.Write([]byte("\n"))
|
if _, err := w.Write([]byte("\n")); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_, _ = w.Write([]byte("\n\n"))
|
if _, err := w.Write([]byte("\n\n")); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,14 +35,18 @@ func TestForwardResponsesStreamSeparatesDataOnlySSEChunks(t *testing.T) {
|
|||||||
|
|
||||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs)
|
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs)
|
||||||
body := recorder.Body.String()
|
body := recorder.Body.String()
|
||||||
|
parts := strings.Split(strings.TrimSpace(body), "\n\n")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
t.Fatalf("expected 2 SSE events, got %d. Body: %q", len(parts), body)
|
||||||
|
}
|
||||||
|
|
||||||
if !strings.Contains(body, "data: {\"type\":\"response.output_item.done\"") {
|
expectedPart1 := "data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"arguments\":\"{}\"}}"
|
||||||
t.Fatalf("expected first SSE data chunk, got: %q", body)
|
if parts[0] != expectedPart1 {
|
||||||
|
t.Errorf("unexpected first event.\nGot: %q\nWant: %q", parts[0], expectedPart1)
|
||||||
}
|
}
|
||||||
if !strings.Contains(body, "\n\ndata: {\"type\":\"response.completed\"") {
|
|
||||||
t.Fatalf("expected blank-line separation before second SSE event, got: %q", body)
|
expectedPart2 := "data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp-1\",\"output\":[]}}"
|
||||||
}
|
if parts[1] != expectedPart2 {
|
||||||
if strings.Contains(body, "arguments\":\"{}\"}}data: {\"type\":\"response.completed\"") {
|
t.Errorf("unexpected second event.\nGot: %q\nWant: %q", parts[1], expectedPart2)
|
||||||
t.Fatalf("second SSE event was concatenated onto first event body: %q", body)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user