blob: 7b1a6daf4ae6c2581a734950655b00cf4f139962 [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 "strings"
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// MarkerPager sample and test cases.
14
15type MarkerPageResult struct {
jrperritt3d966162016-06-06 14:08:54 -050016 pagination.MarkerPageBase
Ash Wilsonc8e68872014-09-16 10:36:56 -040017}
18
19func (r MarkerPageResult) IsEmpty() (bool, error) {
20 results, err := ExtractMarkerStrings(r)
21 if err != nil {
22 return true, err
23 }
24 return len(results) == 0, err
25}
26
Ash Wilson74863512014-09-16 11:45:51 -040027func (r MarkerPageResult) LastMarker() (string, error) {
Ash Wilsonc8e68872014-09-16 10:36:56 -040028 results, err := ExtractMarkerStrings(r)
29 if err != nil {
30 return "", err
31 }
32 if len(results) == 0 {
33 return "", nil
34 }
35 return results[len(results)-1], nil
36}
37
jrperritt3d966162016-06-06 14:08:54 -050038func createMarkerPaged(t *testing.T) pagination.Pager {
Ash Wilsonc8e68872014-09-16 10:36:56 -040039 testhelper.SetupHTTP()
40
41 testhelper.Mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
42 r.ParseForm()
43 ms := r.Form["marker"]
44 switch {
45 case len(ms) == 0:
46 fmt.Fprintf(w, "aaa\nbbb\nccc")
47 case len(ms) == 1 && ms[0] == "ccc":
48 fmt.Fprintf(w, "ddd\neee\nfff")
49 case len(ms) == 1 && ms[0] == "fff":
50 fmt.Fprintf(w, "ggg\nhhh\niii")
51 case len(ms) == 1 && ms[0] == "iii":
52 w.WriteHeader(http.StatusNoContent)
53 default:
54 t.Errorf("Request with unexpected marker: [%v]", ms)
55 }
56 })
57
58 client := createClient()
59
jrperritt3d966162016-06-06 14:08:54 -050060 createPage := func(r pagination.PageResult) pagination.Page {
61 p := MarkerPageResult{pagination.MarkerPageBase{PageResult: r}}
Ash Wilson58c4f672014-09-16 11:50:56 -040062 p.MarkerPageBase.Owner = p
Ash Wilsonc8e68872014-09-16 10:36:56 -040063 return p
64 }
65
jrperritt3d966162016-06-06 14:08:54 -050066 return pagination.NewPager(client, testhelper.Server.URL+"/page", createPage)
Ash Wilsonc8e68872014-09-16 10:36:56 -040067}
68
jrperritt3d966162016-06-06 14:08:54 -050069func ExtractMarkerStrings(page pagination.Page) ([]string, error) {
Ash Wilsonc8e68872014-09-16 10:36:56 -040070 content := page.(MarkerPageResult).Body.([]uint8)
71 parts := strings.Split(string(content), "\n")
72 results := make([]string, 0, len(parts))
73 for _, part := range parts {
74 if len(part) > 0 {
75 results = append(results, part)
76 }
77 }
78 return results, nil
79}
80
81func TestEnumerateMarker(t *testing.T) {
82 pager := createMarkerPaged(t)
83 defer testhelper.TeardownHTTP()
84
85 callCount := 0
jrperritt3d966162016-06-06 14:08:54 -050086 err := pager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonc8e68872014-09-16 10:36:56 -040087 actual, err := ExtractMarkerStrings(page)
88 if err != nil {
89 return false, err
90 }
91
92 t.Logf("Handler invoked with %v", actual)
93
94 var expected []string
95 switch callCount {
96 case 0:
97 expected = []string{"aaa", "bbb", "ccc"}
98 case 1:
99 expected = []string{"ddd", "eee", "fff"}
100 case 2:
101 expected = []string{"ggg", "hhh", "iii"}
102 default:
103 t.Fatalf("Unexpected call count: %d", callCount)
104 return false, nil
105 }
106
107 testhelper.CheckDeepEquals(t, expected, actual)
108
109 callCount++
110 return true, nil
111 })
112 testhelper.AssertNoErr(t, err)
113 testhelper.AssertEquals(t, callCount, 3)
114}
Jon Perrittdb319f12015-02-17 19:32:40 -0700115
116func TestAllPagesMarker(t *testing.T) {
117 pager := createMarkerPaged(t)
118 defer testhelper.TeardownHTTP()
119
Jon Perrittdb319f12015-02-17 19:32:40 -0700120 page, err := pager.AllPages()
121 testhelper.AssertNoErr(t, err)
122
123 expected := []string{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"}
124 actual, err := ExtractMarkerStrings(page)
125 testhelper.AssertNoErr(t, err)
126 testhelper.CheckDeepEquals(t, expected, actual)
127}