Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 1 | package pagination |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/testhelper" |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | // LinkedPager sample and test cases. |
| 14 | |
| 15 | type LinkedPageResult struct { |
| 16 | LinkedPageBase |
| 17 | } |
| 18 | |
| 19 | func (r LinkedPageResult) IsEmpty() (bool, error) { |
| 20 | is, err := ExtractLinkedInts(r) |
| 21 | if err != nil { |
| 22 | return true, nil |
| 23 | } |
| 24 | return len(is) == 0, nil |
| 25 | } |
| 26 | |
| 27 | func ExtractLinkedInts(page Page) ([]int, error) { |
| 28 | var response struct { |
| 29 | Ints []int `mapstructure:"ints"` |
| 30 | } |
| 31 | |
| 32 | err := mapstructure.Decode(page.(LinkedPageResult).Body, &response) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | |
| 37 | return response.Ints, nil |
| 38 | } |
| 39 | |
| 40 | func createLinked(t *testing.T) Pager { |
| 41 | testhelper.SetupHTTP() |
| 42 | |
| 43 | testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) { |
| 44 | w.Header().Add("Content-Type", "application/json") |
| 45 | fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, testhelper.Server.URL) |
| 46 | }) |
| 47 | |
| 48 | testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) { |
| 49 | w.Header().Add("Content-Type", "application/json") |
| 50 | fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL) |
| 51 | }) |
| 52 | |
| 53 | testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) { |
| 54 | w.Header().Add("Content-Type", "application/json") |
| 55 | fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`) |
| 56 | }) |
| 57 | |
| 58 | client := createClient() |
| 59 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 60 | createPage := func(r PageResult) Page { |
| 61 | return LinkedPageResult{LinkedPageBase{PageResult: r}} |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 64 | return NewPager(client, testhelper.Server.URL+"/page1", createPage) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | func TestEnumerateLinked(t *testing.T) { |
| 68 | pager := createLinked(t) |
| 69 | defer testhelper.TeardownHTTP() |
| 70 | |
| 71 | callCount := 0 |
| 72 | err := pager.EachPage(func(page Page) (bool, error) { |
| 73 | actual, err := ExtractLinkedInts(page) |
| 74 | if err != nil { |
| 75 | return false, err |
| 76 | } |
| 77 | |
| 78 | t.Logf("Handler invoked with %v", actual) |
| 79 | |
| 80 | var expected []int |
| 81 | switch callCount { |
| 82 | case 0: |
| 83 | expected = []int{1, 2, 3} |
| 84 | case 1: |
| 85 | expected = []int{4, 5, 6} |
| 86 | case 2: |
| 87 | expected = []int{7, 8, 9} |
| 88 | default: |
| 89 | t.Fatalf("Unexpected call count: %d", callCount) |
| 90 | return false, nil |
| 91 | } |
| 92 | |
| 93 | if !reflect.DeepEqual(expected, actual) { |
| 94 | t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual) |
| 95 | } |
| 96 | |
| 97 | callCount++ |
| 98 | return true, nil |
| 99 | }) |
| 100 | if err != nil { |
| 101 | t.Errorf("Unexpected error for page iteration: %v", err) |
| 102 | } |
| 103 | |
| 104 | if callCount != 3 { |
| 105 | t.Errorf("Expected 3 calls, but was %d", callCount) |
| 106 | } |
| 107 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 108 | |
| 109 | func TestAllPagesLinked(t *testing.T) { |
| 110 | pager := createLinked(t) |
| 111 | defer testhelper.TeardownHTTP() |
| 112 | |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 113 | page, err := pager.AllPages() |
| 114 | testhelper.AssertNoErr(t, err) |
| 115 | |
| 116 | expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 117 | actual, err := ExtractLinkedInts(page) |
| 118 | testhelper.AssertNoErr(t, err) |
| 119 | testhelper.CheckDeepEquals(t, expected, actual) |
| 120 | } |