mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-25 00:54:45 +08:00
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
|
|
//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"))
|
||
|
|
}
|