Extract generic trusted proxy handling into xff/ (stdlib only), Echo framework adapter into xff/echo/, and slim xff/fastlyxff/ down to Fastly JSON loading. Key changes: - xff/ uses netip.Prefix for efficient IP matching - Fix XFF extraction to walk right-to-left per MDN spec - Remove echo dependency from core xff package - fastlyxff.New() now returns *xff.TrustedProxies
45 lines
817 B
Go
45 lines
817 B
Go
package fastlyxff
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestNew(t *testing.T) {
|
|
tp, err := New("fastly.json")
|
|
if err != nil {
|
|
t.Fatalf("could not load test data: %s", err)
|
|
}
|
|
|
|
prefixes := tp.Prefixes()
|
|
if len(prefixes) < 10 {
|
|
t.Errorf("only got %d prefixes, expected more", len(prefixes))
|
|
}
|
|
}
|
|
|
|
func TestNewFileNotFound(t *testing.T) {
|
|
_, err := New("nonexistent.json")
|
|
if err == nil {
|
|
t.Fatal("expected error for missing file")
|
|
}
|
|
}
|
|
|
|
func TestNewInvalidJSON(t *testing.T) {
|
|
// Create a temp file with invalid JSON
|
|
f, err := os.CreateTemp("", "fastlyxff-test-*.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Remove(f.Name())
|
|
|
|
if _, err := f.WriteString("{invalid"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.Close()
|
|
|
|
_, err = New(f.Name())
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid JSON")
|
|
}
|
|
}
|