Files
go-otel/internal/honeypot/honeypot_test.go
T
mats 21f04d696b
Build and Publish / Metadata (push) Successful in 2s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 58s
more otel
2026-09-18 20:51:37 +00:00

42 lines
1.4 KiB
Go

package honeypot
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestPromotesAfterThresholdAndExpires(t *testing.T) {
tracker := New(Config{Threshold: 2, Window: time.Minute, TTL: time.Millisecond, Token: "secret"})
for i := 0; i < 3; i++ {
req := httptest.NewRequest(http.MethodGet, "https://example.test/.env", nil)
req.RemoteAddr = "192.0.2.10:1234"
tracker.Middleware(http.HandlerFunc(http.NotFound)).ServeHTTP(httptest.NewRecorder(), req)
}
if got := len(tracker.PromotedIPs()); got != 1 {
t.Fatalf("got %d promoted IPs, want 1", got)
}
time.Sleep(2 * time.Millisecond)
if got := len(tracker.PromotedIPs()); got != 0 {
t.Fatalf("got %d expired IPs, want 0", got)
}
}
func TestHandleIPsRequiresToken(t *testing.T) {
tracker := New(Config{Threshold: 1, Token: "secret"})
recorder := httptest.NewRecorder()
tracker.HandleIPs(recorder, httptest.NewRequest(http.MethodGet, "https://example.test/internal/honeypot/ips", nil))
if recorder.Code != http.StatusUnauthorized {
t.Fatalf("got %d, want 401", recorder.Code)
}
recorder = httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "https://example.test/internal/honeypot/ips", nil)
req.Header.Set("Authorization", "Bearer secret")
tracker.HandleIPs(recorder, req)
if recorder.Code != http.StatusOK || !strings.HasPrefix(recorder.Body.String(), "[]") {
t.Fatalf("got %d and %q", recorder.Code, recorder.Body.String())
}
}