blob: b1fe6af03dac0fc7ab41810af0cbe218b00717c9 [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 Wilson6b35e502014-09-12 15:15:23 -040049 err := setupSinglePaged().EachPage(func(page Page) (bool, error) {
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 {
Ash Wilson6b35e502014-09-12 15:15:23 -040055 return false, err
Ash Wilson5bf6f662014-09-12 12:31:17 -040056 }
Ash Wilson64d67b22014-09-05 13:04:12 -040057 if !reflect.DeepEqual(expected, actual) {
58 t.Errorf("Expected %v, but was %v", expected, actual)
59 }
Ash Wilson6b35e502014-09-12 15:15:23 -040060 return true, nil
Ash Wilson64d67b22014-09-05 13:04:12 -040061 })
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 Wilson6b35e502014-09-12 15:15:23 -0400106 err := pager.EachPage(func(page Page) (bool, error) {
Ash Wilson5bf6f662014-09-12 12:31:17 -0400107 actual, err := ExtractLinkedInts(page)
108 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400109 return false, err
Ash Wilson5bf6f662014-09-12 12:31:17 -0400110 }
111
Ash Wilson64d67b22014-09-05 13:04:12 -0400112 t.Logf("Handler invoked with %v", actual)
113
114 var expected []int
115 switch callCount {
116 case 0:
117 expected = []int{1, 2, 3}
118 case 1:
119 expected = []int{4, 5, 6}
120 case 2:
121 expected = []int{7, 8, 9}
122 default:
123 t.Fatalf("Unexpected call count: %d", callCount)
Ash Wilson6b35e502014-09-12 15:15:23 -0400124 return false, nil
Ash Wilson64d67b22014-09-05 13:04:12 -0400125 }
126
127 if !reflect.DeepEqual(expected, actual) {
128 t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
129 }
130
131 callCount++
Ash Wilson6b35e502014-09-12 15:15:23 -0400132 return true, nil
Ash Wilson64d67b22014-09-05 13:04:12 -0400133 })
134 if err != nil {
135 t.Errorf("Unexpected error for page iteration: %v", err)
136 }
137
138 if callCount != 3 {
139 t.Errorf("Expected 3 calls, but was %d", callCount)
140 }
141}