Repository: cloudflare/cloudflare-go
Pull request #1171 (2023-01-09 10:00:29)

dns: update ListDNSRecords default `per_page` attribute to 100 records
Bump the `PerPage` value used when auto-pagination is performed on `ListDNSRecords` to 100, matching that of the API: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records

## Description

As outlined in https://github.com/cloudflare/cloudflare-go/pull/1169, when no pagination options are passed in to `ListDNSRecords`, auto- pagination is performed. However, the current `per_page` value doesn't match that of the official API (50 vs 100). This commit makes both values match. 

## Has your change been tested?

A new test was implemented that more explicitly tests the auto pagination behaviour.

## Screenshots (if appropriate):

N/A

## Types of changes

What sort of change does your code introduce/modify?

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

None of the above. I'd classify this as an enhancement, per the discussion on https://github.com/cloudflare/cloudflare-go/pull/1169

## Checklist:

- [X] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
- [X] This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.

[1]: https://help.github.com/articles/closing-issues-using-keywords/


github-actions[bot]: changelog detected :white_check_mark:

Patch:
diff --git a/.changelog/1171.txt b/.changelog/1171.txt
new file mode 100644
index 000000000..174071f00
--- /dev/null
+++ b/.changelog/1171.txt
@@ -0,0 +1,3 @@
+```release-note:enhancement
+dns: update default `per_page` attribute to 100 records
+```
diff --git a/dns.go b/dns.go
index 473f9ae7d..c2e8b886e 100644
--- a/dns.go
+++ b/dns.go
@@ -89,6 +89,9 @@ type DNSListResponse struct {
 	ResultInfo `json:"result_info"`
 }
 
+// listDNSRecordsDefaultPageSize represents the default per_page size of the API.
+var listDNSRecordsDefaultPageSize int = 100
+
 // nontransitionalLookup implements the nontransitional processing as specified in
 // Unicode Technical Standard 46 with almost all checkings off to maximize user freedom.
 var nontransitionalLookup = idna.New(
@@ -170,7 +173,7 @@ func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, param
 	}
 
 	if params.PerPage < 1 {
-		params.PerPage = 50
+		params.PerPage = listDNSRecordsDefaultPageSize
 	}
 
 	if params.Page < 1 {


Test patch:
diff --git a/dns_test.go b/dns_test.go
index 08a55e4eb..8e81159e7 100644
--- a/dns_test.go
+++ b/dns_test.go
@@ -153,7 +153,7 @@ func TestCreateDNSRecord(t *testing.T) {
 	assert.Equal(t, want, actual)
 }
 
-func TestDNSRecords(t *testing.T) {
+func TestListDNSRecords(t *testing.T) {
 	setup()
 	defer teardown()
 
@@ -242,7 +242,7 @@ func TestDNSRecords(t *testing.T) {
 	assert.Equal(t, want, actual)
 }
 
-func TestDNSRecordsSearch(t *testing.T) {
+func TestListDNSRecordsSearch(t *testing.T) {
 	setup()
 	defer teardown()
 
@@ -341,6 +341,42 @@ func TestDNSRecordsSearch(t *testing.T) {
 	assert.Equal(t, want, actual)
 }
 
+func TestListDNSRecordsPagination(t *testing.T) {
+	// change listDNSRecordsDefaultPageSize value to 1 to force pagination
+	listDNSRecordsDefaultPageSize = 1
+
+	setup()
+	defer teardown()
+
+	var page1Called, page2Called bool
+	handler := func(w http.ResponseWriter, r *http.Request) {
+		page := r.URL.Query().Get("page")
+		w.Header().Set("content-type", "application/json")
+
+		var response string
+		switch page {
+		case "1":
+			response = loadFixture("dns", "list_page_1")
+			page1Called = true
+		case "2":
+			response = loadFixture("dns", "list_page_2")
+			page2Called = true
+		default:
+			assert.Failf(t, "Unexpeted page requested: %s", page)
+			return
+		}
+		fmt.Fprint(w, response)
+	}
+
+	mux.HandleFunc("/zones/"+testZoneID+"/dns_records", handler)
+
+	actual, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), ListDNSRecordsParams{})
+	require.NoError(t, err)
+	assert.True(t, page1Called)
+	assert.True(t, page2Called)
+	assert.Len(t, actual, 2)
+}
+
 func TestDNSRecord(t *testing.T) {
 	setup()
 	defer teardown()
diff --git a/testdata/fixtures/dns/list_page_1.json b/testdata/fixtures/dns/list_page_1.json
new file mode 100644
index 000000000..15acc5533
--- /dev/null
+++ b/testdata/fixtures/dns/list_page_1.json
@@ -0,0 +1,37 @@
+{
+    "success": true,
+    "errors": [],
+    "messages": [],
+    "result": [
+        {
+            "id": "372e67954025e0ba6aaa6d586b9e0b59",
+            "type": "A",
+            "name": "example.com",
+            "content": "198.51.100.4",
+            "proxiable": true,
+            "proxied": true,
+            "ttl": 120,
+            "locked": false,
+            "zone_id": "d56084adb405e0b7e32c52321bf07be6",
+            "zone_name": "example.com",
+            "created_on": "2014-01-01T05:20:00Z",
+            "modified_on": "2014-01-01T05:20:00Z",
+            "data": {},
+            "meta": {
+                "auto_added": true,
+                "source": "primary"
+            },
+            "tags": [
+                "tag1",
+                "tag2extended"
+            ]
+        }
+    ],
+    "result_info": {
+        "count": 1,
+        "page": 1,
+        "per_page": 1,
+        "total_count": 2,
+        "total_pages": 2
+    }
+}
diff --git a/testdata/fixtures/dns/list_page_2.json b/testdata/fixtures/dns/list_page_2.json
new file mode 100644
index 000000000..b7836c01c
--- /dev/null
+++ b/testdata/fixtures/dns/list_page_2.json
@@ -0,0 +1,37 @@
+{
+    "success": true,
+    "errors": [],
+    "messages": [],
+    "result": [
+        {
+            "id": "372e67954025e0ba6aaa6d586b9e0b59",
+            "type": "A",
+            "name": "www.example.com",
+            "content": "198.51.100.4",
+            "proxiable": true,
+            "proxied": true,
+            "ttl": 120,
+            "locked": false,
+            "zone_id": "d56084adb405e0b7e32c52321bf07be6",
+            "zone_name": "example.com",
+            "created_on": "2014-01-01T05:20:00Z",
+            "modified_on": "2014-01-01T05:20:00Z",
+            "data": {},
+            "meta": {
+                "auto_added": true,
+                "source": "primary"
+            },
+            "tags": [
+                "tag1",
+                "tag2extended"
+            ]
+        }
+    ],
+    "result_info": {
+        "count": 1,
+        "page": 2,
+        "per_page": 1,
+        "total_count": 2,
+        "total_pages": 2
+    }
+}