feat: 实现商品目录批量导入接口 (#133)

This commit is contained in:
chengma
2026-08-11 10:06:10 +08:00
parent 585d9d7834
commit 273bcc14a6
8 changed files with 695 additions and 0 deletions
+31
View File
@@ -1,6 +1,7 @@
package integration
import (
"bytes"
"net/http"
"net/http/httptest"
"strings"
@@ -45,3 +46,33 @@ func TestRequireCatalogToken(t *testing.T) {
})
}
}
func TestCatalogBatchAPI_鉴权JSON与体积限制(t *testing.T) {
gin.SetMode(gin.TestMode)
const token = "0123456789abcdef0123456789abcdef"
r := gin.New()
Register(r, nil, config.CatalogIntegrationConfig{Source: "script", Token: token})
for _, tc := range []struct {
name, auth string
body []byte
want int
}{
{"无鉴权", "", []byte(`{}`), http.StatusUnauthorized},
{"坏JSON", "Bearer " + token, []byte(`{"schema_version":`), http.StatusBadRequest},
{"超限", "Bearer " + token, bytes.Repeat([]byte("x"), catalogMaxBody+1), http.StatusRequestEntityTooLarge},
} {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/api/v1/integrations/catalog/batches", bytes.NewReader(tc.body))
req.Header.Set("Authorization", tc.auth)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != tc.want {
t.Fatalf("status=%d body=%s", resp.Code, resp.Body.String())
}
if !strings.Contains(resp.Header().Get("Content-Type"), "application/json") {
t.Fatalf("错误响应必须是 JSON:%s", resp.Header())
}
})
}
}