Short and single-letter names in application code become full words (tests keep theirs). Wrapped calls, signatures and composite literals put one item per line, and one-line function bodies are expanded. Exported names and groups of constants get doc comments, and comments refer to the renamed identifiers.
26 lines
598 B
Go
26 lines
598 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestRateLimit(t *testing.T) {
|
|
l := NewKeyedLimiter(t.Context(), 1, 2, 0)
|
|
h := RateLimit(l, nil)(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
|
}))
|
|
var last int
|
|
for i := 0; i < 4; i++ {
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/", nil))
|
|
last = rec.Code
|
|
if last == http.StatusTooManyRequests && rec.Header().Get("Retry-After") == "" {
|
|
t.Fatal("missing Retry-After")
|
|
}
|
|
}
|
|
if last != http.StatusTooManyRequests {
|
|
t.Fatalf("want 429, got %d", last)
|
|
}
|
|
}
|