- Add comprehensive root README with badges, architecture diagram, and setup guide - Update backend README with security middlewares and endpoint documentation - Update frontend README with design system and page structure - Update seeder-api README with generated data and credentials - Add internal module READMEs (middleware, handlers, components) - Document Clean Architecture layers and request flow - Add environment variables reference table
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRateLimiter_isAllowed(t *testing.T) {
|
|
limiter := NewRateLimiter(3, time.Minute)
|
|
|
|
// First 3 requests should be allowed
|
|
for i := 0; i < 3; i++ {
|
|
if !limiter.isAllowed("192.168.1.1") {
|
|
t.Errorf("Request %d should be allowed", i+1)
|
|
}
|
|
}
|
|
|
|
// 4th request should be denied
|
|
if limiter.isAllowed("192.168.1.1") {
|
|
t.Error("Request 4 should be denied")
|
|
}
|
|
|
|
// Different IP should still be allowed
|
|
if !limiter.isAllowed("192.168.1.2") {
|
|
t.Error("Different IP should be allowed")
|
|
}
|
|
}
|
|
|
|
func TestRateLimitMiddleware(t *testing.T) {
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
middleware := RateLimitMiddleware(2, time.Minute)(handler)
|
|
|
|
// Create test requests
|
|
for i := 0; i < 3; i++ {
|
|
req := httptest.NewRequest("GET", "/test", nil)
|
|
req.RemoteAddr = "192.168.1.100:12345"
|
|
rr := httptest.NewRecorder()
|
|
|
|
middleware.ServeHTTP(rr, req)
|
|
|
|
if i < 2 {
|
|
if rr.Code != http.StatusOK {
|
|
t.Errorf("Request %d: expected status 200, got %d", i+1, rr.Code)
|
|
}
|
|
} else {
|
|
if rr.Code != http.StatusTooManyRequests {
|
|
t.Errorf("Request %d: expected status 429, got %d", i+1, rr.Code)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSecurityHeadersMiddleware(t *testing.T) {
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
middleware := SecurityHeadersMiddleware(handler)
|
|
|
|
req := httptest.NewRequest("GET", "/test", nil)
|
|
rr := httptest.NewRecorder()
|
|
|
|
middleware.ServeHTTP(rr, req)
|
|
|
|
expectedHeaders := map[string]string{
|
|
"X-Frame-Options": "DENY",
|
|
"X-Content-Type-Options": "nosniff",
|
|
"X-XSS-Protection": "1; mode=block",
|
|
}
|
|
|
|
for header, expected := range expectedHeaders {
|
|
actual := rr.Header().Get(header)
|
|
if actual != expected {
|
|
t.Errorf("Header %s: expected %q, got %q", header, expected, actual)
|
|
}
|
|
}
|
|
}
|