89 lines
4.3 KiB
Go
89 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/creator-hub/internal/creator"
|
|
"git.ipao.vip/rogee/creator-hub/internal/hub"
|
|
"git.ipao.vip/rogee/creator-hub/internal/xiaohongshu"
|
|
)
|
|
|
|
const testXiaohongshuIdentityURL = "https://edith.xiaohongshu.com/api/sns/web/v2/user/me"
|
|
|
|
func TestXiaohongshuGatewayBrowserFencesAccountGeneration(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
if request.Header.Get("Authorization") != "Bearer gateway-token-1" {
|
|
t.Fatal("missing gateway authorization")
|
|
}
|
|
var body map[string]any
|
|
if json.NewDecoder(request.Body).Decode(&body) != nil || body["binding_version"] != float64(2) || body["runtime_id"] != "runtime-a" || body["network_id"] != "network-a" || body["network_exit_id"] != "exit-a" {
|
|
t.Fatalf("generation fence missing: %#v", body)
|
|
}
|
|
if request.URL.Path != "/v1/browsers/account-a/xiaohongshu/get" || body["url"] != testXiaohongshuIdentityURL {
|
|
t.Fatalf("unexpected request: path=%s body=%#v", request.URL.Path, body)
|
|
}
|
|
_ = json.NewEncoder(response).Encode(map[string]any{"status": 200, "body": `{"success":true}`, "challenge": ""})
|
|
}))
|
|
defer server.Close()
|
|
browser := xiaohongshuGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()}
|
|
result, err := browser.Get(context.Background(), testXiaohongshuIdentityURL)
|
|
if err != nil || result.Status != 200 || string(result.Body) != `{"success":true}` {
|
|
t.Fatalf("unexpected result: %#v err=%v", result, err)
|
|
}
|
|
}
|
|
|
|
func TestXiaohongshuGatewayBrowserResolvesShareLinks(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
if request.URL.Path != "/v1/browsers/account-a/xiaohongshu/resolve" {
|
|
t.Fatalf("unexpected path: %s", request.URL.Path)
|
|
}
|
|
_ = json.NewEncoder(response).Encode(map[string]any{"url": "https://www.xiaohongshu.com/explore/n-1"})
|
|
}))
|
|
defer server.Close()
|
|
browser := xiaohongshuGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()}
|
|
resolved, err := browser.Resolve(context.Background(), "https://xhslink.com/a/abc")
|
|
if err != nil || resolved != "https://www.xiaohongshu.com/explore/n-1" {
|
|
t.Fatalf("resolved URL=%q err=%v", resolved, err)
|
|
}
|
|
}
|
|
|
|
func TestNewXiaohongshuCollectorKeepsViewerAndTargetSeparate(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
if request.URL.Path != "/v1/browsers/account-a/xiaohongshu/identity" {
|
|
t.Fatalf("unexpected path: %s", request.URL.Path)
|
|
}
|
|
_ = json.NewEncoder(response).Encode(map[string]any{"uid": "viewer-1"})
|
|
}))
|
|
defer server.Close()
|
|
collector, target, err := newCreatorCollector(context.Background(), creator.PlatformXiaohongshu, hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, readyDouyinEnvironment(), "viewer-1", "target-1", "https://www.xiaohongshu.com/user/profile/target-1?xsec_source=pc_search", creator.SourceCompetitor, "source-1")
|
|
if err != nil {
|
|
t.Fatalf("new collector: %v", err)
|
|
}
|
|
xhsCollector, ok := collector.(*xiaohongshu.Collector)
|
|
if !ok || xhsCollector.AccountKey != "target-1" || target != "target-1" || xhsCollector.HomepageURL == "" {
|
|
t.Fatalf("collector=%#v target=%q", collector, target)
|
|
}
|
|
}
|
|
|
|
func TestXiaohongshuGatewayBrowserPostCarriesJSONBody(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
var body map[string]any
|
|
if json.NewDecoder(request.Body).Decode(&body) != nil || body["url"] != "https://so.xiaohongshu.com/api/sns/web/v2/search/notes" {
|
|
t.Fatalf("unexpected request body: %#v", body)
|
|
}
|
|
if _, ok := body["body"].(map[string]any); !ok {
|
|
t.Fatalf("missing nested request body: %#v", body)
|
|
}
|
|
_ = json.NewEncoder(response).Encode(map[string]any{"status": 200, "body": `{"success":true}`, "challenge": ""})
|
|
}))
|
|
defer server.Close()
|
|
browser := xiaohongshuGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()}
|
|
if _, err := browser.Post(context.Background(), "https://so.xiaohongshu.com/api/sns/web/v2/search/notes", []byte(`{"keyword":"x"}`)); err != nil {
|
|
t.Fatalf("post failed: %v", err)
|
|
}
|
|
}
|