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