Skip to content

Commit

Permalink
Merge pull request #1171 from arturhoo/list-dns-auto-pagination-size
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz authored Jan 10, 2023
2 parents ff64222 + f59c0fc commit 0f63f35
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/1171.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
dns: update default `per_page` attribute to 100 records
```
5 changes: 4 additions & 1 deletion dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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(
Expand Down Expand Up @@ -164,7 +167,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 {
Expand Down
40 changes: 38 additions & 2 deletions dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
37 changes: 37 additions & 0 deletions testdata/fixtures/dns/list_page_1.json
Original file line number Diff line number Diff line change
@@ -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
}
}
37 changes: 37 additions & 0 deletions testdata/fixtures/dns/list_page_2.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 0f63f35

Please sign in to comment.