Files
masyunis/lib/asn_logging_test.go
T
sol_bot Bot e07de898cd Import Anubis v1.28.2
Replace the tree with upstream TecharoHQ/anubis v1.28.2 (a1384fbf42e3).
Masyunis customizations are re-applied in the following commits.
2026-11-28 14:52:26 +03:00

47 lines
1.2 KiB
Go

package lib
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/TecharoHQ/anubis/lib/geoip/geoiptest"
)
func TestGetRequestLoggerASN(t *testing.T) {
for _, tt := range []struct {
name string
logASN bool
withDB bool
ip string
wantASN string
wantDesc string
}{
{name: "announced", logASN: true, withDB: true, ip: "1.1.1.1", wantASN: "13335", wantDesc: "Cloudflare, Inc."},
{name: "private", logASN: true, withDB: true, ip: "10.0.0.1"},
{name: "not an IP", logASN: true, withDB: true, ip: "taco"},
{name: "logging disabled", withDB: true, ip: "1.1.1.1"},
{name: "no database", logASN: true, ip: "1.1.1.1"},
} {
t.Run(tt.name, func(t *testing.T) {
pol := loadPolicies(t, "", 4)
pol.LogASN = tt.logASN
pol.GeoIP = nil
if tt.withDB {
pol.GeoIP = geoiptest.DB(t)
}
srv := spawnAnubis(t, Options{Policy: pol})
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("X-Real-IP", tt.ip)
_, req = srv.getRequestLogger(req)
asn, desc := asnFromContext(req.Context())
if asn != tt.wantASN || desc != tt.wantDesc {
t.Errorf("want (%q, %q), got (%q, %q)", tt.wantASN, tt.wantDesc, asn, desc)
}
})
}
}