Files
sub2api/backend/internal/pkg/googleapi/status_test.go
cyh 3425ba3663 fix(gateway): bound stalled request body reads and classify read failures
Gateway handlers such as Anthropic /v1/messages read the whole request body
before they know the model or account. The HTTP server sets no ReadTimeout
(large bodies may legitimately take long), so a stalled upload held the
handler until the client gave up, and every read failure then surfaced as a
generic 400 "Failed to read request body" with no diagnostics.

- Add server.request_body_idle_timeout (default 60s, 0 disables). A body
  read that receives no bytes for that long fails with 408. Each Read re-arms
  the deadline, so total upload time is not limited and handler pauses are
  not counted. HTTP/2 deadlines are armed only during a Read; HTTP/1 keeps
  one armed while the body is unfinished so net/http's own drain of an
  unread body cannot hang an early error response.
- Classify body read failures in the shared reader: read timeout (408),
  client disconnect (499), truncated body, decode errors and size limit.
  Other failures keep the stable 400 message. Map 408 to DEADLINE_EXCEEDED
  in Google-format errors.
- Log payload-free diagnostics for every gateway and middleware body read
  failure: kind, protocol, declared length, encoding, bytes received and
  read duration.
2026-10-31 11:45:55 +08:00

30 lines
777 B
Go

package googleapi
import (
"net/http"
"testing"
)
func TestHTTPStatusToGoogleStatus(t *testing.T) {
cases := []struct {
status int
want string
}{
{http.StatusBadRequest, "INVALID_ARGUMENT"},
{http.StatusUnauthorized, "UNAUTHENTICATED"},
{http.StatusForbidden, "PERMISSION_DENIED"},
{http.StatusNotFound, "NOT_FOUND"},
{http.StatusRequestTimeout, "DEADLINE_EXCEEDED"},
{http.StatusTooManyRequests, "RESOURCE_EXHAUSTED"},
{499, "CANCELLED"},
{http.StatusBadGateway, "INTERNAL"},
{http.StatusServiceUnavailable, "INTERNAL"},
{http.StatusConflict, "UNKNOWN"},
}
for _, tc := range cases {
if got := HTTPStatusToGoogleStatus(tc.status); got != tc.want {
t.Errorf("HTTPStatusToGoogleStatus(%d) = %q, want %q", tc.status, got, tc.want)
}
}
}