Files
lijiaoqiao/supply-api/internal/middleware/ratelimit_basic_test.go
Your Name 8ac23bf7d4 test: improve coverage and fix sanitizer bug
- Fix MaskMap to properly handle []string sensitive fields
- Add missing slice handling in sanitizer
- Add comprehensive tests for GetMetrics and CreateEventsBatch
- Improve audit/handler coverage from 49.8% to 68.8%
- Fix test expectations to match actual sanitizer behavior
- All tests pass
2026-04-08 07:44:58 +08:00

55 lines
1.1 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
// ==================== RateLimit Helper Function Tests ====================
func TestGetTenantIDFromRequest(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
tenantID := getTenantIDFromRequest(req)
// Simplified implementation returns 0
if tenantID != 0 {
t.Errorf("expected tenantID 0, got %d", tenantID)
}
}
func TestGetUserIDFromRequest(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
userID := getUserIDFromRequest(req)
// Simplified implementation returns "unknown"
if userID != "unknown" {
t.Errorf("expected userID 'unknown', got '%s'", userID)
}
}
func TestNewRateLimitHandler(t *testing.T) {
config := &RateLimitConfig{
Enabled: true,
Requests: 100,
Window: time.Minute,
}
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
handler := NewRateLimitHandler(config, nextHandler)
if handler == nil {
t.Fatal("expected non-nil handler")
}
if handler.config != config {
t.Error("expected config to be set")
}
if handler.buckets == nil {
t.Error("expected buckets to be initialized")
}
}