Files
creator-hub/internal/controlplane/api/creator_route_validation_test.go
2026-09-21 16:44:09 +08:00

88 lines
3.4 KiB
Go

package api
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gofiber/fiber/v3"
)
func TestCreatorUpdatesStreamStopsOnRequestCancellation(t *testing.T) {
app := fiber.New()
registerCreatorWithServices(app, nil, nil, nil, nil, nil, nil)
request := httptest.NewRequest(http.MethodGet, "/api/creator/updates", http.NoBody)
response, err := app.Test(request, fiber.TestConfig{
Timeout: 200 * time.Millisecond,
FailOnTimeout: false,
})
if err != nil {
t.Fatal(err)
}
defer response.Body.Close()
body, readErr := io.ReadAll(response.Body)
if readErr != nil && !strings.Contains(readErr.Error(), "unexpected EOF") {
t.Fatalf("read stream response: %v", readErr)
}
if response.StatusCode != http.StatusOK {
t.Fatalf("stream status = %d", response.StatusCode)
}
if !strings.Contains(string(body), "retry: 5000") {
t.Fatalf("stream body = %q", body)
}
}
func TestCreatorWriteRoutesRejectMalformedInputBeforeStoreAccess(t *testing.T) {
app := fiber.New()
registerCreatorWithServices(app, nil, nil, nil, nil, nil, nil)
routes := []struct {
method string
path string
}{
{method: http.MethodPut, path: "/api/creator/settings"},
{method: http.MethodPut, path: "/api/creator/accounts/account-1/profile"},
{method: http.MethodPut, path: "/api/creator/accounts/account-1/tags"},
{method: http.MethodPost, path: "/api/creator/accounts/account-1/login-result"},
{method: http.MethodPost, path: "/api/creator/accounts/account-1/big-account"},
{method: http.MethodPost, path: "/api/creator/relations"},
{method: http.MethodPost, path: "/api/creator/accounts/account-1/strategies"},
{method: http.MethodPut, path: "/api/creator/strategies/strategy-1"},
{method: http.MethodPost, path: "/api/creator/competitor-share-jobs"},
{method: http.MethodPut, path: "/api/creator/competitors/competitor-1"},
{method: http.MethodPost, path: "/api/creator/competitors/competitor-1/sync"},
{method: http.MethodPost, path: "/api/creator/xiaohongshu/search"},
{method: http.MethodPost, path: "/api/creator/xiaohongshu/detail"},
{method: http.MethodPost, path: "/api/creator/test/works"},
{method: http.MethodPost, path: "/api/creator/works/work-1/metrics"},
{method: http.MethodPost, path: "/api/creator/works/work-1/material/rewrite/confirm"},
{method: http.MethodPut, path: "/api/creator/works/work-1/material/rewrite"},
{method: http.MethodPost, path: "/api/creator/test/comments"},
{method: http.MethodPost, path: "/api/creator/rules"},
{method: http.MethodPut, path: "/api/creator/rules/rule-1"},
{method: http.MethodPost, path: "/api/creator/comments/analyze"},
{method: http.MethodPost, path: "/api/creator/comments/comment-1/analyze"},
{method: http.MethodPost, path: "/api/creator/test/events"},
{method: http.MethodPost, path: "/api/creator/events/process"},
{method: http.MethodPost, path: "/api/creator/messages"},
}
for _, route := range routes {
route := route
t.Run(route.method+" "+route.path, func(t *testing.T) {
request := httptest.NewRequest(route.method, route.path, strings.NewReader("{"))
request.Header.Set("Content-Type", "application/json")
response, err := app.Test(request)
if err != nil {
t.Fatal(err)
}
if response.StatusCode != http.StatusBadRequest && response.StatusCode != http.StatusConflict {
t.Fatalf("malformed request status = %d", response.StatusCode)
}
})
}
}