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