54 lines
2.5 KiB
Go
54 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/creator-hub/internal/hub"
|
|
)
|
|
|
|
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 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)
|
|
}
|
|
}
|