package control import ( "context" "encoding/json" "io" "net/http" "strings" "testing" ) func TestPorkbunEnsureRandomARecord(t *testing.T) { t.Parallel() retrieveCalls := 0 client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { recorder := map[string]any{} switch { case strings.Contains(r.URL.Path, "/dns/retrieveByNameType/"): retrieveCalls++ recorder = map[string]any{ "status": "SUCCESS", "records": []map[string]any{}, } case strings.Contains(r.URL.Path, "/dns/create/"): recorder = map[string]any{ "status": "SUCCESS", "id": "123", } default: t.Fatalf("unexpected path %s", r.URL.Path) } body, _ := json.Marshal(recorder) return &http.Response{ StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(string(body))), }, nil })} clientAPI := PorkbunClient{APIKey: "a", SecretAPIKey: "b", HTTPClient: client} name, err := clientAPI.EnsureRandomARecord(context.Background(), "em-sysadmin.xyz", "vpn", "203.0.113.10", 600) if err != nil { t.Fatalf("EnsureRandomARecord error = %v", err) } if !strings.HasSuffix(name, ".em-sysadmin.xyz") { t.Fatalf("expected fqdn suffix, got %q", name) } if retrieveCalls == 0 { t.Fatal("expected retrieve call") } } type roundTripFunc func(*http.Request) (*http.Response, error) func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }