// Package tests provides HTTP client helpers for manually testing ppanel-server APIs. // // Usage: // // go test -v -run TestXxx ./tests/ // // Configure the constants below to match your local environment. package tests import ( "bytes" "encoding/json" "fmt" "io" "net/http" "testing" ) // ─── Environment Configuration ──────────────────────────────────────────────── const ( // BaseURL is the ppanel-server base URL (no trailing slash). BaseURL = "http://localhost:8080" // AdminToken is the JWT token for an admin user. // Obtain by calling CallAdminLogin first, then paste the token here. AdminToken = "YOUR_ADMIN_TOKEN" // UserToken is the JWT token for a regular user. // Obtain by calling CallUserLogin first, then paste the token here. UserToken = "YOUR_USER_TOKEN" // TestEmail is the email address for user-related test calls. TestEmail = "test@example.com" // TestPassword is the password for test user login. TestPassword = "Test@123456" // AdminEmail is the email for admin login. AdminEmail = "admin@example.com" // AdminPassword is the admin password. AdminPassword = "Admin@123456" ) // ─── Request Options ────────────────────────────────────────────────────────── type reqOption func(*http.Request) // withToken adds a Bearer token to the request. func withToken(token string) reqOption { return func(r *http.Request) { r.Header.Set("Authorization", "Bearer "+token) } } // withAdminToken adds the AdminToken. func withAdminToken() reqOption { return withToken(AdminToken) } // withUserToken adds the UserToken. func withUserToken() reqOption { return withToken(UserToken) } // ─── Core HTTP Helper ───────────────────────────────────────────────────────── // doRequest sends an HTTP request and prints the response. // body may be nil (for GET/DELETE without body), a map[string]any, or any JSON-serialisable value. // Returns the raw response body as a string. func doRequest(t *testing.T, method, path string, body any, opts ...reqOption) string { t.Helper() var reqBody io.Reader if body != nil { b, err := json.Marshal(body) if err != nil { t.Fatalf("marshal body: %v", err) } reqBody = bytes.NewReader(b) } req, err := http.NewRequest(method, BaseURL+path, reqBody) if err != nil { t.Fatalf("new request: %v", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") for _, o := range opts { o(req) } resp, err := http.DefaultClient.Do(req) if err != nil { t.Fatalf("do request: %v", err) } defer resp.Body.Close() raw, _ := io.ReadAll(resp.Body) result := prettyJSON(raw) t.Logf("[%s %s] %d\n%s", method, path, resp.StatusCode, result) return string(raw) } // doRequestRaw is like doRequest but for non-testing contexts (prints to stdout). func doRequestRaw(method, path string, body any, opts ...reqOption) string { var reqBody io.Reader if body != nil { b, _ := json.Marshal(body) reqBody = bytes.NewReader(b) } req, _ := http.NewRequest(method, BaseURL+path, reqBody) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") for _, o := range opts { o(req) } resp, err := http.DefaultClient.Do(req) if err != nil { fmt.Printf("request error: %v\n", err) return "" } defer resp.Body.Close() raw, _ := io.ReadAll(resp.Body) result := prettyJSON(raw) fmt.Printf("[%s %s] %d\n%s\n", method, path, resp.StatusCode, result) return string(raw) } func prettyJSON(raw []byte) string { var buf bytes.Buffer if err := json.Indent(&buf, raw, "", " "); err != nil { return string(raw) } return buf.String() }