whoissdk/lookup_ip_asn_test.go

83 lines
2.1 KiB
Go
Raw Normal View History

2026-03-19 11:53:07 +08:00
package whois
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestLookupRDAPOnlyIP(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/ip/1.1.1.1" {
http.NotFound(w, r)
return
}
_, _ = w.Write([]byte(`{"objectClassName":"ip network","handle":"NET-1-1-1-0-1"}`))
}))
defer srv.Close()
rdc, err := NewRDAPClientWithBootstrap(&RDAPBootstrap{
Version: "1.0",
Services: []RDAPService{
{TLDs: []string{"com"}, URLs: []string{"https://rdap.example.com/"}},
},
})
if err != nil {
t.Fatalf("NewRDAPClientWithBootstrap() error: %v", err)
}
rdc.SetIPServers(srv.URL)
c := NewClient()
got, meta, err := c.LookupContext(context.Background(), "1.1.1.1",
WithLookupMode(LookupModeRDAPOnly),
WithLookupRDAPClient(rdc),
)
if err != nil {
t.Fatalf("LookupContext() error: %v", err)
}
if meta.Source != LookupSourceRDAP || !got.Exists() {
t.Fatalf("unexpected source=%s exists=%v", meta.Source, got.Exists())
}
if got.Domain() != "1.1.1.1" {
t.Fatalf("unexpected domain: %q", got.Domain())
}
}
func TestLookupRDAPOnlyASN(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/autnum/13335" {
http.NotFound(w, r)
return
}
_, _ = w.Write([]byte(`{"objectClassName":"autnum","handle":"AS13335"}`))
}))
defer srv.Close()
rdc, err := NewRDAPClientWithBootstrap(&RDAPBootstrap{
Version: "1.0",
Services: []RDAPService{
{TLDs: []string{"com"}, URLs: []string{"https://rdap.example.com/"}},
},
})
if err != nil {
t.Fatalf("NewRDAPClientWithBootstrap() error: %v", err)
}
rdc.SetASNServers(srv.URL)
c := NewClient()
got, meta, err := c.LookupContext(context.Background(), "AS13335",
WithLookupMode(LookupModeRDAPOnly),
WithLookupRDAPClient(rdc),
)
if err != nil {
t.Fatalf("LookupContext() error: %v", err)
}
if meta.Source != LookupSourceRDAP || !got.Exists() {
t.Fatalf("unexpected source=%s exists=%v", meta.Source, got.Exists())
}
if got.Domain() != "AS13335" {
t.Fatalf("unexpected domain: %q", got.Domain())
}
}