feat(Sora): 直连生成并移除sora2api依赖
实现直连 Sora 客户端、媒体落地与清理策略\n更新网关与前端配置以支持 Sora 平台\n补齐单元测试与契约测试,新增 curl 测试脚本\n\n测试: go test ./... -tags=unit
This commit is contained in:
54
backend/internal/service/sora_client_test.go
Normal file
54
backend/internal/service/sora_client_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
//go:build unit
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSoraDirectClient_DoRequestSuccess(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"ok":true}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &config.Config{
|
||||
Sora: config.SoraConfig{
|
||||
Client: config.SoraClientConfig{BaseURL: server.URL},
|
||||
},
|
||||
}
|
||||
client := NewSoraDirectClient(cfg, nil, nil)
|
||||
|
||||
body, _, err := client.doRequest(context.Background(), &Account{ID: 1}, http.MethodGet, server.URL, http.Header{}, nil, false)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(body), "ok")
|
||||
}
|
||||
|
||||
func TestSoraDirectClient_BuildBaseHeaders(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Sora: config.SoraConfig{
|
||||
Client: config.SoraClientConfig{
|
||||
Headers: map[string]string{
|
||||
"X-Test": "yes",
|
||||
"Authorization": "should-ignore",
|
||||
"openai-sentinel-token": "skip",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
client := NewSoraDirectClient(cfg, nil, nil)
|
||||
|
||||
headers := client.buildBaseHeaders("token-123", "UA")
|
||||
require.Equal(t, "Bearer token-123", headers.Get("Authorization"))
|
||||
require.Equal(t, "UA", headers.Get("User-Agent"))
|
||||
require.Equal(t, "yes", headers.Get("X-Test"))
|
||||
require.Empty(t, headers.Get("openai-sentinel-token"))
|
||||
}
|
||||
Reference in New Issue
Block a user