package whois import ( "errors" "net" "strings" "testing" "time" ) type testDialer struct{} func (d *testDialer) Dial(_, _ string) (net.Conn, error) { return nil, errors.New("test dialer") } func TestWithLookupProxySetsCommonProxy(t *testing.T) { cfg := defaultLookupOptions() WithLookupProxy(" socks5://127.0.0.1:1080 ")(&cfg) if got, want := cfg.Ops.Common.Proxy, "socks5://127.0.0.1:1080"; got != want { t.Fatalf("WithLookupProxy() got %q, want %q", got, want) } } func TestEffectiveRDAPProxyFallbackAndOverride(t *testing.T) { cfg := defaultLookupOptions() cfg.Ops.Common.Proxy = "socks5://127.0.0.1:1080" if got := cfg.effectiveRDAPProxy(); got != cfg.Ops.Common.Proxy { t.Fatalf("effectiveRDAPProxy() fallback got %q, want %q", got, cfg.Ops.Common.Proxy) } cfg.Ops.RDAP.Proxy = "http://127.0.0.1:8080" if got := cfg.effectiveRDAPProxy(); got != cfg.Ops.RDAP.Proxy { t.Fatalf("effectiveRDAPProxy() override got %q, want %q", got, cfg.Ops.RDAP.Proxy) } } func TestEffectiveWHOISDialerFromCommonProxy(t *testing.T) { cfg := defaultLookupOptions() cfg.Ops.Common.Proxy = "127.0.0.1:1080" d, err := cfg.effectiveWHOISDialer(time.Second) if err != nil { t.Fatalf("effectiveWHOISDialer() error: %v", err) } if d == nil { t.Fatal("effectiveWHOISDialer() dialer is nil") } } func TestEffectiveWHOISDialerPrefersExplicitDialer(t *testing.T) { cfg := defaultLookupOptions() cfg.Ops.Common.Proxy = "socks5://127.0.0.1:1080" explicit := &testDialer{} cfg.Ops.WHOIS.Dialer = explicit d, err := cfg.effectiveWHOISDialer(time.Second) if err != nil { t.Fatalf("effectiveWHOISDialer() error: %v", err) } if d != explicit { t.Fatal("effectiveWHOISDialer() did not return explicit dialer") } } func TestEffectiveWHOISDialerRejectsUnsupportedScheme(t *testing.T) { cfg := defaultLookupOptions() cfg.Ops.Common.Proxy = "http://127.0.0.1:8080" d, err := cfg.effectiveWHOISDialer(time.Second) if err == nil { t.Fatal("expected effectiveWHOISDialer() error for unsupported proxy scheme") } if d != nil { t.Fatal("effectiveWHOISDialer() returned unexpected dialer") } if !strings.Contains(err.Error(), "whois proxy unsupported") { t.Fatalf("unexpected error: %v", err) } }