@@ -0,0 +1,61 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"data-terminal-cloud/internal/orchestration"
|
||||
"data-terminal-cloud/internal/volcsubtitle"
|
||||
)
|
||||
|
||||
type subtitleTestProcessor struct {
|
||||
calls int
|
||||
err error
|
||||
}
|
||||
|
||||
func (p *subtitleTestProcessor) ProbeNativeSubtitleDevice(_ context.Context, _ volcsubtitle.Packet) error {
|
||||
p.calls++
|
||||
return p.err
|
||||
}
|
||||
|
||||
func (p *subtitleTestProcessor) ProcessNativeSubtitle(_ context.Context, _ string, _ volcsubtitle.Packet) (orchestration.NativeSubtitleResult, error) {
|
||||
p.calls++
|
||||
return orchestration.NativeSubtitleResult{Mode: "probe"}, p.err
|
||||
}
|
||||
|
||||
func TestNativeSubtitleHTTPAuthenticationAndFraming(t *testing.T) {
|
||||
secret := []byte("test-native-subtitle-secret-32-bytes")
|
||||
id := "de4e5d8a-708e-4e9b-b565-9265a9b6873a"
|
||||
payload := []byte(`{"type":"subtitle","data":[{"userId":"test-device","roundId":0,"sequence":0,"definite":true,"text":"测试"}]}`)
|
||||
frame := append(make([]byte, 8), payload...)
|
||||
copy(frame, "subv")
|
||||
binary.BigEndian.PutUint32(frame[4:8], uint32(len(payload)))
|
||||
for _, tc := range []struct {
|
||||
name, id, sig, message string
|
||||
status int
|
||||
calls int
|
||||
}{
|
||||
{"no Content-Type", id, volcsubtitle.Signature(secret, id), base64.StdEncoding.EncodeToString(frame), 200, 1},
|
||||
{"another conversation", id, volcsubtitle.Signature(secret, "other"), base64.StdEncoding.EncodeToString(frame), 401, 0},
|
||||
{"missing conversation", "", string(secret), base64.StdEncoding.EncodeToString(frame), 400, 0},
|
||||
{"invalid frame", id, volcsubtitle.Signature(secret, id), "bad", 400, 0},
|
||||
{"hardware device probe", "", string(secret), base64.StdEncoding.EncodeToString([]byte(`{"type":"subtitle","productKey":"product","deviceName":"device","data":[{"userId":"input","roundId":1,"sequence":0,"definite":true,"text":"test"}]}`)), 200, 1},
|
||||
{"hardware invalid signature", "", "wrong", base64.StdEncoding.EncodeToString([]byte(`{"type":"subtitle","productKey":"product","deviceName":"device","data":[{"userId":"input","roundId":1,"sequence":0,"definite":true,"text":"test"}]}`)), 401, 0},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := &subtitleTestProcessor{}
|
||||
body, _ := json.Marshal(volcsubtitle.Envelope{Message: tc.message, Signature: tc.sig})
|
||||
req := httptest.NewRequest("POST", "/callbacks/v1/volcengine/subtitles?conversation_id="+tc.id, strings.NewReader(string(body)))
|
||||
w := httptest.NewRecorder()
|
||||
NativeSubtitleCallbackHandler(secret, p).ServeHTTP(w, req)
|
||||
if w.Code != tc.status || p.calls != tc.calls {
|
||||
t.Fatalf("status=%d calls=%d body=%s", w.Code, p.calls, w.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user