devops/feishu/unit_test.go
2025-04-02 10:26:57 +08:00

92 lines
1.9 KiB
Go

package main
import (
"bufio"
"encoding/json"
"feishu/client"
"feishu/data"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"testing"
)
type message struct {
Event string `json:"event"`
Conversation_id string `json:"conversation_id"`
Message_id string `json:"message_id"`
Create_at int `json:"create_at"`
Task_id string `json:"task_id"`
Id string `json:"id"`
Answer string `json:"answer"`
}
func TestCard(t *testing.T) {
data, err := data.GetOperation()
if err != nil {
t.Fatalf("GetOperation error: %v", err)
}
err = client.SendMsg(data)
if err != nil {
t.Fatalf("SendMsg error: %v", err)
}
}
// ...existing code...
func TestHTTPPost(t *testing.T) {
body := `{
"inputs": {},
"query": "游戏订单提交失败",
"response_mode": "streaming",
"conversation_id": "",
"user": "abc-123",
"files": []
}`
req, err := http.NewRequest("POST", "http://8.155.13.51/v1/chat-messages", strings.NewReader(body))
if err != nil {
t.Fatalf("创建请求失败: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer app-14eZWBFFIyUSZi7OYrnOGhSR")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
// handle error
}
defer resp.Body.Close()
reader := bufio.NewReader(resp.Body)
var str string
for {
var message message
line, readErr := reader.ReadString('\n')
if readErr != nil {
if readErr == io.EOF {
break
}
t.Fatalf("读取响应时出错: %v", readErr)
}
newLine := strings.TrimPrefix(line, "data:")
// 解析 JSON 数据
if err := json.Unmarshal([]byte(newLine), &message); err != nil {
continue
}
if message.Event == "message" {
str += message.Answer
}
// 处理数据
//fmt.Print("收到数据: ", line)
}
r := regexp.MustCompile(`<think>[\s\S]*?<\/think>`)
str = r.ReplaceAllString(str, "")
fmt.Print("收到数据: ", str)
}