blob: 3b1df1d68b9da1864e70c53df3fde8a14e66935b [file] [log] [blame]
Ash Wilsonc8e68872014-09-16 10:36:56 -04001package pagination
2
3import (
4 "fmt"
5 "net/http"
6 "strings"
7 "testing"
8
9 "github.com/rackspace/gophercloud/testhelper"
10)
11
12// MarkerPager sample and test cases.
13
14type MarkerPageResult struct {
15 MarkerPageBase
16}
17
18func (r MarkerPageResult) IsEmpty() (bool, error) {
19 results, err := ExtractMarkerStrings(r)
20 if err != nil {
21 return true, err
22 }
23 return len(results) == 0, err
24}
25
Ash Wilson74863512014-09-16 11:45:51 -040026func (r MarkerPageResult) LastMarker() (string, error) {
Ash Wilsonc8e68872014-09-16 10:36:56 -040027 results, err := ExtractMarkerStrings(r)
28 if err != nil {
29 return "", err
30 }
31 if len(results) == 0 {
32 return "", nil
33 }
34 return results[len(results)-1], nil
35}
36
37func createMarkerPaged(t *testing.T) Pager {
38 testhelper.SetupHTTP()
39
40 testhelper.Mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
41 r.ParseForm()
42 ms := r.Form["marker"]
43 switch {
44 case len(ms) == 0:
45 fmt.Fprintf(w, "aaa\nbbb\nccc")
46 case len(ms) == 1 && ms[0] == "ccc":
47 fmt.Fprintf(w, "ddd\neee\nfff")
48 case len(ms) == 1 && ms[0] == "fff":
49 fmt.Fprintf(w, "ggg\nhhh\niii")
50 case len(ms) == 1 && ms[0] == "iii":
51 w.WriteHeader(http.StatusNoContent)
52 default:
53 t.Errorf("Request with unexpected marker: [%v]", ms)
54 }
55 })
56
57 client := createClient()
58
Ash Wilsonb8b16f82014-10-20 10:19:49 -040059 createPage := func(r PageResult) Page {
60 p := MarkerPageResult{MarkerPageBase{PageResult: r}}
Ash Wilson58c4f672014-09-16 11:50:56 -040061 p.MarkerPageBase.Owner = p
Ash Wilsonc8e68872014-09-16 10:36:56 -040062 return p
63 }
64
Ash Wilson7049af42014-09-16 13:04:48 -040065 return NewPager(client, testhelper.Server.URL+"/page", createPage)
Ash Wilsonc8e68872014-09-16 10:36:56 -040066}
67
68func ExtractMarkerStrings(page Page) ([]string, error) {
69 content := page.(MarkerPageResult).Body.([]uint8)
70 parts := strings.Split(string(content), "\n")
71 results := make([]string, 0, len(parts))
72 for _, part := range parts {
73 if len(part) > 0 {
74 results = append(results, part)
75 }
76 }
77 return results, nil
78}
79
80func TestEnumerateMarker(t *testing.T) {
81 pager := createMarkerPaged(t)
82 defer testhelper.TeardownHTTP()
83
84 callCount := 0
85 err := pager.EachPage(func(page Page) (bool, error) {
86 actual, err := ExtractMarkerStrings(page)
87 if err != nil {
88 return false, err
89 }
90
91 t.Logf("Handler invoked with %v", actual)
92
93 var expected []string
94 switch callCount {
95 case 0:
96 expected = []string{"aaa", "bbb", "ccc"}
97 case 1:
98 expected = []string{"ddd", "eee", "fff"}
99 case 2:
100 expected = []string{"ggg", "hhh", "iii"}
101 default:
102 t.Fatalf("Unexpected call count: %d", callCount)
103 return false, nil
104 }
105
106 testhelper.CheckDeepEquals(t, expected, actual)
107
108 callCount++
109 return true, nil
110 })
111 testhelper.AssertNoErr(t, err)
112 testhelper.AssertEquals(t, callCount, 3)
113}