Harden host deletion and test stability

This commit is contained in:
phamnazage-jpg
2026-05-25 07:30:07 +08:00
parent 916569ccc5
commit 5e76fb20d0
12 changed files with 240 additions and 61 deletions

View File

@@ -5,11 +5,11 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"sub2api-cn-relay-manager/internal/store/sqlite"
"sub2api-cn-relay-manager/internal/testutil"
)
func TestBatchImportHTTP(t *testing.T) {
@@ -139,11 +139,8 @@ func TestBatchImportHTTP(t *testing.T) {
server := httptest.NewServer(newBatchImportActionStubServer(t))
defer server.Close()
dsn := fmt.Sprintf("file:%s?_busy_timeout=5000&_pragma=foreign_keys(0)", filepath.ToSlash(filepath.Join(t.TempDir(), "state.db")))
store, err := sqlite.Open(context.Background(), dsn)
if err != nil {
t.Fatalf("sqlite.Open() error = %v", err)
}
dsn := testutil.SQLiteTestDSN(t, "state.db", true)
store := testutil.OpenSQLiteStore(t, dsn)
defer closeAppTestStore(t, store)
if _, err := store.Hosts().Create(context.Background(), sqlite.Host{
@@ -260,6 +257,24 @@ func TestBatchImportWrapperFunctions(t *testing.T) {
})
}
func TestBatchImportRejectsOversizedJSONBody(t *testing.T) {
t.Parallel()
handler := NewAPIHandler("secret-token", ActionSet{
CreateBatchImportRun: func(_ context.Context, req CreateBatchImportRunRequest) (BatchImportRunCreateResponse, error) {
t.Fatal("CreateBatchImportRun should not be called for oversized body")
return BatchImportRunCreateResponse{}, nil
},
})
payload := `{"host_id":"host-1","mode":"strict","access_mode":"self_service","probe_api_key":"probe-key","entries":[{"base_url":"https://kimi.example.com/v1","api_key":"` + strings.Repeat("x", int(maxJSONBodyBytes)) + `"}]}`
req := httptest.NewRequest(http.MethodPost, "/api/batch-import/runs", strings.NewReader(payload))
req.Header.Set("Authorization", "Bearer secret-token")
res := httptestRecorder(handler, req)
assertStatusCode(t, res, http.StatusRequestEntityTooLarge)
assertJSONContains(t, res.Body().Bytes(), "error.code", "request_too_large")
}
func newBatchImportActionStubServer(t *testing.T) http.Handler {
t.Helper()