Files
telemt-api/internal/server/clientip_test.go
T
Denozordec 91f289c647
ci / test (push) Successful in 1m15s
ci / docker (push) Successful in 1m8s
Init
2026-03-29 22:51:02 +07:00

35 lines
727 B
Go

package server
import (
"net/http"
"net/netip"
"testing"
)
func TestAllowed(t *testing.T) {
p, _ := netip.ParsePrefix("127.0.0.1/32")
a := netip.MustParseAddr("127.0.0.1")
if !Allowed(a, false, []netip.Prefix{p}) {
t.Fatal("expected allowed")
}
if Allowed(a, false, nil) {
t.Fatal("empty whitelist should deny")
}
if !Allowed(a, true, nil) {
t.Fatal("allow_all")
}
}
func TestClientIPTrustedXFF(t *testing.T) {
trusted, _ := netip.ParsePrefix("10.0.0.1/32")
r := &http.Request{
Header: http.Header{},
RemoteAddr: "10.0.0.1:12345",
}
r.Header.Set("X-Forwarded-For", "203.0.113.5, 10.0.0.1")
ip := ClientIP(r, []netip.Prefix{trusted})
if ip.String() != "203.0.113.5" {
t.Fatalf("got %v", ip)
}
}