blob: 1ac0f73164f65be9de30b6318cda8d3317cb8efd [file] [log] [blame]
Ash Wilsonc8e68872014-09-16 10:36:56 -04001package pagination
2
3import (
4 "fmt"
5 "net/http"
6 "reflect"
7 "testing"
8
9 "github.com/mitchellh/mapstructure"
10 "github.com/rackspace/gophercloud/testhelper"
11)
12
13// LinkedPager sample and test cases.
14
15type LinkedPageResult struct {
16 LinkedPageBase
17}
18
19func (r LinkedPageResult) IsEmpty() (bool, error) {
20 is, err := ExtractLinkedInts(r)
21 if err != nil {
22 return true, nil
23 }
24 return len(is) == 0, nil
25}
26
27func ExtractLinkedInts(page Page) ([]int, error) {
28 var response struct {
29 Ints []int `mapstructure:"ints"`
30 }
31
32 err := mapstructure.Decode(page.(LinkedPageResult).Body, &response)
33 if err != nil {
34 return nil, err
35 }
36
37 return response.Ints, nil
38}
39
40func createLinked(t *testing.T) Pager {
41 testhelper.SetupHTTP()
42
43 testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) {
44 w.Header().Add("Content-Type", "application/json")
45 fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, testhelper.Server.URL)
46 })
47
48 testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
49 w.Header().Add("Content-Type", "application/json")
50 fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL)
51 })
52
53 testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) {
54 w.Header().Add("Content-Type", "application/json")
55 fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`)
56 })
57
58 client := createClient()
59
Ash Wilsonb8b16f82014-10-20 10:19:49 -040060 createPage := func(r PageResult) Page {
61 return LinkedPageResult{LinkedPageBase{PageResult: r}}
Ash Wilsonc8e68872014-09-16 10:36:56 -040062 }
63
Ash Wilson7049af42014-09-16 13:04:48 -040064 return NewPager(client, testhelper.Server.URL+"/page1", createPage)
Ash Wilsonc8e68872014-09-16 10:36:56 -040065}
66
67func TestEnumerateLinked(t *testing.T) {
68 pager := createLinked(t)
69 defer testhelper.TeardownHTTP()
70
71 callCount := 0
72 err := pager.EachPage(func(page Page) (bool, error) {
73 actual, err := ExtractLinkedInts(page)
74 if err != nil {
75 return false, err
76 }
77
78 t.Logf("Handler invoked with %v", actual)
79
80 var expected []int
81 switch callCount {
82 case 0:
83 expected = []int{1, 2, 3}
84 case 1:
85 expected = []int{4, 5, 6}
86 case 2:
87 expected = []int{7, 8, 9}
88 default:
89 t.Fatalf("Unexpected call count: %d", callCount)
90 return false, nil
91 }
92
93 if !reflect.DeepEqual(expected, actual) {
94 t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
95 }
96
97 callCount++
98 return true, nil
99 })
100 if err != nil {
101 t.Errorf("Unexpected error for page iteration: %v", err)
102 }
103
104 if callCount != 3 {
105 t.Errorf("Expected 3 calls, but was %d", callCount)
106 }
107}
Jon Perrittdb319f12015-02-17 19:32:40 -0700108
109func TestAllPagesLinked(t *testing.T) {
110 pager := createLinked(t)
111 defer testhelper.TeardownHTTP()
112
Jon Perrittdb319f12015-02-17 19:32:40 -0700113 page, err := pager.AllPages()
114 testhelper.AssertNoErr(t, err)
115
116 expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
117 actual, err := ExtractLinkedInts(page)
118 testhelper.AssertNoErr(t, err)
119 testhelper.CheckDeepEquals(t, expected, actual)
120}