106 lines
4.0 KiB
Go
106 lines
4.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestQueryBudgetExcludesLongLivedAndStreamingRequests(t *testing.T) {
|
|
cases := []struct {
|
|
method string
|
|
path string
|
|
route string
|
|
want time.Duration
|
|
}{
|
|
{method: http.MethodGet, path: "/api/orders", route: "/api/orders", want: defaultAPIQueryBudget},
|
|
{method: http.MethodGet, path: "/api/admin/dashboard", route: "/api/admin/dashboard", want: 8 * time.Second},
|
|
{method: http.MethodGet, path: "/api/admin/chats", route: "/api/admin/chats", want: 8 * time.Second},
|
|
{method: http.MethodPost, path: "/api/files/access/batch", route: "/api/files/access/batch", want: 5 * time.Second},
|
|
{method: http.MethodPost, path: "/api/payments/lakala/notify", route: "/api/payments/lakala/notify", want: 30 * time.Second},
|
|
{method: http.MethodPost, path: "/api/admin/chats/qrcodes/ocr-group-name", route: "/api/admin/chats/qrcodes/ocr-group-name", want: 60 * time.Second},
|
|
{method: http.MethodPost, path: "/api/open/listing-uploads", route: "/api/open/listing-uploads", want: 60 * time.Second},
|
|
{method: http.MethodPost, path: "/api/admin/payment-configs/1/test-orders", route: "/api/admin/payment-configs/:id/test-orders", want: 30 * time.Second},
|
|
{method: http.MethodGet, path: "/api/files/object", route: "/api/files/object", want: 0},
|
|
{method: http.MethodPost, path: "/api/admin/files/upload", route: "/api/admin/files/upload", want: 0},
|
|
{method: http.MethodGet, path: "/api/chats/events", route: "/api/chats/events", want: 0},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := queryBudgetForRequest(tc.method, tc.path, tc.route); got != tc.want {
|
|
t.Fatalf("queryBudgetForRequest(%s, %s) = %s, want %s", tc.method, tc.path, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHighCostAdminReadClassification(t *testing.T) {
|
|
cases := []struct {
|
|
path string
|
|
route string
|
|
want bool
|
|
}{
|
|
{path: "/api/admin/dashboard", route: "/api/admin/dashboard", want: true},
|
|
{path: "/api/admin/chats/counts", route: "/api/admin/chats/counts", want: true},
|
|
{path: "/api/admin/chats/events", route: "/api/admin/chats/events", want: false},
|
|
{path: "/api/admin/finance/details", route: "/api/admin/finance/details", want: true},
|
|
{path: "/api/admin/orders/123", route: "/api/admin/orders/:id", want: true},
|
|
{path: "/api/admin/users", route: "/api/admin/users", want: false},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := isHighCostAdminRead(tc.path, tc.route); got != tc.want {
|
|
t.Fatalf("isHighCostAdminRead(%s) = %v, want %v", tc.path, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTenantConcurrencyLimiterRejectsWhenSaturated(t *testing.T) {
|
|
limiter := &tenantConcurrencyLimiter{
|
|
sites: make(map[uint64]*tenantConcurrencyState),
|
|
limit: 1,
|
|
wait: 10 * time.Millisecond,
|
|
}
|
|
release, ok := limiter.acquire(context.Background(), 101)
|
|
if !ok {
|
|
t.Fatal("first acquire failed")
|
|
}
|
|
if _, ok := limiter.acquire(context.Background(), 101); ok {
|
|
t.Fatal("second acquire unexpectedly succeeded")
|
|
}
|
|
release()
|
|
releaseAgain, ok := limiter.acquire(context.Background(), 101)
|
|
if !ok {
|
|
t.Fatal("acquire after release failed")
|
|
}
|
|
releaseAgain()
|
|
}
|
|
|
|
func TestQueryBudgetUsesResolvedRouteBeforeHandler(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
engine := gin.New()
|
|
engine.Use(QueryBudget())
|
|
engine.GET("/api/admin/dashboard", func(c *gin.Context) {
|
|
if _, ok := c.Request.Context().Deadline(); !ok {
|
|
t.Fatal("high-cost route missing query deadline")
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
engine.GET("/api/files/object", func(c *gin.Context) {
|
|
if _, ok := c.Request.Context().Deadline(); ok {
|
|
t.Fatal("streaming route unexpectedly has query deadline")
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
for _, path := range []string{"/api/admin/dashboard", "/api/files/object"} {
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodGet, path, nil)
|
|
engine.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusNoContent {
|
|
t.Fatalf("%s status = %d, want 204", path, recorder.Code)
|
|
}
|
|
}
|
|
}
|