@@ -22,12 +22,12 @@ import (
2222 "fmt"
2323 "os"
2424 "slices"
25- "sort"
2625 "strings"
2726 "testing"
2827 "time"
2928
3029 cloudflarev0 "github.com/cloudflare/cloudflare-go"
30+ "github.com/cloudflare/cloudflare-go/v5"
3131 "github.com/cloudflare/cloudflare-go/v5/dns"
3232 "github.com/cloudflare/cloudflare-go/v5/zones"
3333 "github.com/maxatome/go-testdeep/td"
@@ -171,49 +171,22 @@ func (m *mockCloudFlareClient) CreateDNSRecord(ctx context.Context, params dns.R
171171 return & record , nil
172172}
173173
174- func (m * mockCloudFlareClient ) ListDNSRecords (ctx context.Context , rc * cloudflarev0. ResourceContainer , rp cloudflarev0. ListDNSRecordsParams ) ([] dns.RecordResponse , * cloudflarev0. ResultInfo , error ) {
174+ func (m * mockCloudFlareClient ) ListDNSRecords (ctx context.Context , params dns. RecordListParams ) autoPager [ dns.RecordResponse ] {
175175 if m .dnsRecordsError != nil {
176- return nil , & cloudflarev0. ResultInfo {}, m .dnsRecordsError
176+ return & mockAutoPager [dns. RecordResponse ]{ err : m .dnsRecordsError }
177177 }
178- result := [] dns.RecordResponse {}
179- if zone , ok := m .Records [rc . Identifier ]; ok {
178+ iter := & mockAutoPager [ dns.RecordResponse ] {}
179+ if zone , ok := m .Records [params . ZoneID . Value ]; ok {
180180 for _ , record := range zone {
181181 if strings .HasPrefix (record .Name , "newerror-list-" ) {
182- m .DeleteDNSRecord (ctx , rc , record .ID )
183- return nil , & cloudflarev0.ResultInfo {}, errors .New ("failed to list erroring DNS record" )
182+ m .DeleteDNSRecord (ctx , cloudflarev0 .ResourceIdentifier (params .ZoneID .Value ), record .ID )
183+ iter .err = errors .New ("failed to list erroring DNS record" )
184+ return iter
184185 }
185- result = append (result , record )
186+ iter . items = append (iter . items , record )
186187 }
187188 }
188-
189- if len (result ) == 0 || rp .PerPage == 0 {
190- return result , & cloudflarev0.ResultInfo {Page : 1 , TotalPages : 1 , Count : 0 , Total : 0 }, nil
191- }
192-
193- // if not pagination options were passed in, return the result as is
194- if rp .Page == 0 {
195- return result , & cloudflarev0.ResultInfo {Page : 1 , TotalPages : 1 , Count : len (result ), Total : len (result )}, nil
196- }
197-
198- // otherwise, split the result into chunks of size rp.PerPage to simulate the pagination from the API
199- chunks := [][]dns.RecordResponse {}
200-
201- // to ensure consistency in the multiple calls to this function, sort the result slice
202- sort .Slice (result , func (i , j int ) bool { return strings .Compare (result [i ].ID , result [j ].ID ) > 0 })
203- for rp .PerPage < len (result ) {
204- result , chunks = result [rp .PerPage :], append (chunks , result [0 :rp .PerPage ])
205- }
206- chunks = append (chunks , result )
207-
208- // return the requested page
209- partialResult := chunks [rp .Page - 1 ]
210- return partialResult , & cloudflarev0.ResultInfo {
211- PerPage : rp .PerPage ,
212- Page : rp .Page ,
213- TotalPages : len (chunks ),
214- Count : len (partialResult ),
215- Total : len (result ),
216- }, nil
189+ return iter
217190}
218191
219192func (m * mockCloudFlareClient ) UpdateDNSRecord (ctx context.Context , rc * cloudflarev0.ResourceContainer , rp cloudflarev0.UpdateDNSRecordParams ) error {
@@ -1500,7 +1473,7 @@ func TestGroupByNameAndTypeWithCustomHostnames_MX(t *testing.T) {
15001473 }
15011474 ctx := context .Background ()
15021475 chs := CustomHostnamesMap {}
1503- records , err := provider .listDNSRecordsWithAutoPagination (ctx , "001" )
1476+ records , err := provider .getDNSRecordsMap (ctx , "001" )
15041477 assert .NoError (t , err )
15051478
15061479 endpoints := provider .groupByNameAndTypeWithCustomHostnames (records , chs )
@@ -3346,3 +3319,22 @@ func TestDnsRecordFromLegacyAPI(t *testing.T) {
33463319 })
33473320 }
33483321}
3322+
3323+ func TestZoneService (t * testing.T ) {
3324+ t .Parallel ()
3325+
3326+ ctx , cancel := context .WithCancel (t .Context ())
3327+ cancel ()
3328+
3329+ client := & zoneService {
3330+ service : cloudflare .NewClient (),
3331+ }
3332+
3333+ t .Run ("UpdateDNSRecord" , func (t * testing.T ) {
3334+ t .Parallel ()
3335+ iter := client .ListDNSRecords (ctx , dns.RecordListParams {ZoneID : cloudflare .F ("foo" )})
3336+ require .False (t , iter .Next ())
3337+ require .Empty (t , iter .Current ())
3338+ require .ErrorIs (t , iter .Err (), context .Canceled )
3339+ })
3340+ }
0 commit comments