Replace fmt/errors/strings/encoding/json/os/os/exec/path/filepath with
core primitives; rename abbreviated variables; add Ugly test variants to
all test files; rename integration tests to TestFilename_Function_{Good,Bad,Ugly}.
Co-Authored-By: Virgil <virgil@lethean.io>
148 lines
3.5 KiB
Go
148 lines
3.5 KiB
Go
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
package api_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
api "dappco.re/go/core/api"
|
|
)
|
|
|
|
// ── Pprof profiling endpoints ─────────────────────────────────────────
|
|
|
|
func TestWithPprof_Good_IndexAccessible(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
e, err := api.New(api.WithPprof())
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
srv := httptest.NewServer(e.Handler())
|
|
defer srv.Close()
|
|
|
|
resp, err := http.Get(srv.URL + "/debug/pprof/")
|
|
if err != nil {
|
|
t.Fatalf("request failed: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("expected 200 for /debug/pprof/, got %d", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestWithPprof_Good_ProfileEndpointExists(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
e, err := api.New(api.WithPprof())
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
srv := httptest.NewServer(e.Handler())
|
|
defer srv.Close()
|
|
|
|
resp, err := http.Get(srv.URL + "/debug/pprof/heap")
|
|
if err != nil {
|
|
t.Fatalf("request failed: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("expected 200 for /debug/pprof/heap, got %d", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestWithPprof_Good_CombinesWithOtherMiddleware(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
e, err := api.New(api.WithRequestID(), api.WithPprof())
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
srv := httptest.NewServer(e.Handler())
|
|
defer srv.Close()
|
|
|
|
resp, err := http.Get(srv.URL + "/debug/pprof/")
|
|
if err != nil {
|
|
t.Fatalf("request failed: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("expected 200 for /debug/pprof/ with middleware, got %d", resp.StatusCode)
|
|
}
|
|
|
|
// Verify the request ID middleware is still active.
|
|
rid := resp.Header.Get("X-Request-ID")
|
|
if rid == "" {
|
|
t.Fatal("expected X-Request-ID header from WithRequestID middleware")
|
|
}
|
|
}
|
|
|
|
func TestWithPprof_Bad_NotMountedWithoutOption(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
e, _ := api.New()
|
|
|
|
h := e.Handler()
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest(http.MethodGet, "/debug/pprof/", nil)
|
|
h.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("expected 404 for /debug/pprof/ without WithPprof, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestWithPprof_Good_CmdlineEndpointExists(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
e, err := api.New(api.WithPprof())
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
srv := httptest.NewServer(e.Handler())
|
|
defer srv.Close()
|
|
|
|
resp, err := http.Get(srv.URL + "/debug/pprof/cmdline")
|
|
if err != nil {
|
|
t.Fatalf("request failed: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("expected 200 for /debug/pprof/cmdline, got %d", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestWithPprof_Ugly_DoubleRegistrationDoesNotPanic(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Fatalf("double WithPprof panicked: %v", r)
|
|
}
|
|
}()
|
|
|
|
// Registering pprof twice should not panic on engine construction.
|
|
engine, err := api.New(api.WithPprof(), api.WithPprof())
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
request, _ := http.NewRequest(http.MethodGet, "/health", nil)
|
|
engine.Handler().ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", recorder.Code)
|
|
}
|
|
}
|