Files
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

90 lines
2.8 KiB
Go

package api
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"academy/internal/types"
"github.com/stretchr/testify/require"
)
func TestEndpointRequestIdentity(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
// One endpoint is shared by different sessions. Static model headers must
// neither merge them nor leak one session into a call without a session.
ep := Endpoint{
Auth: BearerAuth("test-key"),
Headers: map[string]string{
"X-Conversation-ID": "static-id", "User-Agent": "custom-client",
"X-Custom": "preserved",
},
}
for _, sessionID := range []string{"session-a", "session-b", ""} {
t.Run("session="+sessionID, func(t *testing.T) {
ctx := types.WithSessionID(context.Background(), sessionID)
for _, stream := range []bool{false, true} {
req, _, err := ep.NewRequest(ctx, "http://127.0.0.1/v1/chat/completions", map[string]any{}, stream)
require.NoError(t, err)
require.Equal(t, sessionID, req.Header.Get("X-Conversation-ID"))
require.Equal(t, "Academy", req.UserAgent())
require.Equal(t, "preserved", req.Header.Get("X-Custom"))
require.Equal(t, "Bearer test-key", req.Header.Get("Authorization"))
if sessionID == "" {
require.NotContains(t, req.Header, "X-Conversation-Id")
}
}
require.Equal(t, "static-id", ep.Headers["X-Conversation-ID"])
})
}
}
func TestDoKeepsHeadersOnHTTPError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Retry-After", "12")
w.WriteHeader(http.StatusTooManyRequests)
_, _ = w.Write([]byte(`{"error":"rate limited"}`))
}))
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL, nil)
require.NoError(t, err)
_, err = Endpoint{Client: srv.Client()}.Do(req)
var httpErr *HTTPError
require.True(t, errors.As(err, &httpErr))
require.Equal(t, http.StatusTooManyRequests, httpErr.StatusCode)
require.Equal(t, 12*time.Second, httpErr.RetryAfter())
}
func TestParseRetryAfter(t *testing.T) {
now := time.Date(2026, 9, 26, 12, 0, 0, 0, time.UTC)
for _, tc := range []struct {
name string
value string
want time.Duration
}{
{"absent", "", 0},
{"seconds", "30", 30 * time.Second},
{"seconds with spaces", " 5 ", 5 * time.Second},
{"zero", "0", 0},
{"negative", "-3", 0},
{"http date", now.Add(90 * time.Second).Format(http.TimeFormat), 90 * time.Second},
{"http date in the past", now.Add(-time.Minute).Format(http.TimeFormat), 0},
{"garbage", "tomorrow", 0},
} {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.want, parseRetryAfter(tc.value, now))
})
}
}
func TestRetryAfterOnNilError(t *testing.T) {
var e *HTTPError
require.Zero(t, e.RetryAfter())
require.Zero(t, (&HTTPError{StatusCode: 503}).RetryAfter())
}