API版本化:迁移关键词与默认回复入口
This commit is contained in:
1 parent
d3648e9985
commit
acd9470d51
9 files changed
+177
-19
No files matched your search
@@ -0,0 +1,77 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// replyRouteCase 描述一个关键词回复或默认回复兼容路由测试样例。
|
||||
type replyRouteCase struct {
|
||||
// name 是测试样例名称。
|
||||
name string
|
||||
// method 是请求使用的 HTTP 方法。
|
||||
method string
|
||||
// versionedPath 是版本化入口路径。
|
||||
versionedPath string
|
||||
// legacyPath 是旧兼容入口路径。
|
||||
legacyPath string
|
||||
// body 是两条入口共用的请求体。
|
||||
body string
|
||||
// wantStatus 是两条入口应返回的状态码。
|
||||
wantStatus int
|
||||
}
|
||||
|
||||
// requestReplyRoute 发送带认证会话的回复规则请求并返回状态码。
|
||||
func requestReplyRoute(t *testing.T, handler http.Handler, sessionCookie *http.Cookie, method, path, body string) int {
|
||||
t.Helper()
|
||||
// request 是当前关键词或默认回复兼容入口请求。
|
||||
request := httptest.NewRequest(method, path, strings.NewReader(body))
|
||||
request.AddCookie(sessionCookie)
|
||||
// recorder 是捕获兼容入口响应的记录器。
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
return recorder.Code
|
||||
}
|
||||
|
||||
// TestVersionedReplyRoutesPreserveLegacyContracts 验证关键词和默认回复入口复用旧 handler 与权限边界。
|
||||
func TestVersionedReplyRoutesPreserveLegacyContracts(t *testing.T) {
|
||||
// srv 是用于验证关键词和默认回复路由的 HTTP 测试服务。
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// handler 是当前测试使用的完整路由树。
|
||||
handler := srv.Router()
|
||||
// sessionCookie 是管理员登录后得到的认证会话。
|
||||
sessionCookie := loginHelper(t, handler)
|
||||
// cases 是覆盖版本化与旧路径状态码兼容性的测试样例集合。
|
||||
cases := []replyRouteCase{
|
||||
{name: "keywords-list", method: http.MethodGet, versionedPath: "/api/v1/reply-rules/acc1", legacyPath: "/keywords/acc1", wantStatus: http.StatusOK},
|
||||
{name: "keywords-create-invalid", method: http.MethodPost, versionedPath: "/api/v1/reply-rules/acc1", legacyPath: "/keywords/acc1", body: `{}`, wantStatus: http.StatusBadRequest},
|
||||
{name: "keywords-item-list", method: http.MethodGet, versionedPath: "/api/v1/reply-rules/acc1/items", legacyPath: "/keywords-with-item-id/acc1", wantStatus: http.StatusOK},
|
||||
{name: "keywords-item-create-invalid", method: http.MethodPost, versionedPath: "/api/v1/reply-rules/acc1/items", legacyPath: "/keywords-with-item-id/acc1", body: `{}`, wantStatus: http.StatusBadRequest},
|
||||
{name: "keywords-typed-list", method: http.MethodGet, versionedPath: "/api/v1/reply-rules/acc1/typed", legacyPath: "/keywords-with-type/acc1", wantStatus: http.StatusOK},
|
||||
{name: "keywords-typed-update-invalid", method: http.MethodPut, versionedPath: "/api/v1/reply-rules/acc1/typed/invalid", legacyPath: "/keywords-with-type/acc1/invalid", body: `{}`, wantStatus: http.StatusBadRequest},
|
||||
{name: "keywords-typed-delete-invalid", method: http.MethodDelete, versionedPath: "/api/v1/reply-rules/acc1/typed/invalid", legacyPath: "/keywords-with-type/acc1/invalid", wantStatus: http.StatusBadRequest},
|
||||
{name: "keywords-index-delete-missing", method: http.MethodDelete, versionedPath: "/api/v1/reply-rules/missing/index/0", legacyPath: "/keywords/missing/0", wantStatus: http.StatusNotFound},
|
||||
{name: "item-replies-list", method: http.MethodGet, versionedPath: "/api/v1/reply-rules/items", legacyPath: "/itemReplays", wantStatus: http.StatusOK},
|
||||
{name: "item-reply-get-missing", method: http.MethodGet, versionedPath: "/api/v1/reply-rules/items/acc1/missing", legacyPath: "/item-reply/acc1/missing", wantStatus: http.StatusOK},
|
||||
{name: "item-reply-update-invalid", method: http.MethodPut, versionedPath: "/api/v1/reply-rules/items/acc1/item-1", legacyPath: "/item-reply/acc1/item-1", body: `not-json`, wantStatus: http.StatusBadRequest},
|
||||
{name: "item-reply-delete", method: http.MethodDelete, versionedPath: "/api/v1/reply-rules/items/acc1/missing", legacyPath: "/item-reply/acc1/missing", wantStatus: http.StatusOK},
|
||||
{name: "default-replies-map", method: http.MethodGet, versionedPath: "/api/v1/default-replies", legacyPath: "/api/default-replies", wantStatus: http.StatusOK},
|
||||
{name: "default-replies-list", method: http.MethodGet, versionedPath: "/api/v1/default-replies/list", legacyPath: "/default-replies", wantStatus: http.StatusOK},
|
||||
{name: "default-reply-get", method: http.MethodGet, versionedPath: "/api/v1/default-replies/acc1", legacyPath: "/api/default-reply/acc1", wantStatus: http.StatusOK},
|
||||
{name: "default-reply-update-invalid", method: http.MethodPut, versionedPath: "/api/v1/default-replies/acc1", legacyPath: "/api/default-reply/acc1", body: `not-json`, wantStatus: http.StatusBadRequest},
|
||||
{name: "default-reply-delete-missing", method: http.MethodDelete, versionedPath: "/api/v1/default-replies/missing", legacyPath: "/default-replies/missing", wantStatus: http.StatusNotFound},
|
||||
{name: "default-reply-clear-missing", method: http.MethodPost, versionedPath: "/api/v1/default-replies/missing/clear-records", legacyPath: "/api/default-reply/missing/clear-records", wantStatus: http.StatusNotFound},
|
||||
}
|
||||
for _, routeCase := range cases { // routeCase 是当前正在执行的回复规则路由样例。
|
||||
// versionedStatus 是版本化入口实际返回的状态码。
|
||||
versionedStatus := requestReplyRoute(t, handler, sessionCookie, routeCase.method, routeCase.versionedPath, routeCase.body)
|
||||
// legacyStatus 是旧兼容入口实际返回的状态码。
|
||||
legacyStatus := requestReplyRoute(t, handler, sessionCookie, routeCase.method, routeCase.legacyPath, routeCase.body)
|
||||
if versionedStatus != routeCase.wantStatus || legacyStatus != routeCase.wantStatus {
|
||||
t.Errorf("%s status versioned=%d legacy=%d want=%d", routeCase.name, versionedStatus, legacyStatus, routeCase.wantStatus)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"xianyu-go/internal/auth"
|
||||
)
|
||||
|
||||
// mountVersionedReplyRoutes 挂载关键词回复、指定商品回复和默认回复的 `/api/v1` 兼容入口。
|
||||
func (s *Server) mountVersionedReplyRoutes(r chi.Router) {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(s.Auth.Middleware)
|
||||
r.Use(auth.RequireAuth)
|
||||
|
||||
// 指定商品回复使用独立资源前缀,避免与账号级关键词规则混淆。
|
||||
r.Get("/api/v1/reply-rules/items", s.listItemReplies)
|
||||
r.Get("/api/v1/reply-rules/items/{cookie_id}/{item_id}", s.getItemReply)
|
||||
r.Put("/api/v1/reply-rules/items/{cookie_id}/{item_id}", s.setItemReply)
|
||||
r.Delete("/api/v1/reply-rules/items/{cookie_id}/{item_id}", s.deleteItemReply)
|
||||
|
||||
// 关键词规则保留基础、商品和类型三种旧响应语义。
|
||||
r.Get("/api/v1/reply-rules/{cid}", s.listKeywords)
|
||||
r.Post("/api/v1/reply-rules/{cid}", s.addKeyword)
|
||||
r.Get("/api/v1/reply-rules/{cid}/items", s.listKeywordsWithItemID)
|
||||
r.Post("/api/v1/reply-rules/{cid}/items", s.addKeywordWithItemID)
|
||||
r.Get("/api/v1/reply-rules/{cid}/typed", s.listKeywordsWithType)
|
||||
r.Put("/api/v1/reply-rules/{cid}/typed/{id}", s.updateKeywordByID)
|
||||
r.Delete("/api/v1/reply-rules/{cid}/typed/{id}", s.deleteKeywordByID)
|
||||
r.Delete("/api/v1/reply-rules/{cid}/index/{index}", s.deleteKeyword)
|
||||
|
||||
// 默认回复的列表、单账号配置和记录清理统一使用默认回复资源前缀。
|
||||
r.Get("/api/v1/default-replies", s.listDefaultRepliesMap)
|
||||
r.Get("/api/v1/default-replies/list", s.listDefaultReplies)
|
||||
r.Get("/api/v1/default-replies/{cid}", s.getDefaultReply)
|
||||
r.Put("/api/v1/default-replies/{cid}", s.setDefaultReply)
|
||||
r.Delete("/api/v1/default-replies/{cid}", s.deleteDefaultReply)
|
||||
r.Post("/api/v1/default-replies/{cid}/clear-records", s.clearDefaultReplyRecords)
|
||||
})
|
||||
}
|
||||
@@ -12,6 +12,7 @@ func (s *Server) mountHealthAndVersionedRoutes(r chi.Router) {
|
||||
s.mountVersionedItemBatchRoutes(r)
|
||||
s.mountVersionedSettingsCardNotificationRoutes(r)
|
||||
s.mountVersionedChatTaskRoutes(r)
|
||||
s.mountVersionedReplyRoutes(r)
|
||||
}
|
||||
|
||||
// mountVersionedSession 挂载会话 API 的 `/api/v1` 兼容入口,复用现有 handler。
|
||||
|
||||
Reference in new issue
Block a user