summaryrefslogtreecommitdiff
path: root/internal/control/dns_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/control/dns_test.go')
-rw-r--r--internal/control/dns_test.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/internal/control/dns_test.go b/internal/control/dns_test.go
new file mode 100644
index 0000000..cf44639
--- /dev/null
+++ b/internal/control/dns_test.go
@@ -0,0 +1,58 @@
+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)
+}