blob: 4c8674da44449dc834ba40d67b854c42c034082f [file] [log] [blame]
Ash Wilson64d67b22014-09-05 13:04:12 -04001package gophercloud
2
3import (
4 "errors"
5 "fmt"
6 "net/http"
7 "reflect"
8 "testing"
9
10 "github.com/rackspace/gophercloud/testhelper"
11)
12
13// SinglePage sample and test cases.
14
15type SinglePageCollection struct {
16 results []int
17}
18
19func (c SinglePageCollection) Pager() Pager {
20 return SinglePager{}
21}
22
23func AsSingleInts(c Collection) []int {
24 return c.(SinglePageCollection).results
25}
26
27var single = SinglePageCollection{
28 results: []int{1, 2, 3},
29}
30
31func TestEnumerateSinglePaged(t *testing.T) {
32 callCount := 0
33 EachPage(single, func(page Collection) bool {
34 callCount++
35
36 expected := []int{1, 2, 3}
37 actual := AsSingleInts(page)
38 if !reflect.DeepEqual(expected, actual) {
39 t.Errorf("Expected %v, but was %v", expected, actual)
40 }
41 return true
42 })
43
44 if callCount != 1 {
45 t.Errorf("Callback was invoked %d times", callCount)
46 }
47}
48
49func TestAllSinglePaged(t *testing.T) {
50 r, err := AllPages(single)
51 if err != nil {
52 t.Fatalf("Unexpected error when iterating pages: %v", err)
53 }
54
55 expected := []int{1, 2, 3}
56 actual := AsSingleInts(r)
57 if !reflect.DeepEqual(expected, actual) {
58 t.Errorf("Expected %v, but was %v", expected, actual)
59 }
60}
61
62// LinkedPager sample and test cases.
63
64type LinkedCollection struct {
65 PaginationLinks
66
67 service *ServiceClient
68 results []int
69}
70
71func (c LinkedCollection) Links() PaginationLinks {
72 return c.PaginationLinks
73}
74
75func (c LinkedCollection) Service() *ServiceClient {
76 return c.service
77}
78
79func (c LinkedCollection) Interpret(response interface{}) (LinkCollection, error) {
80 fmt.Printf("Interpreting result: %#v\n", response)
81 casted, ok := response.([]interface{})
82 if ok {
83 asInts := make([]int, len(casted))
84 for index, item := range casted {
85 f := item.(float64)
86 asInts[index] = int(f)
87 }
88
89 var nextURL *string
90 switch asInts[0] {
91 case 4:
92 u := testhelper.Server.URL + "/foo?page=3&perPage=3"
93 nextURL = &u
94 case 7:
95 // Leave nextURL as nil.
96 default:
97 return nil, fmt.Errorf("Unexpected resultset: %#v", asInts)
98 }
99
100 result := LinkedCollection{
101 PaginationLinks: PaginationLinks{Next: nextURL},
102 service: c.service,
103 results: asInts,
104 }
105 if nextURL != nil {
106 fmt.Printf("Returning result: %s\n", *nextURL)
107 } else {
108 fmt.Printf("No next link")
109 }
110 return result, nil
111 }
112 return nil, errors.New("Wat")
113}
114
115func (c LinkedCollection) Pager() Pager {
116 return NewLinkPager(c)
117}
118
119func AsLinkedInts(results Collection) []int {
120 return results.(LinkedCollection).results
121}
122
123func createLinked() LinkedCollection {
124 nextURL := testhelper.Server.URL + "/foo?page=2&perPage=3"
125 return LinkedCollection{
126 PaginationLinks: PaginationLinks{Next: &nextURL},
127 service: &ServiceClient{
128 Provider: &ProviderClient{TokenID: "1234"},
129 Endpoint: testhelper.Endpoint(),
130 },
131 results: []int{1, 2, 3},
132 }
133}
134
135func setupLinkedResponses(t *testing.T) {
136 testhelper.Mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
137 testhelper.TestMethod(t, r, "GET")
138 testhelper.TestHeader(t, r, "X-Auth-Token", "1234")
139 w.Header().Add("Content-Type", "application/json")
140
141 r.ParseForm()
142
143 pages := r.Form["page"]
144 if len(pages) != 1 {
145 t.Errorf("Endpoint called with unexpected page: %#v", r.Form)
146 }
147
148 switch pages[0] {
149 case "2":
150 fmt.Fprintf(w, `[4, 5, 6]`)
151 case "3":
152 fmt.Fprintf(w, `[7, 8, 9]`)
153 default:
154 t.Errorf("Endpoint called with unexpected page: %s", pages[0])
155 }
156 })
157}
158
159func TestEnumerateLinked(t *testing.T) {
160 testhelper.SetupHTTP()
161 defer testhelper.TeardownHTTP()
162
163 setupLinkedResponses(t)
164 lc := createLinked()
165
166 callCount := 0
167 err := EachPage(lc, func(page Collection) bool {
168 actual := AsLinkedInts(page)
169 t.Logf("Handler invoked with %v", actual)
170
171 var expected []int
172 switch callCount {
173 case 0:
174 expected = []int{1, 2, 3}
175 case 1:
176 expected = []int{4, 5, 6}
177 case 2:
178 expected = []int{7, 8, 9}
179 default:
180 t.Fatalf("Unexpected call count: %d", callCount)
181 return false
182 }
183
184 if !reflect.DeepEqual(expected, actual) {
185 t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
186 }
187
188 callCount++
189 return true
190 })
191 if err != nil {
192 t.Errorf("Unexpected error for page iteration: %v", err)
193 }
194
195 if callCount != 3 {
196 t.Errorf("Expected 3 calls, but was %d", callCount)
197 }
198}