85 lines
3.1 KiB
Go
85 lines
3.1 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/creator-hub/internal/creator"
|
|
hub "git.ipao.vip/rogee/creator-hub/internal/environment"
|
|
)
|
|
|
|
func TestCreatorGatewayBrowserResolveAndSharePlatformValidation(t *testing.T) {
|
|
environment := hub.EnvironmentContext{Env: hub.Env{Alias: "account-1"}, RuntimeID: "runtime-1", RuntimeNetworkID: "network-1", BindingVersion: 3}
|
|
cases := []struct {
|
|
name string
|
|
status int
|
|
body string
|
|
wantURL string
|
|
wantErr bool
|
|
}{
|
|
{name: "success", status: http.StatusOK, body: `{"url":"https://www.douyin.com/video/1"}`, wantURL: "https://www.douyin.com/video/1"},
|
|
{name: "invalid json", status: http.StatusOK, body: `{`, wantErr: true},
|
|
{name: "empty url", status: http.StatusOK, body: `{"url":""}`, wantErr: true},
|
|
{name: "rejected", status: http.StatusBadGateway, body: `gateway down`, wantErr: true},
|
|
}
|
|
for _, testCase := range cases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/browsers/account-1/douyin/resolve" {
|
|
t.Fatalf("resolve path = %q", r.URL.Path)
|
|
}
|
|
w.WriteHeader(testCase.status)
|
|
_, _ = w.Write([]byte(testCase.body))
|
|
}))
|
|
defer server.Close()
|
|
got, err := (creatorGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL}, environment: environment}).Resolve(context.Background(), "https://www.douyin.com/video/1")
|
|
if (err != nil) != testCase.wantErr || got != testCase.wantURL {
|
|
t.Fatalf("resolve = %q, err = %v", got, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
for _, testCase := range []struct {
|
|
value string
|
|
platform string
|
|
wantErr bool
|
|
}{
|
|
{value: "https://www.douyin.com/video/1", platform: creator.PlatformDouyin},
|
|
{value: "https://v.douyin.com/abc", platform: creator.PlatformDouyin},
|
|
{value: "https://www.xiaohongshu.com/explore/1", platform: creator.PlatformXiaohongshu},
|
|
{value: "https://xhslink.com/abc", platform: creator.PlatformXiaohongshu},
|
|
{value: "http://www.douyin.com/video/1", wantErr: true},
|
|
{value: "https://user@www.douyin.com/video/1", wantErr: true},
|
|
{value: "https://www.douyin.com:443/video/1", wantErr: true},
|
|
{value: "https://www.douyin.com/video/1#part", wantErr: true},
|
|
{value: "https://example.com/video/1", wantErr: true},
|
|
} {
|
|
platform, err := competitorSharePlatform(testCase.value)
|
|
if testCase.wantErr {
|
|
if !errors.Is(err, creator.ErrInvalid) {
|
|
t.Errorf("competitorSharePlatform(%q) error = %v", testCase.value, err)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil || platform != testCase.platform {
|
|
t.Errorf("competitorSharePlatform(%q) = %q, %v", testCase.value, platform, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRebindRecoveryErrorPreservesFailureStates(t *testing.T) {
|
|
sentinel := errors.New("rebind failed")
|
|
if got := rebindRecoveryError(true, sentinel); !errors.Is(got, sentinel) {
|
|
t.Fatalf("existing error = %v", got)
|
|
}
|
|
if got := rebindRecoveryError(false, nil); got == nil {
|
|
t.Fatal("incomplete recovery was reported as successful")
|
|
}
|
|
if got := rebindRecoveryError(true, nil); got != nil {
|
|
t.Fatalf("complete recovery error = %v", got)
|
|
}
|
|
}
|