whoissdk/rdap_bootstrap_conditional_test.go

63 lines
1.7 KiB
Go
Raw Permalink Normal View History

2026-03-19 11:53:07 +08:00
package whois
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
)
func TestLoadRDAPBootstrapLayeredConditionalRefresh(t *testing.T) {
ClearRDAPBootstrapRemoteState()
var hit int32
const etag = `"v1"`
var secondIfNoneMatch atomic.Value
remoteRaw := `{"version":"1.0","services":[[["condtest"],["https://rdap.condtest.example/"]]]}`
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
n := atomic.AddInt32(&hit, 1)
if n == 2 {
secondIfNoneMatch.Store(r.Header.Get("If-None-Match"))
}
if r.Header.Get("If-None-Match") == etag {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("ETag", etag)
_, _ = w.Write([]byte(remoteRaw))
}))
defer srv.Close()
opt := RDAPBootstrapLoadOptions{
RefreshRemote: true,
RemoteURL: srv.URL,
}
boot1, err := LoadRDAPBootstrapLayered(context.Background(), opt)
if err != nil {
t.Fatalf("first LoadRDAPBootstrapLayered() error: %v", err)
}
if got := boot1.URLsForTLD("condtest"); len(got) != 1 || got[0] != "https://rdap.condtest.example/" {
t.Fatalf("unexpected first load mapping: %#v", got)
}
boot2, err := LoadRDAPBootstrapLayered(context.Background(), opt)
if err != nil {
t.Fatalf("second LoadRDAPBootstrapLayered() error: %v", err)
}
if got := boot2.URLsForTLD("condtest"); len(got) != 1 || got[0] != "https://rdap.condtest.example/" {
t.Fatalf("unexpected second load mapping: %#v", got)
}
if got := atomic.LoadInt32(&hit); got != 2 {
t.Fatalf("expected 2 remote calls, got=%d", got)
}
v, _ := secondIfNoneMatch.Load().(string)
if v != etag {
t.Fatalf("expected conditional If-None-Match=%q, got=%q", etag, v)
}
}