forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pager_test.go
160 lines (140 loc) · 3.77 KB
/
pager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package recurly_test
import (
"context"
"net/http"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/toggl/recurly"
)
func TestPager(t *testing.T) {
const CursorA = "1972702718353176814:A1465932489"
client, s := recurly.NewTestServer()
defer s.Close()
var invocations int
s.HandleFunc("GET", "/v2/accounts", func(w http.ResponseWriter, r *http.Request) {
cursor := r.URL.Query().Get("cursor")
switch invocations {
case 0:
if cursor != "" {
t.Fatalf("unexpected cursor: %s", cursor)
}
w.Header().Set("Link", `<https://test.recurly.com/v2/accounts?cursor=`+CursorA+`>; rel="next"`)
case 1:
if cursor != CursorA {
t.Fatalf("unexpected cursor: %s", cursor)
}
default:
t.Fatalf("unexpected number of invocations")
}
query := r.URL.Query()
query.Del("cursor") // conditionally checked above
if diff := cmp.Diff(query, url.Values{
"per_page": []string{"50"},
"sort": []string{"created_at"},
"order": []string{"asc"},
"state": []string{"active"},
"begin_time": []string{"2011-10-17T17:24:53Z"},
"end_time": []string{"2011-10-18T17:24:53Z"},
}); diff != "" {
t.Fatal(diff)
}
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("accounts.xml"))
invocations++
}, t)
pager := client.Accounts.List(&recurly.PagerOptions{
PerPage: 50,
Sort: "created_at",
Order: "asc",
State: "active",
BeginTime: recurly.NewTime(MustParseTime("2011-10-17T17:24:53Z")),
EndTime: recurly.NewTime(MustParseTime("2011-10-18T17:24:53Z")),
})
for pager.Next() {
var a []recurly.Account
if err := pager.Fetch(context.Background(), &a); err != nil {
t.Fatal(err)
} else if !s.Invoked {
t.Fatal("expected s to be invoked")
} else if diff := cmp.Diff(a, []recurly.Account{*NewTestAccount()}); diff != "" {
t.Fatal(diff)
}
// Check cursor.
switch invocations {
case 1:
if pager.Cursor() == CursorA {
break
}
fallthrough
case 2:
if pager.Cursor() == "" {
break
}
fallthrough
default:
t.Fatalf("unexpected cursors on invocation %d: cursor=%s", invocations, pager.Cursor())
}
s.Invoked = false
}
}
// Verify pager can accept a cursor and send it through to Recurly as expected.
func TestPager_CursorProvided(t *testing.T) {
const CursorA = "CURSOR_A"
const CursorB = "CURSOR_B"
client, s := recurly.NewTestServer()
defer s.Close()
var invocations int
s.HandleFunc("GET", "/v2/accounts", func(w http.ResponseWriter, r *http.Request) {
cursor := r.URL.Query().Get("cursor")
switch invocations {
case 0:
if cursor != CursorA {
t.Fatalf("unexpected cursor: %s", cursor)
}
w.Header().Set("Link", `<https://test.recurly.com/v2/accounts?cursor=`+CursorB+`>; rel="next"`)
case 1:
if cursor != CursorB {
t.Fatalf("unexpected cursor: %s", cursor)
}
default:
t.Fatalf("unexpected number of invocations")
}
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("accounts.xml"))
invocations++
}, t)
pager := client.Accounts.List(&recurly.PagerOptions{
Cursor: CursorA, // Provide starting cursor
})
// Verify starting cursor is set.
if pager.Cursor() != CursorA {
t.Fatalf("unexpected next cursor: %s", pager.Cursor())
}
for pager.Next() {
var a []recurly.Account
if err := pager.Fetch(context.Background(), &a); err != nil {
t.Fatal(err)
} else if !s.Invoked {
t.Fatal("expected s to be invoked")
} else if diff := cmp.Diff(a, []recurly.Account{*NewTestAccount()}); diff != "" {
t.Fatal(diff)
}
// Check cursor.
switch invocations {
case 1:
if pager.Cursor() == CursorB {
break
}
fallthrough
case 2:
if pager.Cursor() == "" {
break
}
fallthrough
default:
t.Fatalf("unexpected cursors on invocation %d: cursor=%s", invocations, pager.Cursor())
}
s.Invoked = false
}
}