重构:完成架构一致性整改与生命周期收口

This commit is contained in:
Christ committed 2026-08-18 14:05:08 +08:00
1 parent 55904cc409
commit 9eb6be2875
344 files changed
+15961 -13926

No files matched your search

+24 -24
View File
@@ -10,14 +10,14 @@ import (
"time"
)
// TestProtectedRouteGroupsRequireAuthentication 负责TestProtectedRouteGroupsRequireAuthentication相关处理。
// TestProtectedRouteGroupsRequireAuthentication 封装TestProtectedRouteGroupsRequireAuthentication业务协调。
func TestProtectedRouteGroupsRequireAuthentication(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
// srv、cleanup 用于本次流程后续判断的srv、cleanup
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// routes 保存routes,供当前处理流程使用
// routes 用于本次流程后续判断的routes
routes := []struct {
method string
path string
@@ -38,9 +38,9 @@ func TestProtectedRouteGroupsRequireAuthentication(t *testing.T) {
// route 表示当前遍历过程中的route
for _, route := range routes {
t.Run(route.method+" "+route.path, func(t *testing.T) {
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(route.method, route.path, nil)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
@@ -50,17 +50,17 @@ func TestProtectedRouteGroupsRequireAuthentication(t *testing.T) {
}
}
// TestCookiePreferenceEndpoints 负责Test登录凭证PreferenceEndpoints相关处理。
// TestCookiePreferenceEndpoints 封装Test登录凭证PreferenceEndpoints业务协调。
func TestCookiePreferenceEndpoints(t *testing.T) {
// srv、store、cleanup 保存srv、store、cleanup,供当前处理流程使用
// srv、store、cleanup 用于本次流程后续判断的srv、store、cleanup
srv, store, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := loginHelper(t, h)
// requests 保存请求列表,供当前处理流程使用
// requests 用于本次流程后续判断的请求列表
requests := []struct {
path string
body string
@@ -71,11 +71,11 @@ func TestCookiePreferenceEndpoints(t *testing.T) {
}
// tc 表示当前遍历过程中的tc
for _, tc := range requests {
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPut, tc.path, strings.NewReader(tc.body))
req.Header.Set("Content-Type", "application/json")
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
@@ -85,48 +85,48 @@ func TestCookiePreferenceEndpoints(t *testing.T) {
// path 表示当前遍历过程中的路径
for _, path := range []string{"/cookies/acc1/auto-confirm", "/cookies/acc1/pause-duration", "/cookie/acc1/details"} {
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodGet, path, nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("GET %s status=%d body=%s", path, rec.Code, rec.Body.String())
}
}
// paused、pausedUntil、err 保存paused、pausedUntil、err,供当前处理流程使用
// paused、pausedUntil、err 用于本次流程后续判断的paused、pausedUntil、err
paused, pausedUntil, err := store.Cookies.IsPaused(context.Background(), "acc1")
if err != nil || !paused || pausedUntil <= time.Now().UTC().Unix() {
t.Fatalf("pause deadline not persisted: paused=%v until=%d err=%v", paused, pausedUntil, err)
}
// pauseReq 保存pauseReq,供当前处理流程使用
// pauseReq 用于本次流程后续判断的pauseReq
pauseReq := httptest.NewRequest(http.MethodGet, "/cookies/acc1/pause-duration", nil)
pauseReq.AddCookie(cookie)
// pauseRec 保存pauseRec,供当前处理流程使用
// pauseRec 用于本次流程后续判断的pauseRec
pauseRec := httptest.NewRecorder()
h.ServeHTTP(pauseRec, pauseReq)
// pauseResponse 保存pause响应,供当前处理流程使用
// pauseResponse 用于本次流程后续判断的pause响应
var pauseResponse map[string]any
if // err 保存err,供当前处理流程使用
if // err 用于本次流程后续判断的err
err := json.Unmarshal(pauseRec.Body.Bytes(), &pauseResponse); err != nil || pauseResponse["paused"] != true {
t.Fatalf("pause response=%+v err=%v", pauseResponse, err)
}
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPut, "/cookies/acc1/pause-duration", strings.NewReader(`{"pause_duration":-1}`))
req.Header.Set("Content-Type", "application/json")
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("negative pause status=%d body=%s", rec.Code, rec.Body.String())
}
// tooLongReq 保存tooLongReq,供当前处理流程使用
// tooLongReq 用于本次流程后续判断的tooLongReq
tooLongReq := httptest.NewRequest(http.MethodPut, "/cookies/acc1/pause-duration", strings.NewReader(`{"pause_duration":1441}`))
tooLongReq.AddCookie(cookie)
// tooLongRec 保存tooLongRec,供当前处理流程使用
// tooLongRec 用于本次流程后续判断的tooLongRec
tooLongRec := httptest.NewRecorder()
h.ServeHTTP(tooLongRec, tooLongReq)
if tooLongRec.Code != http.StatusBadRequest {