fix: validate card business responses and trim help
This commit is contained in:
@@ -305,9 +305,25 @@ func (s *CardSender) postCard(ctx context.Context, body map[string]any, token st
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Discard successful response body
|
return validateDingTalkBusinessResponse(payload)
|
||||||
if len(payload) > 0 {
|
}
|
||||||
// Body is already bounded by postJSON→readBoundedAPIResponse; just discard
|
|
||||||
|
func validateDingTalkBusinessResponse(payload []byte) error {
|
||||||
|
if len(bytes.TrimSpace(payload)) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var response struct {
|
||||||
|
Success *bool `json:"success"`
|
||||||
|
ErrCode any `json:"errcode"`
|
||||||
|
}
|
||||||
|
if json.Unmarshal(payload, &response) != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if response.Success != nil && !*response.Success {
|
||||||
|
return errors.New("dingtalk card: API business failure")
|
||||||
|
}
|
||||||
|
if response.ErrCode != nil && fmt.Sprint(response.ErrCode) != "0" {
|
||||||
|
return errors.New("dingtalk card: API business failure")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -326,8 +342,11 @@ func (s *CardSender) finalizeCard(ctx context.Context, outTrackID, content, toke
|
|||||||
"isFinalize": isFinalize,
|
"isFinalize": isFinalize,
|
||||||
"isError": isError,
|
"isError": isError,
|
||||||
}
|
}
|
||||||
_, err = s.doJSON(ctx, http.MethodPut, s.streamURL, body, token)
|
payload, err := s.doJSON(ctx, http.MethodPut, s.streamURL, body, token)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return validateDingTalkBusinessResponse(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CardSender) postJSON(ctx context.Context, endpoint string, value any, token string) ([]byte, error) {
|
func (s *CardSender) postJSON(ctx context.Context, endpoint string, value any, token string) ([]byte, error) {
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ type cardTestServer struct {
|
|||||||
createBodySize int64 // Content-Length for oversized response
|
createBodySize int64 // Content-Length for oversized response
|
||||||
streamStatus int
|
streamStatus int
|
||||||
streamBodySize int64
|
streamBodySize int64
|
||||||
|
streamResponse any
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCardTestServer() *cardTestServer {
|
func newCardTestServer() *cardTestServer {
|
||||||
@@ -42,6 +43,7 @@ func newCardTestServer() *cardTestServer {
|
|||||||
tokenStatus: http.StatusOK,
|
tokenStatus: http.StatusOK,
|
||||||
createStatus: http.StatusOK,
|
createStatus: http.StatusOK,
|
||||||
streamStatus: http.StatusOK,
|
streamStatus: http.StatusOK,
|
||||||
|
streamResponse: map[string]any{"result": "ok"},
|
||||||
tokenResponse: map[string]any{"accessToken": "test-access-token", "expireIn": 3600},
|
tokenResponse: map[string]any{"accessToken": "test-access-token", "expireIn": 3600},
|
||||||
createResponse: map[string]any{"result": "ok"},
|
createResponse: map[string]any{"result": "ok"},
|
||||||
}
|
}
|
||||||
@@ -91,7 +93,7 @@ func newCardTestServer() *cardTestServer {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(map[string]any{"result": "ok"})
|
json.NewEncoder(w).Encode(ts.streamResponse)
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
@@ -969,6 +971,19 @@ func TestSendFinalCardCreateStreamOnce(t *testing.T) {
|
|||||||
// Test: UpdateCard with non-final / error flags
|
// Test: UpdateCard with non-final / error flags
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestUpdateCardRejectsHTTP200BusinessFailure(t *testing.T) {
|
||||||
|
ts := newCardTestServer()
|
||||||
|
defer ts.Close()
|
||||||
|
ts.streamResponse = map[string]any{"success": false, "code": "InvalidParameter", "message": "sensitive detail"}
|
||||||
|
sender, err := newCardSenderForTest("client-id", "client-secret", "card-template-123", ts.Client(), ts.URL()+"/token", ts.URL()+"/create")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := sender.UpdateCard(context.Background(), "track-1", "answer", true, false); err == nil {
|
||||||
|
t.Fatal("HTTP 200 business failure must be rejected")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUpdateCardNonFinalAndError(t *testing.T) {
|
func TestUpdateCardNonFinalAndError(t *testing.T) {
|
||||||
ts := newCardTestServer()
|
ts := newCardTestServer()
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func (c *Controller) Handle(ctx context.Context, in SettingInput) (string, error
|
|||||||
args = []string{action, strings.TrimSpace(in.Text)}
|
args = []string{action, strings.TrimSpace(in.Text)}
|
||||||
}
|
}
|
||||||
if explicit && cmd.Name == "/help" {
|
if explicit && cmd.Name == "/help" {
|
||||||
return "📖 使用帮助:\n• 直接发送问题:普通提问\n• /setting:查看个人设置\n• /setting language <auto|zh-CN|en|ja|ko|es>:设置回答语言\n• /help:查看本帮助", nil
|
return "设置回答语言:/setting language <auto|zh-CN|en|ja|ko|es>\n例如:/setting language en", nil
|
||||||
}
|
}
|
||||||
if explicit && len(args) == 1 && len(args[0]) == 6 && allDigits(args[0]) {
|
if explicit && len(args) == 1 && len(args[0]) == 6 && allDigits(args[0]) {
|
||||||
if strings.TrimSpace(in.ConversationType) != "direct" {
|
if strings.TrimSpace(in.ConversationType) != "direct" {
|
||||||
|
|||||||
@@ -90,10 +90,9 @@ func TestControllerHelpIsHandledLocally(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, want := range []string{"普通提问", "/setting", "/help"} {
|
want := "设置回答语言:/setting language <auto|zh-CN|en|ja|ko|es>\n例如:/setting language en"
|
||||||
if !strings.Contains(out, want) {
|
if out != want {
|
||||||
t.Fatalf("help=%q missing %q", out, want)
|
t.Fatalf("help=%q want %q", out, want)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user