- plugins/channels/kook: config/adapter/gateway/websocket/send/message/errors — 实现 PlatformAdapter 13方法 - WS 有状态网关: 取gateway地址→连WS→信令0/1/2/3/4/6 + 30s心跳/6s超时 + resume(拼接sn/session_id) + 指数退避重连 - 收事件: 信令0 → UnifiedMessage(word注 channel_type/type/target_id/extra.author→字段) - 发消息: /message/create(频道) + /direct-message/create(私聊,修正正确端点,非user-chat) + Bearer鉴权 - 注册 channels.go + CHANNELS_JSON 多账号 + channelbridge.configured 加 kook(bot_token非空才连) - 框架增强: internal/channel/adapter/wsutil(连接管理/心跳/重连/resume骨架,复用gorilla,KOOK用上) + httpclient(Bearer+envelope归一,KOOK用) - 全仓 go build/vet/test 绿, 零新依赖(复用餐gorilla), 未动telegram/qq/weixin/webhook契约 - §4.6 隔离/黑白名单/第三方边界未破坏 注: KOOK WS/WH 双模式待补(ReceiveMode), 实测需主人提供 CHANNEL_KOOK_TOKEN
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
package httpclient
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"sync"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/logger"
|
|
)
|
|
|
|
func discardLogger() *slog.Logger { return logger.New(logger.Options{Out: io.Discard}) }
|
|
|
|
func TestGetJSON_EnvelopeAndAuth(t *testing.T) {
|
|
t.Parallel()
|
|
var mu sync.Mutex
|
|
var auth string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
auth = r.Header.Get("Authorization")
|
|
mu.Unlock()
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"code": 0, "message": "ok",
|
|
"data": map[string]any{"url": "wss://gw.example.com/gateway"},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := New(
|
|
WithBaseURL(srv.URL),
|
|
WithToken("secret-token"),
|
|
WithHTTPClient(srv.Client()),
|
|
WithLogger(discardLogger()),
|
|
)
|
|
data, err := c.GetJSON(context.Background(), "/gateway/index", url.Values{"compress": {"0"}})
|
|
if err != nil {
|
|
t.Fatalf("GetJSON: %v", err)
|
|
}
|
|
if string(data) != `{"url":"wss://gw.example.com/gateway"}` {
|
|
t.Errorf("data = %s", string(data))
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if auth != "Bearer secret-token" {
|
|
t.Errorf("Authorization = %q, want Bearer secret-token", auth)
|
|
}
|
|
}
|
|
|
|
func TestDoJSON_NonZeroCode(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"code": 40101, "message": "invalid token", "data": struct{}{}})
|
|
}))
|
|
defer srv.Close()
|
|
c := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()), WithLogger(discardLogger()))
|
|
_, err := c.DoJSON(context.Background(), http.MethodPost, "/message/create", map[string]any{"a": 1})
|
|
if !errors.Is(err, ErrAPI) {
|
|
t.Fatalf("err = %v, want ErrAPI", err)
|
|
}
|
|
}
|
|
|
|
func TestDoJSON_StatusError(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
_, _ = w.Write([]byte("unauthorized"))
|
|
}))
|
|
defer srv.Close()
|
|
c := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()), WithLogger(discardLogger()))
|
|
_, err := c.DoJSON(context.Background(), http.MethodGet, "/gateway/index", nil)
|
|
if !errors.Is(err, ErrAPI) {
|
|
t.Fatalf("err = %v, want ErrAPI", err)
|
|
}
|
|
}
|
|
|
|
func TestGetJSON_NonEnvelopeBodyPassedThrough(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`[1,2,3]`))
|
|
}))
|
|
defer srv.Close()
|
|
c := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()), WithLogger(discardLogger()))
|
|
data, err := c.GetJSON(context.Background(), "/list", nil)
|
|
if err != nil {
|
|
t.Fatalf("GetJSON: %v", err)
|
|
}
|
|
if string(data) != "[1,2,3]" {
|
|
t.Errorf("data = %s, want [1,2,3]", string(data))
|
|
}
|
|
}
|