blob: 665062b6b6726a62bc03991dc8eeba3909031744 [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 -040028func ExtractSingleInts(page Page) ([]int, error) {
29 var response struct {
30 Ints []int `mapstructure:"ints"`
31 }
Ash Wilson64d67b22014-09-05 13:04:12 -040032
Ash Wilson75eae572014-09-12 12:34:21 -040033 err := mapstructure.Decode(page.(SinglePage).Body, &response)
Ash Wilson5bf6f662014-09-12 12:31:17 -040034 if err != nil {
35 return nil, err
36 }
Ash Wilsonb110fc92014-09-08 13:54:59 -040037
Ash Wilson5bf6f662014-09-12 12:31:17 -040038 return response.Ints, nil
Ash Wilson64d67b22014-09-05 13:04:12 -040039}
40
Ash Wilsone30b76b2014-09-12 08:36:17 -040041func setupSinglePaged() Pager {
Ash Wilson5bf6f662014-09-12 12:31:17 -040042 return NewSinglePager(func() (http.Response, error) {
43 return responseWithBody(`{ "ints": [1, 2, 3] }`)
Ash Wilsone30b76b2014-09-12 08:36:17 -040044 })
Ash Wilson64d67b22014-09-05 13:04:12 -040045}
46
47func TestEnumerateSinglePaged(t *testing.T) {
48 callCount := 0
Ash Wilson5bf6f662014-09-12 12:31:17 -040049 err := setupSinglePaged().EachPage(func(page Page) bool {
Ash Wilson64d67b22014-09-05 13:04:12 -040050 callCount++
51
52 expected := []int{1, 2, 3}
Ash Wilson5bf6f662014-09-12 12:31:17 -040053 actual, err := ExtractSingleInts(page)
54 if err != nil {
55 t.Fatalf("Unexpected error extracting ints: %v", err)
56 }
Ash Wilson64d67b22014-09-05 13:04:12 -040057 if !reflect.DeepEqual(expected, actual) {
58 t.Errorf("Expected %v, but was %v", expected, actual)
59 }
60 return true
61 })
Ash Wilsone30b76b2014-09-12 08:36:17 -040062 if err != nil {
63 t.Fatalf("Unexpected error calling EachPage: %v", err)
64 }
Ash Wilson64d67b22014-09-05 13:04:12 -040065
66 if callCount != 1 {
67 t.Errorf("Callback was invoked %d times", callCount)
68 }
69}
70
Ash Wilson64d67b22014-09-05 13:04:12 -040071// LinkedPager sample and test cases.
72
Ash Wilson5bf6f662014-09-12 12:31:17 -040073func ExtractLinkedInts(page Page) ([]int, error) {
74 var response struct {
75 Ints []int `mapstructure:"ints"`
Ash Wilsone30b76b2014-09-12 08:36:17 -040076 }
Ash Wilsonb110fc92014-09-08 13:54:59 -040077
Ash Wilson583dc732014-09-12 13:30:05 -040078 err := mapstructure.Decode(page.(LinkedPage).Body, &response)
Ash Wilson5bf6f662014-09-12 12:31:17 -040079 if err != nil {
80 return nil, err
Ash Wilsonb110fc92014-09-08 13:54:59 -040081 }
Ash Wilson5bf6f662014-09-12 12:31:17 -040082
83 return response.Ints, nil
Ash Wilsonb110fc92014-09-08 13:54:59 -040084}
85
Ash Wilson5bf6f662014-09-12 12:31:17 -040086func createLinked(t *testing.T) Pager {
87 return NewLinkedPager("page1", func(url string) (http.Response, error) {
88 switch url {
89 case "page1":
90 return responseWithBody(`{ "ints": [1, 2, 3], "links": { "next": "page2" } }`)
91 case "page2":
92 return responseWithBody(`{ "ints": [4, 5, 6], "links": { "next": "page3" } }`)
93 case "page3":
94 return responseWithBody(`{ "ints": [7, 8, 9], "links": { "next": null } }`)
Ash Wilson64d67b22014-09-05 13:04:12 -040095 default:
Ash Wilson5bf6f662014-09-12 12:31:17 -040096 t.Fatalf("LinkedPager called with unexpected URL: %v", url)
97 return http.Response{}, errors.New("Wat")
Ash Wilson64d67b22014-09-05 13:04:12 -040098 }
99 })
100}
101
102func TestEnumerateLinked(t *testing.T) {
Ash Wilson5bf6f662014-09-12 12:31:17 -0400103 pager := createLinked(t)
Ash Wilson64d67b22014-09-05 13:04:12 -0400104
105 callCount := 0
Ash Wilson5bf6f662014-09-12 12:31:17 -0400106 err := pager.EachPage(func(page Page) bool {
107 actual, err := ExtractLinkedInts(page)
108 if err != nil {
109 t.Errorf("Unable to extract ints from page: %v", err)
110 return false
111 }
112
Ash Wilson64d67b22014-09-05 13:04:12 -0400113 t.Logf("Handler invoked with %v", actual)
114
115 var expected []int
116 switch callCount {
117 case 0:
118 expected = []int{1, 2, 3}
119 case 1:
120 expected = []int{4, 5, 6}
121 case 2:
122 expected = []int{7, 8, 9}
123 default:
124 t.Fatalf("Unexpected call count: %d", callCount)
125 return false
126 }
127
128 if !reflect.DeepEqual(expected, actual) {
129 t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
130 }
131
132 callCount++
133 return true
134 })
135 if err != nil {
136 t.Errorf("Unexpected error for page iteration: %v", err)
137 }
138
139 if callCount != 3 {
140 t.Errorf("Expected 3 calls, but was %d", callCount)
141 }
142}