Files
weichen 29f14ac5f2 Add multi-instance groundwork: blobstore seam for artifacts
Details (original message):
Add multi-instance groundwork: blobstore seam for artifacts, PG LISTEN/NOTIFY event fanout, and per-tenant rate limiting with native Redis backend
2026-09-30 02:39:50 +00:00

41 lines
899 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package server
// 限流 e2e:limit=3 时第 4 个请求 429;其他租户不受影响。
import (
"net/http"
"net/http/httptest"
"testing"
"agent-system-backend/internal/ratelimit"
)
func TestRateLimitMiddleware(t *testing.T) {
srv, _ := newTestServer(t)
srv.Limiter = ratelimit.NewMemory()
n := 3
srv.LimitLookup = func() int { return n }
h := srv.Handler()
get := func() int {
req := httptest.NewRequest("GET", "/api/projects", nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec.Code
}
for i := 1; i <= n; i++ {
if c := get(); c != http.StatusOK {
t.Fatalf("request %d: %d", i, c)
}
}
if c := get(); c != http.StatusTooManyRequests {
t.Fatalf("4th request: %d (want 429)", c)
}
// lookup<=0 时放行(默认不限)。
n = 0
srv.Limiter = ratelimit.NewMemory()
if c := get(); c != http.StatusOK {
t.Fatalf("unlimited: %d", c)
}
}