This commit is contained in:
2026-07-25 13:56:09 +08:00
parent 14d96fabbe
commit 6d2539804e
2 changed files with 90 additions and 3 deletions

View File

@@ -1,6 +1,9 @@
package jiyi
import "testing"
import (
"encoding/json"
"testing"
)
func TestIsEmptyBusinessResult(t *testing.T) {
cases := []struct {
@@ -43,6 +46,75 @@ func TestIsEmptyBusinessResult(t *testing.T) {
}
}
func TestUnwrapResultPayload(t *testing.T) {
wuj := map[string]interface{}{
"result": map[string]interface{}{
"black_list": "1",
"black_tag04": "1",
},
}
got := unwrapResultPayload(wuj).(map[string]interface{})
if got["black_list"] != "1" || got["black_tag04"] != "1" {
t.Fatalf("wuj unwrap mismatch: %+v", got)
}
if _, ok := got["result"]; ok {
t.Fatal("should not keep result wrapper")
}
cave := map[string]interface{}{
"result": map[string]interface{}{
"score": "617",
"reason": "",
"contents": map[string]interface{}{
"TC_A001": "0",
},
},
}
got2 := unwrapResultPayload(cave).(map[string]interface{})
if got2["score"] != "617" {
t.Fatalf("cave unwrap mismatch: %+v", got2)
}
pano := map[string]interface{}{
"result": map[string]interface{}{
"apply_report_detail": map[string]interface{}{"A22160001": "543"},
"behavior_report_detail": nil,
"current_report_detail": nil,
},
}
got3 := unwrapResultPayload(pano).(map[string]interface{})
if _, ok := got3["apply_report_detail"]; !ok {
t.Fatalf("pano unwrap mismatch: %+v", got3)
}
plain := map[string]interface{}{"x": "1"}
if unwrapResultPayload(plain).(map[string]interface{})["x"] != "1" {
t.Fatal("plain payload should keep")
}
}
func TestStripAndUnwrapPipeline(t *testing.T) {
raw := []byte(`{"busiCode":10,"busiMsg":"查询成功","result":{"black_list":"1","black_tag07":"1"}}`)
var data interface{}
if err := json.Unmarshal(raw, &data); err != nil {
t.Fatal(err)
}
cleaned, err := stripBusiMetaFromData(data)
if err != nil {
t.Fatal(err)
}
out := unwrapResultPayload(cleaned).(map[string]interface{})
if _, ok := out["busiCode"]; ok {
t.Fatal("busiCode should be stripped")
}
if _, ok := out["result"]; ok {
t.Fatal("result wrapper should be unwrapped")
}
if out["black_list"] != "1" || out["black_tag07"] != "1" {
t.Fatalf("unexpected: %+v", out)
}
}
func TestIsQueryBillingAPIKey(t *testing.T) {
if !isQueryBillingAPIKey("jy000022") || !isQueryBillingAPIKey("jy000042") || !isQueryBillingAPIKey("jy000052") {
t.Fatal("查空计费产品应在白名单中")
@@ -51,6 +123,6 @@ func TestIsQueryBillingAPIKey(t *testing.T) {
t.Fatal("信用司南查空不计费,不应在白名单中")
}
if isQueryBillingAPIKey("jy000002") || isQueryBillingAPIKey("jy000003") {
t.Fatal("全景雷达PD查空不计费,不应在白名单中")
t.Fatal("全景雷达BH查空不计费,不应在白名单中")
}
}

View File

@@ -246,7 +246,8 @@ func (s *JiyiService) CallAPI(ctx context.Context, apiKey, apiPath string, body
s.logError(transactionID, apiKey, jiyiResp.SeqNo, err, requestPayload)
return nil, err
}
jiyiResp.Data = cleanedData
// 去掉 data.result 包装,直接返回最内层业务数据
jiyiResp.Data = unwrapResultPayload(cleanedData)
s.logResponse(transactionID, apiKey, resp.StatusCode, duration, jiyiResp.SeqNo)
@@ -287,6 +288,20 @@ func stripBusiMetaFromData(data interface{}) (interface{}, error) {
return payload, nil
}
// unwrapResultPayload 若载荷为 {"result": <业务数据>},则直接返回业务数据。
// 五个集奕产品实测成功响应均为此结构。
func unwrapResultPayload(data interface{}) interface{} {
payload, ok := data.(map[string]interface{})
if !ok {
return data
}
result, exists := payload["result"]
if !exists {
return data
}
return result
}
// isEmptyBusinessResult 判断业务载荷是否为空(无 result或 result 无有效内容)
func isEmptyBusinessResult(data interface{}) bool {
payload, ok := data.(map[string]interface{})