Files
Ydisks-Xianyu-Helper/internal/xianyu/mtop/publish_extra_test.go
T

1027 lines
41 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package mtop
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"image"
"image/color"
"image/png"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
// publish.go 用常量 URL(UploadMediaAPI / RecommendItemAPI / PublishItemAPI),
// 通过 dispatchTransport 按 api query 参数分发到不同本地 handler,覆盖各 mtop 调用路径。
// dispatchTransport 保存dispatchTransport,供当前处理流程使用
type dispatchTransport struct {
handlers map[string]http.HandlerFunc
}
// RoundTrip 负责RoundTrip相关处理。
func (d *dispatchTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// rec 保存rec,供当前处理流程使用
rec := httptest.NewRecorder()
// 选 handler:upload 走 stream-upload 域名(无 api query),其它用 api query 区分
api := req.URL.Query().Get("api")
// h、ok 保存h、ok,供当前处理流程使用
h, ok := d.handlers[api]
if !ok {
// upload 的请求路径不含 api=mtop.* ;用 "_upload" key
if strings.Contains(req.URL.Host, "stream-upload") {
h, ok = d.handlers["_upload"]
}
}
if !ok {
h = func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "no handler for api="+api, http.StatusNotFound)
}
}
h(rec, req)
// resp 保存resp,供当前处理流程使用
resp := &http.Response{
StatusCode: rec.Code,
Header: rec.Header().Clone(),
Body: rec.Result().Body,
Request: req,
}
return resp, nil
}
// TestPublishItemValidationFailures: 各参数校验分支。
func TestPublishItemValidationFailures(t *testing.T) {
// cases 保存cases,供当前处理流程使用
cases := []struct {
name string
req PublishItemRequest
want string
}{
{"empty title", PublishItemRequest{Title: " ", PriceCents: 100, Quantity: 1, Images: []PublishImage{{}}}, "商品标题不能为空"},
{"zero price", PublishItemRequest{Title: "T", PriceCents: 0, Quantity: 1, Images: []PublishImage{{}}}, "商品价格必须大于 0"},
{"zero quantity", PublishItemRequest{Title: "T", PriceCents: 100, Quantity: 0, Images: []PublishImage{{}}}, "库存数量必须大于 0"},
{"no images", PublishItemRequest{Title: "T", PriceCents: 100, Quantity: 1}, "至少上传 1 张商品图片"},
{"too many images", PublishItemRequest{Title: "T", PriceCents: 100, Quantity: 1, Images: make([]PublishImage, 10)}, "商品图片最多 9 张"},
{"incomplete preferred category", PublishItemRequest{Title: "T", PriceCents: 100, Quantity: 1, PreferredCategory: &PublishCategory{CatID: "5001"}, Images: []PublishImage{{}}}, "默认类目必须同时包含"},
}
// c 表示当前遍历过程中的c
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
// client 保存client,供当前处理流程使用
client := &ClientImpl{}
// err 保存err,供当前处理流程使用
_, err := client.PublishItem(context.Background(), consignCookies, c.req)
if err == nil || !strings.Contains(err.Error(), c.want) {
t.Fatalf("err=%v want %q", err, c.want)
}
})
}
}
// TestPublishItemDescriptionDefaultsToTitle: description 为空时回退为 title。
func TestPublishItemDescriptionDefaultsToTitle(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// gotDesc 保存gotDesc,供当前处理流程使用
var gotDesc string
// publishedData 保存published数据,供当前处理流程使用
var publishedData map[string]any
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/a.jpg","pix":"800x600"}}`)
},
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
// body 保存请求体,供当前处理流程使用
body := readBody(r)
// decoded 保存decoded,供当前处理流程使用
var decoded struct {
Data struct {
Desc string `json:"description"`
} `json:"data"`
}
_ = json.Unmarshal([]byte("{"+body+"}"), &decoded) // data= 已 url-encoded
// 直接解析 url-encoded data
if v, ok := parseDataURL(body); ok {
gotDesc = v["description"].(string)
}
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"categoryPredictResult":{"catId":"c1","catName":"类目"}}}`)
},
"mtop.idle.pc.idleitem.publish": func(w http.ResponseWriter, r *http.Request) {
publishedData, _ = parseDataURL(readBody(r))
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"itemId":"new-item-1"}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// req 保存req,供当前处理流程使用
req := PublishItemRequest{
Title: "测试商品",
PriceCents: 1000,
Quantity: 1,
PostageMode: "fixed",
PostageCents: 500,
Location: &PublishLocation{Area: "X", City: "Y", DivisionID: "1", Longitude: 118.7, Latitude: 31.9, POIID: "p1", POIName: "P", Province: "Z"},
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
}
// res、err 保存res、err,供当前处理流程使用
res, err := client.PublishItem(context.Background(), consignCookies, req)
if err != nil {
t.Fatalf("err=%v", err)
}
if res.ItemID != "new-item-1" {
t.Fatalf("ItemID=%q", res.ItemID)
}
if res.ItemURL != "https://www.goofish.com/item?id=new-item-1" {
t.Fatalf("ItemURL=%q", res.ItemURL)
}
if gotDesc != "测试商品" {
t.Fatalf("desc=%q want 测试商品 (title 回退)", gotDesc)
}
if res.ImageURL != "https://cdn/a.jpg" {
t.Fatalf("ImageURL=%q", res.ImageURL)
}
// addr、ok 保存addr、ok,供当前处理流程使用
addr, ok := publishedData["itemAddrDTO"].(map[string]any)
if !ok || addr["gps"] != "31.9,118.7" {
t.Fatalf("itemAddrDTO=%+v, gps should be latitude,longitude", publishedData["itemAddrDTO"])
}
}
// TestPublishItemUploadImageFailure: 图片上传失败导致 PublishItem 失败。
func TestPublishItemUploadImageFailure(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{}}`) // 缺 url
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// req 保存req,供当前处理流程使用
req := PublishItemRequest{
Title: "T",
PriceCents: 1000,
Quantity: 1,
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
}
// err 保存err,供当前处理流程使用
_, err := client.PublishItem(context.Background(), consignCookies, req)
if err == nil || !strings.Contains(err.Error(), "图片上传响应缺少 url") {
t.Fatalf("err=%v", err)
}
}
// TestPublishItemRecommendCategoryFailure: 类目推荐失败。
func TestPublishItemRecommendCategoryFailure(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/a.jpg","pix":"800x600"}}`)
},
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["FAIL_SYS_TOKEN_EXPIRED::令牌过期"]}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// req 保存req,供当前处理流程使用
req := PublishItemRequest{
Title: "T",
PriceCents: 1000,
Quantity: 1,
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
}
// err 保存err,供当前处理流程使用
_, err := client.PublishItem(context.Background(), consignCookies, req)
if err == nil {
t.Fatalf("expected err")
}
// pe 保存pe,供当前处理流程使用
var pe *PublishError
if !errors.As(err, &pe) || pe.Code != PublishErrorTokenExpired {
t.Fatalf("err=%v want PublishErrorTokenExpired", err)
}
}
// TestPublishItemRecommendCategoryMissingData: ret 成功但 data 缺 categoryPredictResult。
func TestPublishItemRecommendCategoryMissingDataUsesElectronicMaterials(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/a.jpg","pix":"800x600"}}`)
},
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{}}`)
},
"mtop.idle.pc.idleitem.publish": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"itemId":"fallback-item"}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// req 保存req,供当前处理流程使用
req := PublishItemRequest{
Title: "T",
PriceCents: 1000,
Quantity: 1,
Virtual: true,
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
}
// result、err 保存result、err,供当前处理流程使用
result, err := client.PublishItem(context.Background(), consignCookies, req)
if err != nil {
t.Fatalf("err=%v", err)
}
if result.CategoryID != "50023914" || result.CategoryName != "电子资料" {
t.Fatalf("result=%+v", result)
}
}
// TestPublishItemLocationFailure: 实物发布没有选择发货地。
func TestPublishItemLocationFailure(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/a.jpg","pix":"800x600"}}`)
},
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"categoryPredictResult":{"catId":"c1","catName":"类目"}}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// req 保存req,供当前处理流程使用
req := PublishItemRequest{
Title: "T",
PriceCents: 1000,
Quantity: 1,
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
}
// err 保存err,供当前处理流程使用
_, err := client.PublishItem(context.Background(), consignCookies, req)
if err == nil || !strings.Contains(err.Error(), "必须选择发货地") {
t.Fatalf("err=%v", err)
}
}
// TestPublishVirtualItemSkipsLocation 虚拟商品不查询默认地址,也不发送实物地址字段。
func TestPublishVirtualItemSkipsLocation(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// publishedData 保存published数据,供当前处理流程使用
var publishedData map[string]any
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/a.jpg","pix":"800x600"}}`)
},
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"categoryPredictResult":{"catId":"c1","catName":"类目"}}}`)
},
"mtop.idle.pc.idleitem.publish": func(w http.ResponseWriter, r *http.Request) {
publishedData, _ = parseDataURL(readBody(r))
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"itemId":"virtual-item-1"}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// res、err 保存res、err,供当前处理流程使用
res, err := client.PublishItem(context.Background(), consignCookies, PublishItemRequest{
Title: "虚拟商品",
PriceCents: 1000,
Quantity: 1,
Virtual: true,
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
})
if err != nil {
t.Fatalf("err=%v", err)
}
if res.ItemID != "virtual-item-1" {
t.Fatalf("ItemID=%q", res.ItemID)
}
if publishedData == nil {
t.Fatal("未解析到发布请求 data")
}
if // exists 保存exists,供当前处理流程使用
_, exists := publishedData["itemAddrDTO"]; exists {
t.Fatalf("虚拟商品不应发送 itemAddrDTO: %+v", publishedData["itemAddrDTO"])
}
}
// TestPublishVirtualItemUsesPreferredCategoryWithoutRecommendation 负责Test发布Virtual商品UsesPreferred分类WithoutRecommendation相关处理。
func TestPublishVirtualItemUsesPreferredCategoryWithoutRecommendation(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// publishedData 保存published数据,供当前处理流程使用
var publishedData map[string]any
// recommendCalled 保存recommendCalled,供当前处理流程使用
recommendCalled := false
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/a.jpg","pix":"800x600"}}`)
},
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
recommendCalled = true
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{}}`)
},
"mtop.idle.pc.idleitem.publish": func(w http.ResponseWriter, r *http.Request) {
publishedData, _ = parseDataURL(readBody(r))
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"itemId":"fallback-item-1"}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// res、err 保存res、err,供当前处理流程使用
res, err := client.PublishItem(context.Background(), consignCookies, PublishItemRequest{
Title: "虚拟商品",
PriceCents: 1000,
Quantity: 1,
Virtual: true,
PreferredCategory: &PublishCategory{CatID: "5001", CatName: "虚拟服务", ChannelCatID: "6001", TBCatID: "7001"},
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
})
if err != nil {
t.Fatalf("err=%v", err)
}
if recommendCalled {
t.Fatal("配置默认类目后不应调用自动推荐接口")
}
if res.CategoryID != "5001" || res.CategoryName != "虚拟服务" {
t.Fatalf("category result=%+v", res)
}
// category 保存分类,供当前处理流程使用
category, _ := publishedData["itemCatDTO"].(map[string]any)
if category["catId"] != "5001" || category["catName"] != "虚拟服务" || category["channelCatId"] != "6001" || category["tbCatId"] != "7001" {
t.Fatalf("published category=%+v", category)
}
if // exists 保存exists,供当前处理流程使用
_, exists := publishedData["itemAddrDTO"]; exists {
t.Fatalf("虚拟商品不应发送 itemAddrDTO: %+v", publishedData["itemAddrDTO"])
}
}
// TestPublishVirtualItemUsesElectronicMaterialsWhenRecommendationIsEmpty 负责Test发布Virtual商品UsesElectronicMaterialsWhenRecommendationIsEmpty相关处理。
func TestPublishVirtualItemUsesElectronicMaterialsWhenRecommendationIsEmpty(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// publishedData 保存published数据,供当前处理流程使用
var publishedData map[string]any
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/a.jpg","pix":"800x600"}}`)
},
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{}}`)
},
"mtop.idle.pc.idleitem.publish": func(w http.ResponseWriter, r *http.Request) {
publishedData, _ = parseDataURL(readBody(r))
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"itemId":"electronic-item-1"}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// res、err 保存res、err,供当前处理流程使用
res, err := client.PublishItem(context.Background(), consignCookies, PublishItemRequest{
Title: "无法识别的虚拟商品",
PriceCents: 1000,
Quantity: 1,
Virtual: true,
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
})
if err != nil {
t.Fatalf("err=%v", err)
}
if res.CategoryID != "50023914" || res.CategoryName != "电子资料" {
t.Fatalf("category result=%+v", res)
}
// category 保存分类,供当前处理流程使用
category, _ := publishedData["itemCatDTO"].(map[string]any)
if category["channelCatId"] != "202036301" {
t.Fatalf("published category=%+v", category)
}
}
// TestPublishItemFinalPublishFailure: 发布接口返回 token 过期错误。
func TestPublishItemFinalPublishFailure(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/a.jpg","pix":"800x600"}}`)
},
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"categoryPredictResult":{"catId":"c1","catName":"类目"}}}`)
},
"mtop.idle.pc.idleitem.publish": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["FAIL_SYS_TOKEN_EXPIRED::令牌过期"]}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// req 保存req,供当前处理流程使用
req := PublishItemRequest{
Title: "T",
PriceCents: 1000,
Quantity: 1,
PostageMode: "fixed",
PostageCents: 500,
Location: &PublishLocation{Area: "X", City: "Y", DivisionID: "1", Longitude: 118.7, Latitude: 31.9, POIID: "p1", POIName: "P", Province: "Z"},
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
}
// err 保存err,供当前处理流程使用
_, err := client.PublishItem(context.Background(), consignCookies, req)
if err == nil {
t.Fatalf("expected err")
}
// pe 保存pe,供当前处理流程使用
var pe *PublishError
if !errors.As(err, &pe) || pe.Code != PublishErrorTokenExpired {
t.Fatalf("err=%v want PublishErrorTokenExpired", err)
}
}
// TestPublishItemStockPermissionError: 库存权限错误分类。
func TestPublishItemStockPermissionError(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/a.jpg","pix":"800x600"}}`)
},
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"categoryPredictResult":{"catId":"c1","catName":"类目"}}}`)
},
"mtop.idle.pc.idleitem.publish": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["账号没有库存发布权限"]}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// req 保存req,供当前处理流程使用
req := PublishItemRequest{
Title: "T",
PriceCents: 1000,
Quantity: 1,
Location: &PublishLocation{Area: "X", City: "Y", DivisionID: "1", Longitude: 118.7, Latitude: 31.9, POIID: "p1", POIName: "P", Province: "Z"},
Images: []PublishImage{{Filename: "a.png", ContentType: "image/png", Data: png1}},
}
// err 保存err,供当前处理流程使用
_, err := client.PublishItem(context.Background(), consignCookies, req)
if err == nil {
t.Fatalf("expected err")
}
// pe 保存pe,供当前处理流程使用
var pe *PublishError
if !errors.As(err, &pe) || pe.Code != PublishErrorStockPermissionMissing {
t.Fatalf("err=%v want PublishErrorStockPermissionMissing", err)
}
}
// ---- uploadPublishImage 直接测试 ----
// TestUploadPublishImageSuccess 负责TestUpload发布图片Success相关处理。
func TestUploadPublishImageSuccess(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
// 验证 multipart 包含文件
ct := r.Header.Get("content-type")
if !strings.HasPrefix(ct, "multipart/form-data") {
t.Errorf("content-type=%q", ct)
}
fmt.Fprint(w, `{"object":{"url":"https://cdn/x.jpg","pix":"640x480"}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// img 保存img,供当前处理流程使用
img := PublishImage{Filename: "x.png", ContentType: "image/png", Data: png1}
// res、updated、err 保存res、updated、err,供当前处理流程使用
res, updated, err := client.uploadPublishImage(context.Background(), consignCookies, img)
if err != nil {
t.Fatalf("err=%v", err)
}
if res.URL != "https://cdn/x.jpg" || res.Width != 640 || res.Height != 480 {
t.Fatalf("res=%+v", res)
}
if updated != consignCookies {
t.Fatalf("updated=%q want orig", updated)
}
}
// TestUploadPublishImageHTTPError 负责TestUpload发布图片HTTP错误相关处理。
func TestUploadPublishImageHTTPError(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "boom", http.StatusInternalServerError)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// err 保存err,供当前处理流程使用
_, _, err := client.uploadPublishImage(context.Background(), consignCookies, PublishImage{Data: png1})
if err == nil || !strings.Contains(err.Error(), "上传商品图片失败: http=500") {
t.Fatalf("err=%v", err)
}
}
// TestUploadPublishImageParseFailure 负责TestUpload发布图片ParseFailure相关处理。
func TestUploadPublishImageParseFailure(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `not-json{`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// err 保存err,供当前处理流程使用
_, _, err := client.uploadPublishImage(context.Background(), consignCookies, PublishImage{Data: png1})
if err == nil || !strings.Contains(err.Error(), "解析图片上传响应失败") {
t.Fatalf("err=%v", err)
}
}
// TestUploadPublishImageMissingURL 负责TestUpload发布图片MissingURL相关处理。
func TestUploadPublishImageMissingURL(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"pix":"800x600"}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// err 保存err,供当前处理流程使用
_, _, err := client.uploadPublishImage(context.Background(), consignCookies, PublishImage{Data: png1})
if err == nil || !strings.Contains(err.Error(), "图片上传响应缺少 url") {
t.Fatalf("err=%v", err)
}
}
// TestUploadPublishImageFallbackURL 负责TestUpload发布图片FallbackURL相关处理。
func TestUploadPublishImageFallbackURL(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
// object.url 为空,回退到 decoded["url"]
fmt.Fprint(w, `{"object":{},"url":"https://cdn/fallback.jpg"}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// res、err 保存res、err,供当前处理流程使用
res, _, err := client.uploadPublishImage(context.Background(), consignCookies, PublishImage{Data: png1})
if err != nil {
t.Fatalf("err=%v", err)
}
if res.URL != "https://cdn/fallback.jpg" {
t.Fatalf("URL=%q", res.URL)
}
}
// TestUploadPublishImageDecodeConfigFallback 负责TestUpload发布图片Decode配置Fallback相关处理。
func TestUploadPublishImageDecodeConfigFallback(t *testing.T) {
// pix 字段空,但图片是合法 PNG,走 image.DecodeConfig 回退。
png1 := tinyPNG(t)
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"_upload": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"object":{"url":"https://cdn/y.jpg"}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// res、err 保存res、err,供当前处理流程使用
res, _, err := client.uploadPublishImage(context.Background(), consignCookies, PublishImage{Filename: "y.png", ContentType: "image/png", Data: png1})
if err != nil {
t.Fatalf("err=%v", err)
}
// tinyPNG 是 1x1
if res.Width != 1 || res.Height != 1 {
t.Fatalf("Width=%d Height=%d want 1x1", res.Width, res.Height)
}
}
// ---- recommendPublishCategory 直接测试 ----
// TestRecommendPublishCategorySuccess 负责TestRecommend发布分类Success相关处理。
func TestRecommendPublishCategorySuccess(t *testing.T) {
// png1 保存png1,供当前处理流程使用
png1 := tinyPNG(t)
// imgs 保存imgs,供当前处理流程使用
imgs := []uploadedImage{{URL: "https://cdn/a.jpg", Width: 800, Height: 600}}
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"categoryPredictResult":{"catId":"c1","catName":"类目","channelCatId":"cc1","tbCatId":"tc1"}}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// cat、err 保存cat、err,供当前处理流程使用
cat, _, err := client.recommendPublishCategory(context.Background(), consignCookies, "T", "D", imgs)
if err != nil {
t.Fatalf("err=%v", err)
}
// sub 保存sub,供当前处理流程使用
sub := cat["categoryPredictResult"].(map[string]any)
if sub["catId"] != "c1" {
t.Fatalf("cat=%+v", cat)
}
_ = png1
}
// TestRecommendPublishCategoryUsesSelectedCategoryCard 负责TestRecommend发布分类UsesSelected分类卡密相关处理。
func TestRecommendPublishCategoryUsesSelectedCategoryCard(t *testing.T) {
// imgs 保存imgs,供当前处理流程使用
imgs := []uploadedImage{{URL: "https://cdn/a.jpg", Width: 800, Height: 600}}
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"cardList":[{"cardData":{"propertyId":"-10000","propertyName":"分类","valuesList":[{"catId":"50023914","catName":"电子资料","channelCatId":"202036301","isClicked":"1"}]}}]}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// category、err 保存category、err,供当前处理流程使用
category, _, err := client.recommendPublishCategory(context.Background(), consignCookies, "其他虚拟资料", "其他虚拟资料", imgs)
if err != nil {
t.Fatalf("err=%v", err)
}
// selected 保存selected,供当前处理流程使用
selected := category["categoryPredictResult"].(map[string]any)
if selected["catId"] != "50023914" || selected["catName"] != "电子资料" || selected["channelCatId"] != "202036301" {
t.Fatalf("selected=%+v", selected)
}
if // labels 保存labels,供当前处理流程使用
labels := publishLabels(category); len(labels) != 1 {
t.Fatalf("labels=%+v", labels)
}
}
// TestDefaultVirtualPublishCategoryIncludesSelectedCategoryLabel 负责TestDefaultVirtual发布分类IncludesSelected分类Label相关处理。
func TestDefaultVirtualPublishCategoryIncludesSelectedCategoryLabel(t *testing.T) {
// category 保存分类,供当前处理流程使用
category := DefaultVirtualPublishCategory()
if category.CatID != "50023914" || category.CatName != "电子资料" || category.ChannelCatID != "202036301" || category.TBCatID != "" {
t.Fatalf("default category=%+v", category)
}
// fallback 保存fallback,供当前处理流程使用
fallback := fallbackPublishCategory(category)
// labels 保存labels,供当前处理流程使用
labels := publishLabels(fallback)
if len(labels) != 1 {
t.Fatalf("labels=%+v", labels)
}
// label 保存label,供当前处理流程使用
label := labels[0].(map[string]any)
if label["channelCateId"] != "202036301" || label["channelCateName"] != "电子资料" || label["propertyId"] != "-10000" {
t.Fatalf("label=%+v", label)
}
}
// TestRecommendPublishCategoryDataNil 负责TestRecommend发布分类数据Nil相关处理。
func TestRecommendPublishCategoryDataNil(t *testing.T) {
// imgs 保存imgs,供当前处理流程使用
imgs := []uploadedImage{{URL: "u", Width: 1, Height: 1}}
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"]}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// err 保存err,供当前处理流程使用
_, _, err := client.recommendPublishCategory(context.Background(), consignCookies, "T", "D", imgs)
if err == nil || !strings.Contains(err.Error(), "类目推荐响应缺少 data") {
t.Fatalf("err=%v", err)
}
}
// TestRecommendPublishCategoryEmptyCatId 负责TestRecommend发布分类EmptyCat标识相关处理。
func TestRecommendPublishCategoryEmptyCatId(t *testing.T) {
// imgs 保存imgs,供当前处理流程使用
imgs := []uploadedImage{{URL: "u", Width: 1, Height: 1}}
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"mtop.taobao.idle.kgraph.property.recommend": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"ret":["SUCCESS::调用成功"],"data":{"categoryPredictResult":{}}}`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// err 保存err,供当前处理流程使用
_, _, err := client.recommendPublishCategory(context.Background(), consignCookies, "T", "D", imgs)
if err == nil || !strings.Contains(err.Error(), "未能自动识别商品类目") {
t.Fatalf("err=%v", err)
}
}
// ---- callMTop / buildMTopQuery ----
// TestCallMTopParseFailure 负责TestCallMTopParseFailure相关处理。
func TestCallMTopParseFailure(t *testing.T) {
// dt 保存dt,供当前处理流程使用
dt := &dispatchTransport{handlers: map[string]http.HandlerFunc{
"some.api": func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `bad{`)
},
}}
// client 保存client,供当前处理流程使用
client := &ClientImpl{HTTPClient: &http.Client{Transport: dt}}
// err 保存err,供当前处理流程使用
_, _, err := client.callMTop(context.Background(), consignCookies, "http://x", "some.api", "1.0", "spm", "spmPre", "log", map[string]any{"k": "v"})
if err == nil || !strings.Contains(err.Error(), "解析 some.api 响应失败") {
t.Fatalf("err=%v", err)
}
}
// TestCallMTopRequestError 负责TestCallMTop请求错误相关处理。
func TestCallMTopRequestError(t *testing.T) {
// 指向不可达地址
client := &ClientImpl{HTTPClient: &http.Client{Transport: &rewriteTransport{base: http.DefaultTransport, target: "http://127.0.0.1:1"}, Timeout: 0}}
// err 保存err,供当前处理流程使用
_, _, err := client.callMTop(context.Background(), consignCookies, "http://127.0.0.1:1", "any.api", "1.0", "s", "p", "l", map[string]any{})
if err == nil || !strings.Contains(err.Error(), "any.api 请求失败") {
t.Fatalf("err=%v", err)
}
}
// TestBuildMTopQuery 负责TestBuildMTop查询相关处理。
func TestBuildMTopQuery(t *testing.T) {
// q 保存q,供当前处理流程使用
q := buildMTopQuery("myapi", "2.0", "T", "SIGN", "spmCnt", "spmPre", "logID")
if !strings.Contains(q, "api=myapi") || !strings.Contains(q, "v=2.0") ||
!strings.Contains(q, "t=T") || !strings.Contains(q, "sign=SIGN") ||
!strings.Contains(q, "spm_cnt=spmCnt") || !strings.Contains(q, "spm_pre=spmPre") ||
!strings.Contains(q, "log_id=logID") {
t.Fatalf("query=%q 缺字段", q)
}
}
// ---- 发布相关纯函数 ----
// TestPublishImagePayload 负责Test发布图片请求载荷相关处理。
func TestPublishImagePayload(t *testing.T) {
// p 保存p,供当前处理流程使用
p := publishImagePayload(uploadedImage{URL: "u", Width: 100, Height: 200}, true)
if p["url"] != "u" || p["widthSize"] != 100 || p["heightSize"] != 200 ||
p["major"] != true || p["type"] != 0 || p["status"] != "done" {
t.Fatalf("payload=%+v", p)
}
}
// TestPublishLabelsExtractsClicked 负责Test发布LabelsExtractsClicked相关处理。
func TestPublishLabelsExtractsClicked(t *testing.T) {
// category 保存分类,供当前处理流程使用
category := map[string]any{
"cardList": []any{
map[string]any{
"cardData": map[string]any{
"propertyId": "p1",
"propertyName": "品牌",
"valuesList": []any{
map[string]any{"isClicked": false, "catName": "X"},
map[string]any{"isClicked": true, "catName": "Nike", "channelCatId": "cc", "tbCatId": "tc"},
},
},
},
},
}
// labels 保存labels,供当前处理流程使用
labels := publishLabels(category)
if len(labels) != 1 {
t.Fatalf("labels=%d want 1", len(labels))
}
// l 保存l,供当前处理流程使用
l := labels[0].(map[string]any)
if l["propertyId"] != "p1" || l["channelCateId"] != "cc" || l["tbCatId"] != "tc" {
t.Fatalf("label=%+v", l)
}
if l["properties"] != "p1##品牌:cc##Nike" {
t.Fatalf("properties=%v", l["properties"])
}
}
// TestPublishLabelsEmpty 负责Test发布LabelsEmpty相关处理。
func TestPublishLabelsEmpty(t *testing.T) {
if // got 保存got,供当前处理流程使用
got := publishLabels(map[string]any{}); len(got) != 0 {
t.Fatalf("got=%v", got)
}
// cardData 为 nil 的卡片应跳过
out := publishLabels(map[string]any{"cardList": []any{map[string]any{"cardData": nil}}})
if len(out) != 0 {
t.Fatalf("got=%v", out)
}
}
// TestPublishErrorError 负责Test发布错误错误相关处理。
func TestPublishErrorError(t *testing.T) {
// 有 Ret
e := &PublishError{Ret: []string{"FAIL::a", "FAIL::b"}}
if // got 保存got,供当前处理流程使用
got := e.Error(); got != "FAIL::a; FAIL::b" {
t.Fatalf("Error()=%q", got)
}
// 无 Ret,有 Body
e = &PublishError{Code: PublishErrorUnknown, Body: "some body"}
if // got 保存got,供当前处理流程使用
got := e.Error(); got != "some body" {
t.Fatalf("Error()=%q", got)
}
// 无 Ret 无 Body,回落 Code
e = &PublishError{Code: PublishErrorUnknown}
if // got 保存got,供当前处理流程使用
got := e.Error(); got != string(PublishErrorUnknown) {
t.Fatalf("Error()=%q", got)
}
// 长 Body 截断
longBody := strings.Repeat("x", 300)
e = &PublishError{Code: PublishErrorUnknown, Body: longBody}
// got 保存got,供当前处理流程使用
got := e.Error()
if !strings.HasPrefix(got, "xxxxx") || len(got) > 244 {
t.Fatalf("Error() 截断异常 len=%d", len(got))
}
}
// TestClassifyPublishErrorUnknown 负责TestClassify发布错误Unknown相关处理。
func TestClassifyPublishErrorUnknown(t *testing.T) {
// 非已知错误,回落 unknown
err := classifyPublishError([]string{"FAIL_BIZ_RANDOM::随机错误"}, map[string]any{})
// pe 保存pe,供当前处理流程使用
var pe *PublishError
if !errors.As(err, &pe) || pe.Code != PublishErrorUnknown {
t.Fatalf("err=%v want unknown", err)
}
}
// TestClassifyPublishErrorLoginInBody 负责TestClassify发布错误登录In请求体相关处理。
func TestClassifyPublishErrorLoginInBody(t *testing.T) {
// ret 非空,但 body 含 login
err := classifyPublishError([]string{}, map[string]any{"msg": "need login again"})
// pe 保存pe,供当前处理流程使用
var pe *PublishError
if !errors.As(err, &pe) || pe.Code != PublishErrorTokenExpired {
t.Fatalf("err=%v want token expired", err)
}
}
// TestContainsAny 负责TestContainsAny相关处理。
func TestContainsAny(t *testing.T) {
if !containsAny("has stock permission", []string{"stock", "permission"}) {
t.Fatalf("want true")
}
if containsAny("nothing here", []string{"stock", "permission"}) {
t.Fatalf("want false")
}
if containsAny("", []string{}) {
t.Fatalf("empty want false")
}
}
// TestRetFromDecoded 负责TestRetFromDecoded相关处理。
func TestRetFromDecoded(t *testing.T) {
// decoded 保存decoded,供当前处理流程使用
decoded := map[string]any{"ret": []any{"SUCCESS::a", "FAIL::b", 123}}
// got 保存got,供当前处理流程使用
got := retFromDecoded(decoded)
if len(got) != 3 || got[0] != "SUCCESS::a" || got[1] != "FAIL::b" || got[2] != "123" {
t.Fatalf("got=%v", got)
}
// 无 ret 字段
if got := retFromDecoded(map[string]any{}); len(got) != 0 {
t.Fatalf("got=%v", got)
}
}
// TestMapFromAny 负责TestMapFromAny相关处理。
func TestMapFromAny(t *testing.T) {
if // m 保存m,供当前处理流程使用
m := mapFromAny(map[string]any{"k": "v"}); m == nil || m["k"] != "v" {
t.Fatalf("m=%v", m)
}
if // m 保存m,供当前处理流程使用
m := mapFromAny("not-map"); m != nil {
t.Fatalf("m=%v want nil", m)
}
if // m 保存m,供当前处理流程使用
m := mapFromAny(nil); m != nil {
t.Fatalf("m=%v want nil", m)
}
}
// TestFindStringDeepNested 负责TestFindStringDeepNested相关处理。
func TestFindStringDeepNested(t *testing.T) {
// v 保存v,供当前处理流程使用
v := map[string]any{
"a": []any{
map[string]any{"b": map[string]any{"item_id": "deep-id"}},
},
}
if // got 保存got,供当前处理流程使用
got := findStringDeep(v, "itemId", "item_id", "id"); got != "deep-id" {
t.Fatalf("got=%q", got)
}
// 多 key 命中第一个非空
v = map[string]any{"id": "first"}
if // got 保存got,供当前处理流程使用
got := findStringDeep(v, "itemId", "item_id", "id"); got != "first" {
t.Fatalf("got=%q", got)
}
// 找不到
if got := findStringDeep(map[string]any{"a": "b"}, "missing"); got != "" {
t.Fatalf("got=%q", got)
}
}
// TestEscapeMultipartFilename 负责TestEscapeMultipartFilename相关处理。
func TestEscapeMultipartFilename(t *testing.T) {
if // got 保存got,供当前处理流程使用
got := escapeMultipartFilename(`a"b\c`); got != `a\"b\\c` {
t.Fatalf("got=%q", got)
}
if // got 保存got,供当前处理流程使用
got := escapeMultipartFilename("normal"); got != "normal" {
t.Fatalf("got=%q", got)
}
}
// ---- helpers ----
// tinyPNG 负责tinyPNG相关处理。
func tinyPNG(t *testing.T) []byte {
t.Helper()
// img 保存img,供当前处理流程使用
img := image.NewRGBA(image.Rect(0, 0, 1, 1))
img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255})
// buf 保存buf,供当前处理流程使用
var buf bytes.Buffer
if // err 保存err,供当前处理流程使用
err := png.Encode(&buf, img); err != nil {
t.Fatalf("png encode: %v", err)
}
return buf.Bytes()
}
// readBody 负责read请求体相关处理。
func readBody(r *http.Request) string {
// buf 保存buf,供当前处理流程使用
buf := make([]byte, r.ContentLength)
r.Body.Read(buf)
return string(buf)
}
// parseDataURL 解析 "data=%7B...%7D" 形式的 body 中的 data 字段为 map。
func parseDataURL(body string) (map[string]any, bool) {
if !strings.HasPrefix(body, "data=") {
return nil, false
}
// enc 保存enc,供当前处理流程使用
enc := strings.TrimPrefix(body, "data=")
// dec、err 保存dec、err,供当前处理流程使用
dec, err := url.QueryUnescape(enc)
if err != nil {
return nil, false
}
// m 保存m,供当前处理流程使用
var m map[string]any
if // err 保存err,供当前处理流程使用
err := json.Unmarshal([]byte(dec), &m); err != nil {
return nil, false
}
return m, true
}