blob: d215c336148c83041d76ab4969762e6defc59dae [file] [log] [blame]
Ash Wilson64d67b22014-09-05 13:04:12 -04001package gophercloud
2
3import (
Ash Wilson4bf4fac2014-09-15 09:16:13 -04004 "fmt"
Ash Wilson64d67b22014-09-05 13:04:12 -04005 "net/http"
6 "reflect"
7 "testing"
8
Ash Wilson5bf6f662014-09-12 12:31:17 -04009 "github.com/mitchellh/mapstructure"
Ash Wilson4bf4fac2014-09-15 09:16:13 -040010 "github.com/rackspace/gophercloud/testhelper"
Ash Wilson64d67b22014-09-05 13:04:12 -040011)
12
Ash Wilson4bf4fac2014-09-15 09:16:13 -040013func createClient() *ServiceClient {
14 return &ServiceClient{
15 Provider: &ProviderClient{TokenID: "abc123"},
16 Endpoint: testhelper.Endpoint(),
17 }
Ash Wilson5bf6f662014-09-12 12:31:17 -040018}
19
Ash Wilson64d67b22014-09-05 13:04:12 -040020// SinglePage sample and test cases.
21
Ash Wilson5bf6f662014-09-12 12:31:17 -040022func ExtractSingleInts(page Page) ([]int, error) {
23 var response struct {
24 Ints []int `mapstructure:"ints"`
25 }
Ash Wilson64d67b22014-09-05 13:04:12 -040026
Ash Wilson75eae572014-09-12 12:34:21 -040027 err := mapstructure.Decode(page.(SinglePage).Body, &response)
Ash Wilson5bf6f662014-09-12 12:31:17 -040028 if err != nil {
29 return nil, err
30 }
Ash Wilsonb110fc92014-09-08 13:54:59 -040031
Ash Wilson5bf6f662014-09-12 12:31:17 -040032 return response.Ints, nil
Ash Wilson64d67b22014-09-05 13:04:12 -040033}
34
Ash Wilsone30b76b2014-09-12 08:36:17 -040035func setupSinglePaged() Pager {
Ash Wilson4bf4fac2014-09-15 09:16:13 -040036 testhelper.SetupHTTP()
37 client := createClient()
38
39 testhelper.Mux.HandleFunc("/only", func(w http.ResponseWriter, r *http.Request) {
40 fmt.Fprintf(w, `{ "ints": [1, 2, 3] }`)
Ash Wilsone30b76b2014-09-12 08:36:17 -040041 })
Ash Wilson4bf4fac2014-09-15 09:16:13 -040042
43 return NewSinglePager(client, testhelper.Server.URL+"/only")
Ash Wilson64d67b22014-09-05 13:04:12 -040044}
45
46func TestEnumerateSinglePaged(t *testing.T) {
47 callCount := 0
Ash Wilson4bf4fac2014-09-15 09:16:13 -040048 pager := setupSinglePaged()
49 defer testhelper.TeardownHTTP()
50
51 err := pager.EachPage(func(page Page) (bool, error) {
Ash Wilson64d67b22014-09-05 13:04:12 -040052 callCount++
53
54 expected := []int{1, 2, 3}
Ash Wilson5bf6f662014-09-12 12:31:17 -040055 actual, err := ExtractSingleInts(page)
56 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -040057 return false, err
Ash Wilson5bf6f662014-09-12 12:31:17 -040058 }
Ash Wilson64d67b22014-09-05 13:04:12 -040059 if !reflect.DeepEqual(expected, actual) {
60 t.Errorf("Expected %v, but was %v", expected, actual)
61 }
Ash Wilson6b35e502014-09-12 15:15:23 -040062 return true, nil
Ash Wilson64d67b22014-09-05 13:04:12 -040063 })
Ash Wilsone30b76b2014-09-12 08:36:17 -040064 if err != nil {
65 t.Fatalf("Unexpected error calling EachPage: %v", err)
66 }
Ash Wilson64d67b22014-09-05 13:04:12 -040067
68 if callCount != 1 {
69 t.Errorf("Callback was invoked %d times", callCount)
70 }
71}
72
Ash Wilson64d67b22014-09-05 13:04:12 -040073// LinkedPager sample and test cases.
74
Ash Wilson5bf6f662014-09-12 12:31:17 -040075func ExtractLinkedInts(page Page) ([]int, error) {
76 var response struct {
77 Ints []int `mapstructure:"ints"`
Ash Wilsone30b76b2014-09-12 08:36:17 -040078 }
Ash Wilsonb110fc92014-09-08 13:54:59 -040079
Ash Wilson583dc732014-09-12 13:30:05 -040080 err := mapstructure.Decode(page.(LinkedPage).Body, &response)
Ash Wilson5bf6f662014-09-12 12:31:17 -040081 if err != nil {
82 return nil, err
Ash Wilsonb110fc92014-09-08 13:54:59 -040083 }
Ash Wilson5bf6f662014-09-12 12:31:17 -040084
85 return response.Ints, nil
Ash Wilsonb110fc92014-09-08 13:54:59 -040086}
87
Ash Wilson5bf6f662014-09-12 12:31:17 -040088func createLinked(t *testing.T) Pager {
Ash Wilson4bf4fac2014-09-15 09:16:13 -040089 testhelper.SetupHTTP()
90
91 testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) {
92 fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, testhelper.Server.URL)
Ash Wilson64d67b22014-09-05 13:04:12 -040093 })
Ash Wilson4bf4fac2014-09-15 09:16:13 -040094
95 testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
96 fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL)
97 })
98
99 testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) {
100 fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`)
101 })
102
103 client := createClient()
104
105 return NewLinkedPager(client, testhelper.Server.URL+"/page1")
Ash Wilson64d67b22014-09-05 13:04:12 -0400106}
107
108func TestEnumerateLinked(t *testing.T) {
Ash Wilson5bf6f662014-09-12 12:31:17 -0400109 pager := createLinked(t)
Ash Wilson4bf4fac2014-09-15 09:16:13 -0400110 defer testhelper.TeardownHTTP()
Ash Wilson64d67b22014-09-05 13:04:12 -0400111
112 callCount := 0
Ash Wilson6b35e502014-09-12 15:15:23 -0400113 err := pager.EachPage(func(page Page) (bool, error) {
Ash Wilson5bf6f662014-09-12 12:31:17 -0400114 actual, err := ExtractLinkedInts(page)
115 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400116 return false, err
Ash Wilson5bf6f662014-09-12 12:31:17 -0400117 }
118
Ash Wilson64d67b22014-09-05 13:04:12 -0400119 t.Logf("Handler invoked with %v", actual)
120
121 var expected []int
122 switch callCount {
123 case 0:
124 expected = []int{1, 2, 3}
125 case 1:
126 expected = []int{4, 5, 6}
127 case 2:
128 expected = []int{7, 8, 9}
129 default:
130 t.Fatalf("Unexpected call count: %d", callCount)
Ash Wilson6b35e502014-09-12 15:15:23 -0400131 return false, nil
Ash Wilson64d67b22014-09-05 13:04:12 -0400132 }
133
134 if !reflect.DeepEqual(expected, actual) {
135 t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
136 }
137
138 callCount++
Ash Wilson6b35e502014-09-12 15:15:23 -0400139 return true, nil
Ash Wilson64d67b22014-09-05 13:04:12 -0400140 })
141 if err != nil {
142 t.Errorf("Unexpected error for page iteration: %v", err)
143 }
144
145 if callCount != 3 {
146 t.Errorf("Expected 3 calls, but was %d", callCount)
147 }
148}