Files
ai_academy/internal/models/api/response_limit_test.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

147 lines
4.6 KiB
Go

package api
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
secutils "academy/internal/utils"
)
// fillerReader streams size bytes without allocating them, so a test can offer
// a body far larger than the cap without spending the memory.
type fillerReader struct {
remaining int64
}
func (f *fillerReader) Read(p []byte) (int, error) {
if f.remaining <= 0 {
return 0, io.EOF
}
if int64(len(p)) > f.remaining {
p = p[:f.remaining]
}
for i := range p {
p[i] = 'a'
}
f.remaining -= int64(len(p))
return len(p), nil
}
// allowLoopback whitelists the loopback host Endpoint.newPost's SSRF check
// rejects, so a test can point an Endpoint at its own httptest server.
func allowLoopback(t *testing.T) {
t.Helper()
t.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")
t.Setenv("SSRF_WHITELIST_EXTRA", "")
secutils.ResetSSRFWhitelistForTest()
t.Cleanup(secutils.ResetSSRFWhitelistForTest)
}
func TestReadResponseCapped(t *testing.T) {
t.Run("rejects over the cap", func(t *testing.T) {
const limit = 1 << 10
_, err := ReadResponseCapped(&fillerReader{remaining: limit + 1}, limit)
if !errors.Is(err, ErrResponseTooLarge) {
t.Fatalf("err = %v, want ErrResponseTooLarge", err)
}
if !strings.Contains(err.Error(), "exceeds maximum size") {
t.Errorf("error text = %q, want the canonical over-size message", err)
}
})
t.Run("accepts exactly the cap", func(t *testing.T) {
const limit = 1 << 10
got, err := ReadResponseCapped(strings.NewReader(strings.Repeat("a", limit)), limit)
if err != nil {
t.Fatalf("ReadResponseCapped() = %v, want nil at exactly the cap", err)
}
if len(got) != limit {
t.Errorf("len = %d, want %d", len(got), limit)
}
})
t.Run("non-positive limit falls back to the production cap", func(t *testing.T) {
if _, err := ReadResponseCapped(strings.NewReader(`{"ok":true}`), 0); err != nil {
t.Fatalf("ReadResponseCapped(_, 0) = %v, want nil", err)
}
})
}
// The cap must not silently truncate: a short read that still parses would look
// like a successful reply with fewer fields than the vendor sent.
func TestReadResponseCappedNeverTruncates(t *testing.T) {
const limit = 1 << 10
got, err := ReadResponseCapped(strings.NewReader(strings.Repeat("a", limit*4)), limit)
if err == nil {
t.Fatalf("expected refusal, got %d bytes", len(got))
}
if got != nil {
t.Errorf("returned %d bytes alongside the error; a caller could decode them", len(got))
}
}
// PostJSON is the whole round-trip for rerank, embeddings and transcription. An
// oversized success body has to surface as an error rather than a decode
// failure on a truncated payload.
//
// Endpoint.newPost validates the URL, so the test server's loopback address has
// to be whitelisted the same way other tests in this repo reach an httptest
// server.
func TestPostJSONRejectsOversizedBody(t *testing.T) {
allowLoopback(t)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"data":[`))
_, _ = io.Copy(w, &fillerReader{remaining: MaxResponseBytes + (1 << 20)})
}))
defer srv.Close()
var out map[string]any
err := Endpoint{Client: srv.Client()}.PostJSON(
context.Background(), srv.URL, []byte(`{}`), &out)
if err == nil {
t.Fatal("expected an oversized provider body to be refused")
}
if !errors.Is(err, ErrResponseTooLarge) {
t.Errorf("err = %v, want ErrResponseTooLarge", err)
}
if out != nil {
t.Errorf("out = %v, want nothing decoded from a refused body", out)
}
}
// withRetry retries only TransportError. An oversized reply is the same answer
// every time, and retrying it bills the request again for the same bytes, so it
// must not be classified as a transport failure.
func TestOversizedBodyIsNotRetried(t *testing.T) {
allowLoopback(t)
var attempts int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
attempts++
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"data":[`))
_, _ = io.Copy(w, &fillerReader{remaining: MaxResponseBytes + (1 << 20)})
}))
defer srv.Close()
policy := RetryPolicy{MaxRetries: 3, BaseDelay: time.Millisecond, MaxDelay: time.Millisecond}
var out map[string]any
err := Endpoint{Client: srv.Client()}.PostJSONWithRetry(
context.Background(), srv.URL, []byte(`{}`), &out, policy, "embedding")
if err == nil {
t.Fatal("expected an oversized provider body to be refused")
}
if attempts != 1 {
t.Errorf("made %d attempts, want 1: an oversized reply is deterministic "+
"and retrying it bills the request again", attempts)
}
}