88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
package wup_test
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"huya-go/internal/huya"
|
|
"huya-go/internal/wup"
|
|
)
|
|
|
|
func TestWupEncodeDecodeHeader(t *testing.T) {
|
|
req := wup.NewRequest(huya.ServantWebActUI, huya.FuncGetUserScore, 2)
|
|
err := req.WriteStruct("tReq", &huya.GetUserScoreReq{
|
|
User: huya.NewActivityUserID(123, "yyuid=123;", huya.ActivityWebUA),
|
|
SID: 456,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := req.Encode()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var resp wup.Response
|
|
if err := resp.Decode(raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.Servant != huya.ServantWebActUI || resp.Func != huya.FuncGetUserScore || resp.RequestID != 2 {
|
|
t.Fatalf("unexpected header: %#v", resp)
|
|
}
|
|
if len(resp.NewData["tReq"]) == 0 {
|
|
t.Fatal("missing tReq")
|
|
}
|
|
}
|
|
|
|
// TestResponseDiagnoseIncludesServerFields 验证诊断串会把 WUP 响应里的排障字段带出来。
|
|
//
|
|
// 2026-10-16 的复盘卡在「响应缺少 tRsp/tResp」只有一句文案:服务端回的 iRet、
|
|
// status、context 与数据体键名全被丢掉,同一账号连续 13 次命中也无法定性。
|
|
func TestResponseDiagnoseIncludesServerFields(t *testing.T) {
|
|
resp := &wup.Response{
|
|
Message: -1,
|
|
Servant: "webActUI",
|
|
Func: "scoreExchangePrize",
|
|
Status: map[string]string{"STATUS_RESULT_DESC": "风控拦截", "STATUS_RESULT_CODE": "-1"},
|
|
Context: map[string]string{"uid": "123"},
|
|
NewData: map[string][]byte{"tErr": {0x0c}},
|
|
Buffer: []byte{1, 2, 3},
|
|
}
|
|
got := resp.Diagnose()
|
|
for _, want := range []string{
|
|
"iRet=-1",
|
|
"servant=webActUI.scoreExchangePrize",
|
|
"STATUS_RESULT_CODE=-1",
|
|
"STATUS_RESULT_DESC=风控拦截",
|
|
"context={uid=123}",
|
|
"keys={tErr(1B)}",
|
|
"payload=3B",
|
|
} {
|
|
if !strings.Contains(got, want) {
|
|
t.Fatalf("Diagnose()=%q,缺少 %q", got, want)
|
|
}
|
|
}
|
|
if got := (*wup.Response)(nil).Diagnose(); got != "响应为空" {
|
|
t.Fatalf("空响应诊断=%q", got)
|
|
}
|
|
if got := (&wup.Response{}).Diagnose(); !strings.Contains(got, "iRet=0") || !strings.Contains(got, "keys=空") {
|
|
t.Fatalf("零值响应诊断=%q", got)
|
|
}
|
|
}
|
|
|
|
// TestHexHead 验证原始帧前缀输出的截断与边界。
|
|
func TestHexHead(t *testing.T) {
|
|
data := []byte{0x1f, 0x8b, 0x08, 0x00}
|
|
if got := wup.HexHead(data, 2); got != "1f8b" {
|
|
t.Fatalf("HexHead(2)=%q", got)
|
|
}
|
|
if got := wup.HexHead(data, 32); got != "1f8b0800" {
|
|
t.Fatalf("HexHead 越界应截断到实际长度: %q", got)
|
|
}
|
|
if got := wup.HexHead(nil, 8); got != "" {
|
|
t.Fatalf("空数据=%q", got)
|
|
}
|
|
if got := wup.HexHead(data, 0); got != "" {
|
|
t.Fatalf("长度为 0=%q", got)
|
|
}
|
|
}
|