Files
llm-intelligence/scripts/subscription_import_common_test.go

49 lines
1.1 KiB
Go
Raw Normal View History

//go:build llm_script
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
type assertiveError string
func (e assertiveError) Error() string {
return string(e)
}
func TestFetchSubscriptionPageRetriesForbiddenThenSucceeds(t *testing.T) {
attempts := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attempts++
if attempts == 1 {
w.WriteHeader(http.StatusForbidden)
_, _ = w.Write([]byte("blocked"))
return
}
_, _ = w.Write([]byte("<html><body>套餐价格</body></html>"))
}))
defer server.Close()
client := &http.Client{Timeout: 2 * time.Second}
body, err := fetchSubscriptionPage(server.URL, "", client)
if err != nil {
t.Fatalf("fetchSubscriptionPage 返回错误: %v", err)
}
if body != "套餐价格" {
t.Fatalf("返回体归一化错误: %q", body)
}
if attempts != 2 {
t.Fatalf("期望重试 1 次后成功,实际请求 %d 次", attempts)
}
}
func TestIsRetriableSubscriptionFetchErrorRecognizesForbidden(t *testing.T) {
if !isRetriableSubscriptionFetchError(assertiveError("unexpected status 403")) {
t.Fatalf("403 应被视作可重试错误")
}
}