diff --git a/.changelog/1171.txt b/.changelog/1171.txt new file mode 100644 index 00000000000..174071f005a --- /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 473f9ae7de6..c2e8b886e64 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 { diff --git a/dns_test.go b/dns_test.go index 08a55e4eb85..8e81159e71d 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 00000000000..15acc55332e --- /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 00000000000..b7836c01c36 --- /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 + } +}