blob: 3533e445a36b9766008677be08829b793f707342 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsonc8e68872014-09-16 10:36:56 -04002
3import (
4 "fmt"
5 "net/http"
6 "reflect"
7 "testing"
8
jrperritt3d966162016-06-06 14:08:54 -05009 "github.com/gophercloud/gophercloud/pagination"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsonc8e68872014-09-16 10:36:56 -040011)
12
13// LinkedPager sample and test cases.
14
15type LinkedPageResult struct {
jrperritt3d966162016-06-06 14:08:54 -050016 pagination.LinkedPageBase
Ash Wilsonc8e68872014-09-16 10:36:56 -040017}
18
19func (r LinkedPageResult) IsEmpty() (bool, error) {
20 is, err := ExtractLinkedInts(r)
Jon Perrittc7d828e2016-02-25 03:06:33 -060021 return len(is) == 0, err
Ash Wilsonc8e68872014-09-16 10:36:56 -040022}
23
jrperritt3d966162016-06-06 14:08:54 -050024func ExtractLinkedInts(r pagination.Page) ([]int, error) {
Jon Perrittc7d828e2016-02-25 03:06:33 -060025 var s struct {
26 Ints []int `json:"ints"`
Ash Wilsonc8e68872014-09-16 10:36:56 -040027 }
Jon Perrittc7d828e2016-02-25 03:06:33 -060028 err := (r.(LinkedPageResult)).ExtractInto(&s)
29 return s.Ints, err
Ash Wilsonc8e68872014-09-16 10:36:56 -040030}
31
jrperritt3d966162016-06-06 14:08:54 -050032func createLinked(t *testing.T) pagination.Pager {
Ash Wilsonc8e68872014-09-16 10:36:56 -040033 testhelper.SetupHTTP()
34
35 testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) {
36 w.Header().Add("Content-Type", "application/json")
37 fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, testhelper.Server.URL)
38 })
39
40 testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
41 w.Header().Add("Content-Type", "application/json")
42 fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL)
43 })
44
45 testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) {
46 w.Header().Add("Content-Type", "application/json")
47 fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`)
48 })
49
50 client := createClient()
51
jrperritt3d966162016-06-06 14:08:54 -050052 createPage := func(r pagination.PageResult) pagination.Page {
53 return LinkedPageResult{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonc8e68872014-09-16 10:36:56 -040054 }
55
jrperritt3d966162016-06-06 14:08:54 -050056 return pagination.NewPager(client, testhelper.Server.URL+"/page1", createPage)
Ash Wilsonc8e68872014-09-16 10:36:56 -040057}
58
59func TestEnumerateLinked(t *testing.T) {
60 pager := createLinked(t)
61 defer testhelper.TeardownHTTP()
62
63 callCount := 0
jrperritt3d966162016-06-06 14:08:54 -050064 err := pager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonc8e68872014-09-16 10:36:56 -040065 actual, err := ExtractLinkedInts(page)
66 if err != nil {
67 return false, err
68 }
69
70 t.Logf("Handler invoked with %v", actual)
71
72 var expected []int
73 switch callCount {
74 case 0:
75 expected = []int{1, 2, 3}
76 case 1:
77 expected = []int{4, 5, 6}
78 case 2:
79 expected = []int{7, 8, 9}
80 default:
81 t.Fatalf("Unexpected call count: %d", callCount)
82 return false, nil
83 }
84
85 if !reflect.DeepEqual(expected, actual) {
86 t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
87 }
88
89 callCount++
90 return true, nil
91 })
92 if err != nil {
93 t.Errorf("Unexpected error for page iteration: %v", err)
94 }
95
96 if callCount != 3 {
97 t.Errorf("Expected 3 calls, but was %d", callCount)
98 }
99}
Jon Perrittdb319f12015-02-17 19:32:40 -0700100
101func TestAllPagesLinked(t *testing.T) {
102 pager := createLinked(t)
103 defer testhelper.TeardownHTTP()
104
Jon Perrittdb319f12015-02-17 19:32:40 -0700105 page, err := pager.AllPages()
106 testhelper.AssertNoErr(t, err)
107
108 expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
109 actual, err := ExtractLinkedInts(page)
110 testhelper.AssertNoErr(t, err)
111 testhelper.CheckDeepEquals(t, expected, actual)
112}