Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 4 | "fmt" |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 5 | "net/http" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 9 | "github.com/mitchellh/mapstructure" |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/testhelper" |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 13 | func createClient() *ServiceClient { |
| 14 | return &ServiceClient{ |
| 15 | Provider: &ProviderClient{TokenID: "abc123"}, |
| 16 | Endpoint: testhelper.Endpoint(), |
| 17 | } |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 18 | } |
| 19 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 20 | // SinglePage sample and test cases. |
| 21 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 22 | func ExtractSingleInts(page Page) ([]int, error) { |
| 23 | var response struct { |
| 24 | Ints []int `mapstructure:"ints"` |
| 25 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 26 | |
Ash Wilson | 75eae57 | 2014-09-12 12:34:21 -0400 | [diff] [blame] | 27 | err := mapstructure.Decode(page.(SinglePage).Body, &response) |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 31 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 32 | return response.Ints, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 33 | } |
| 34 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 35 | func setupSinglePaged() Pager { |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 36 | testhelper.SetupHTTP() |
| 37 | client := createClient() |
| 38 | |
| 39 | testhelper.Mux.HandleFunc("/only", func(w http.ResponseWriter, r *http.Request) { |
| 40 | fmt.Fprintf(w, `{ "ints": [1, 2, 3] }`) |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 41 | }) |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 42 | |
| 43 | return NewSinglePager(client, testhelper.Server.URL+"/only") |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func TestEnumerateSinglePaged(t *testing.T) { |
| 47 | callCount := 0 |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 48 | pager := setupSinglePaged() |
| 49 | defer testhelper.TeardownHTTP() |
| 50 | |
| 51 | err := pager.EachPage(func(page Page) (bool, error) { |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 52 | callCount++ |
| 53 | |
| 54 | expected := []int{1, 2, 3} |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 55 | actual, err := ExtractSingleInts(page) |
| 56 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 57 | return false, err |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 58 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 59 | if !reflect.DeepEqual(expected, actual) { |
| 60 | t.Errorf("Expected %v, but was %v", expected, actual) |
| 61 | } |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 62 | return true, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 63 | }) |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 64 | if err != nil { |
| 65 | t.Fatalf("Unexpected error calling EachPage: %v", err) |
| 66 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 67 | |
| 68 | if callCount != 1 { |
| 69 | t.Errorf("Callback was invoked %d times", callCount) |
| 70 | } |
| 71 | } |
| 72 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 73 | // LinkedPager sample and test cases. |
| 74 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 75 | func ExtractLinkedInts(page Page) ([]int, error) { |
| 76 | var response struct { |
| 77 | Ints []int `mapstructure:"ints"` |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 78 | } |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 79 | |
Ash Wilson | 583dc73 | 2014-09-12 13:30:05 -0400 | [diff] [blame] | 80 | err := mapstructure.Decode(page.(LinkedPage).Body, &response) |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 81 | if err != nil { |
| 82 | return nil, err |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 83 | } |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 84 | |
| 85 | return response.Ints, nil |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 88 | func createLinked(t *testing.T) Pager { |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 89 | testhelper.SetupHTTP() |
| 90 | |
| 91 | testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) { |
| 92 | fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, testhelper.Server.URL) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 93 | }) |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 94 | |
| 95 | testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) { |
| 96 | fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL) |
| 97 | }) |
| 98 | |
| 99 | testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) { |
| 100 | fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`) |
| 101 | }) |
| 102 | |
| 103 | client := createClient() |
| 104 | |
| 105 | return NewLinkedPager(client, testhelper.Server.URL+"/page1") |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | func TestEnumerateLinked(t *testing.T) { |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 109 | pager := createLinked(t) |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 110 | defer testhelper.TeardownHTTP() |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 111 | |
| 112 | callCount := 0 |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 113 | err := pager.EachPage(func(page Page) (bool, error) { |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 114 | actual, err := ExtractLinkedInts(page) |
| 115 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 116 | return false, err |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 117 | } |
| 118 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 119 | t.Logf("Handler invoked with %v", actual) |
| 120 | |
| 121 | var expected []int |
| 122 | switch callCount { |
| 123 | case 0: |
| 124 | expected = []int{1, 2, 3} |
| 125 | case 1: |
| 126 | expected = []int{4, 5, 6} |
| 127 | case 2: |
| 128 | expected = []int{7, 8, 9} |
| 129 | default: |
| 130 | t.Fatalf("Unexpected call count: %d", callCount) |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 131 | return false, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | if !reflect.DeepEqual(expected, actual) { |
| 135 | t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual) |
| 136 | } |
| 137 | |
| 138 | callCount++ |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 139 | return true, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 140 | }) |
| 141 | if err != nil { |
| 142 | t.Errorf("Unexpected error for page iteration: %v", err) |
| 143 | } |
| 144 | |
| 145 | if callCount != 3 { |
| 146 | t.Errorf("Expected 3 calls, but was %d", callCount) |
| 147 | } |
| 148 | } |