Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 1 | package pagination |
| 2 | |
| 3 | import ( |
| 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 | |
| 14 | type MarkerPageResult struct { |
| 15 | MarkerPageBase |
| 16 | } |
| 17 | |
| 18 | func (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 Wilson | 7486351 | 2014-09-16 11:45:51 -0400 | [diff] [blame] | 26 | func (r MarkerPageResult) LastMarker() (string, error) { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 27 | 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 | |
| 37 | func 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 | |
| 59 | createPage := func(r LastHTTPResponse) MarkerPage { |
| 60 | p := MarkerPageResult{MarkerPageBase{LastHTTPResponse: r}} |
Ash Wilson | 58c4f67 | 2014-09-16 11:50:56 -0400 | [diff] [blame^] | 61 | p.MarkerPageBase.Owner = p |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 62 | return p |
| 63 | } |
| 64 | |
| 65 | return NewMarkerPager(client, testhelper.Server.URL+"/page", createPage) |
| 66 | } |
| 67 | |
| 68 | func 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 | |
| 80 | func 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 | } |