blob: a005fd71e5cdc6bad3372256fbb1d1644a26e7d4 [file] [log] [blame]
Ash Wilson64d67b22014-09-05 13:04:12 -04001package gophercloud
2
3import (
Ash Wilson5bf6f662014-09-12 12:31:17 -04004 "bytes"
5 "errors"
6 "io"
Ash Wilson64d67b22014-09-05 13:04:12 -04007 "net/http"
8 "reflect"
9 "testing"
10
Ash Wilson5bf6f662014-09-12 12:31:17 -040011 "github.com/mitchellh/mapstructure"
Ash Wilson64d67b22014-09-05 13:04:12 -040012)
13
Ash Wilson5bf6f662014-09-12 12:31:17 -040014type nopCloser struct {
15 io.Reader
16}
17
18func (nopCloser) Close() error { return nil }
19
20func responseWithBody(body string) (http.Response, error) {
21 return http.Response{
22 Body: nopCloser{bytes.NewReader([]byte(body))},
23 }, nil
24}
25
Ash Wilson64d67b22014-09-05 13:04:12 -040026// SinglePage sample and test cases.
27
Ash Wilson5bf6f662014-09-12 12:31:17 -040028type SingleIntList struct {
29 SinglePage
Ash Wilson64d67b22014-09-05 13:04:12 -040030}
31
Ash Wilson5bf6f662014-09-12 12:31:17 -040032func ExtractSingleInts(page Page) ([]int, error) {
33 var response struct {
34 Ints []int `mapstructure:"ints"`
35 }
Ash Wilson64d67b22014-09-05 13:04:12 -040036
Ash Wilson5bf6f662014-09-12 12:31:17 -040037 err := mapstructure.Decode(page.(SingleIntList).Body, &response)
38 if err != nil {
39 return nil, err
40 }
Ash Wilsonb110fc92014-09-08 13:54:59 -040041
Ash Wilson5bf6f662014-09-12 12:31:17 -040042 return response.Ints, nil
Ash Wilson64d67b22014-09-05 13:04:12 -040043}
44
Ash Wilsone30b76b2014-09-12 08:36:17 -040045func setupSinglePaged() Pager {
Ash Wilson5bf6f662014-09-12 12:31:17 -040046 return NewSinglePager(func() (http.Response, error) {
47 return responseWithBody(`{ "ints": [1, 2, 3] }`)
Ash Wilsone30b76b2014-09-12 08:36:17 -040048 })
Ash Wilson64d67b22014-09-05 13:04:12 -040049}
50
51func TestEnumerateSinglePaged(t *testing.T) {
52 callCount := 0
Ash Wilson5bf6f662014-09-12 12:31:17 -040053 err := setupSinglePaged().EachPage(func(page Page) bool {
Ash Wilson64d67b22014-09-05 13:04:12 -040054 callCount++
55
56 expected := []int{1, 2, 3}
Ash Wilson5bf6f662014-09-12 12:31:17 -040057 actual, err := ExtractSingleInts(page)
58 if err != nil {
59 t.Fatalf("Unexpected error extracting ints: %v", err)
60 }
Ash Wilson64d67b22014-09-05 13:04:12 -040061 if !reflect.DeepEqual(expected, actual) {
62 t.Errorf("Expected %v, but was %v", expected, actual)
63 }
64 return true
65 })
Ash Wilsone30b76b2014-09-12 08:36:17 -040066 if err != nil {
67 t.Fatalf("Unexpected error calling EachPage: %v", err)
68 }
Ash Wilson64d67b22014-09-05 13:04:12 -040069
70 if callCount != 1 {
71 t.Errorf("Callback was invoked %d times", callCount)
72 }
73}
74
Ash Wilson64d67b22014-09-05 13:04:12 -040075// LinkedPager sample and test cases.
76
Ash Wilson5bf6f662014-09-12 12:31:17 -040077type LinkedIntPage struct {
78 PaginatedLinksPage
Ash Wilson64d67b22014-09-05 13:04:12 -040079}
80
Ash Wilson5bf6f662014-09-12 12:31:17 -040081func ExtractLinkedInts(page Page) ([]int, error) {
82 var response struct {
83 Ints []int `mapstructure:"ints"`
Ash Wilsone30b76b2014-09-12 08:36:17 -040084 }
Ash Wilsonb110fc92014-09-08 13:54:59 -040085
Ash Wilson5bf6f662014-09-12 12:31:17 -040086 err := mapstructure.Decode(page.(LinkedIntPage).Body, &response)
87 if err != nil {
88 return nil, err
Ash Wilsonb110fc92014-09-08 13:54:59 -040089 }
Ash Wilson5bf6f662014-09-12 12:31:17 -040090
91 return response.Ints, nil
Ash Wilsonb110fc92014-09-08 13:54:59 -040092}
93
Ash Wilson5bf6f662014-09-12 12:31:17 -040094func createLinked(t *testing.T) Pager {
95 return NewLinkedPager("page1", func(url string) (http.Response, error) {
96 switch url {
97 case "page1":
98 return responseWithBody(`{ "ints": [1, 2, 3], "links": { "next": "page2" } }`)
99 case "page2":
100 return responseWithBody(`{ "ints": [4, 5, 6], "links": { "next": "page3" } }`)
101 case "page3":
102 return responseWithBody(`{ "ints": [7, 8, 9], "links": { "next": null } }`)
Ash Wilson64d67b22014-09-05 13:04:12 -0400103 default:
Ash Wilson5bf6f662014-09-12 12:31:17 -0400104 t.Fatalf("LinkedPager called with unexpected URL: %v", url)
105 return http.Response{}, errors.New("Wat")
Ash Wilson64d67b22014-09-05 13:04:12 -0400106 }
107 })
108}
109
110func TestEnumerateLinked(t *testing.T) {
Ash Wilson5bf6f662014-09-12 12:31:17 -0400111 pager := createLinked(t)
Ash Wilson64d67b22014-09-05 13:04:12 -0400112
113 callCount := 0
Ash Wilson5bf6f662014-09-12 12:31:17 -0400114 err := pager.EachPage(func(page Page) bool {
115 actual, err := ExtractLinkedInts(page)
116 if err != nil {
117 t.Errorf("Unable to extract ints from page: %v", err)
118 return false
119 }
120
Ash Wilson64d67b22014-09-05 13:04:12 -0400121 t.Logf("Handler invoked with %v", actual)
122
123 var expected []int
124 switch callCount {
125 case 0:
126 expected = []int{1, 2, 3}
127 case 1:
128 expected = []int{4, 5, 6}
129 case 2:
130 expected = []int{7, 8, 9}
131 default:
132 t.Fatalf("Unexpected call count: %d", callCount)
133 return false
134 }
135
136 if !reflect.DeepEqual(expected, actual) {
137 t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
138 }
139
140 callCount++
141 return true
142 })
143 if err != nil {
144 t.Errorf("Unexpected error for page iteration: %v", err)
145 }
146
147 if callCount != 3 {
148 t.Errorf("Expected 3 calls, but was %d", callCount)
149 }
150}