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" |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 7 | "strings" |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 8 | "testing" |
| 9 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 10 | "github.com/mitchellh/mapstructure" |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/testhelper" |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 12 | ) |
| 13 | |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 14 | func createClient() *ServiceClient { |
| 15 | return &ServiceClient{ |
| 16 | Provider: &ProviderClient{TokenID: "abc123"}, |
| 17 | Endpoint: testhelper.Endpoint(), |
| 18 | } |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 19 | } |
| 20 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 21 | // SinglePage sample and test cases. |
| 22 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 23 | func ExtractSingleInts(page Page) ([]int, error) { |
| 24 | var response struct { |
| 25 | Ints []int `mapstructure:"ints"` |
| 26 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 27 | |
Ash Wilson | 75eae57 | 2014-09-12 12:34:21 -0400 | [diff] [blame] | 28 | err := mapstructure.Decode(page.(SinglePage).Body, &response) |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 32 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 33 | return response.Ints, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 34 | } |
| 35 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 36 | func setupSinglePaged() Pager { |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 37 | testhelper.SetupHTTP() |
| 38 | client := createClient() |
| 39 | |
| 40 | testhelper.Mux.HandleFunc("/only", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 41 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 42 | fmt.Fprintf(w, `{ "ints": [1, 2, 3] }`) |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 43 | }) |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 44 | |
| 45 | return NewSinglePager(client, testhelper.Server.URL+"/only") |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | func TestEnumerateSinglePaged(t *testing.T) { |
| 49 | callCount := 0 |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 50 | pager := setupSinglePaged() |
| 51 | defer testhelper.TeardownHTTP() |
| 52 | |
| 53 | err := pager.EachPage(func(page Page) (bool, error) { |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 54 | callCount++ |
| 55 | |
| 56 | expected := []int{1, 2, 3} |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 57 | actual, err := ExtractSingleInts(page) |
| 58 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 59 | return false, err |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 60 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 61 | if !reflect.DeepEqual(expected, actual) { |
| 62 | t.Errorf("Expected %v, but was %v", expected, actual) |
| 63 | } |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 64 | return true, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 65 | }) |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 66 | if err != nil { |
| 67 | t.Fatalf("Unexpected error calling EachPage: %v", err) |
| 68 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 69 | |
| 70 | if callCount != 1 { |
| 71 | t.Errorf("Callback was invoked %d times", callCount) |
| 72 | } |
| 73 | } |
| 74 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 75 | // LinkedPager sample and test cases. |
| 76 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 77 | func ExtractLinkedInts(page Page) ([]int, error) { |
| 78 | var response struct { |
| 79 | Ints []int `mapstructure:"ints"` |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 80 | } |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 81 | |
Ash Wilson | 583dc73 | 2014-09-12 13:30:05 -0400 | [diff] [blame] | 82 | err := mapstructure.Decode(page.(LinkedPage).Body, &response) |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 83 | if err != nil { |
| 84 | return nil, err |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 85 | } |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 86 | |
| 87 | return response.Ints, nil |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 90 | func createLinked(t *testing.T) Pager { |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 91 | testhelper.SetupHTTP() |
| 92 | |
| 93 | testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 94 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 95 | 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] | 96 | }) |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 97 | |
| 98 | testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 99 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 100 | fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL) |
| 101 | }) |
| 102 | |
| 103 | testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 104 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 105 | fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`) |
| 106 | }) |
| 107 | |
| 108 | client := createClient() |
| 109 | |
| 110 | return NewLinkedPager(client, testhelper.Server.URL+"/page1") |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | func TestEnumerateLinked(t *testing.T) { |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 114 | pager := createLinked(t) |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 115 | defer testhelper.TeardownHTTP() |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 116 | |
| 117 | callCount := 0 |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 118 | err := pager.EachPage(func(page Page) (bool, error) { |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 119 | actual, err := ExtractLinkedInts(page) |
| 120 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 121 | return false, err |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 124 | t.Logf("Handler invoked with %v", actual) |
| 125 | |
| 126 | var expected []int |
| 127 | switch callCount { |
| 128 | case 0: |
| 129 | expected = []int{1, 2, 3} |
| 130 | case 1: |
| 131 | expected = []int{4, 5, 6} |
| 132 | case 2: |
| 133 | expected = []int{7, 8, 9} |
| 134 | default: |
| 135 | t.Fatalf("Unexpected call count: %d", callCount) |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 136 | return false, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if !reflect.DeepEqual(expected, actual) { |
| 140 | t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual) |
| 141 | } |
| 142 | |
| 143 | callCount++ |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 144 | return true, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 145 | }) |
| 146 | if err != nil { |
| 147 | t.Errorf("Unexpected error for page iteration: %v", err) |
| 148 | } |
| 149 | |
| 150 | if callCount != 3 { |
| 151 | t.Errorf("Expected 3 calls, but was %d", callCount) |
| 152 | } |
| 153 | } |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 154 | |
| 155 | func createMarkerPaged(t *testing.T) Pager { |
| 156 | testhelper.SetupHTTP() |
| 157 | |
| 158 | testhelper.Mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) { |
| 159 | r.ParseForm() |
| 160 | ms := r.Form["marker"] |
| 161 | switch { |
| 162 | case len(ms) == 0: |
| 163 | fmt.Fprintf(w, "aaa\nbbb\nccc") |
| 164 | case len(ms) == 1 && ms[0] == "ccc": |
| 165 | fmt.Fprintf(w, "ddd\neee\nfff") |
| 166 | case len(ms) == 1 && ms[0] == "fff": |
| 167 | fmt.Fprintf(w, "ggg\nhhh\niii") |
| 168 | case len(ms) == 1 && ms[0] == "iii": |
| 169 | w.WriteHeader(http.StatusNoContent) |
| 170 | default: |
| 171 | t.Errorf("Request with unexpected marker: [%v]", ms) |
| 172 | } |
| 173 | }) |
| 174 | |
| 175 | client := createClient() |
| 176 | |
| 177 | return NewMarkerPager(client, testhelper.Server.URL+"/page", func(p MarkerPage) (string, error) { |
| 178 | items, err := ExtractMarkerStrings(p) |
| 179 | if err != nil { |
| 180 | return "", err |
| 181 | } |
| 182 | return items[len(items)-1], nil |
| 183 | }) |
| 184 | } |
| 185 | |
| 186 | func ExtractMarkerStrings(page Page) ([]string, error) { |
| 187 | content := page.(MarkerPage).Body.([]uint8) |
| 188 | return strings.Split(string(content), "\n"), nil |
| 189 | } |
| 190 | |
| 191 | func TestEnumerateMarker(t *testing.T) { |
| 192 | pager := createMarkerPaged(t) |
| 193 | defer testhelper.TeardownHTTP() |
| 194 | |
| 195 | callCount := 0 |
| 196 | err := pager.EachPage(func(page Page) (bool, error) { |
| 197 | actual, err := ExtractMarkerStrings(page) |
| 198 | if err != nil { |
| 199 | return false, err |
| 200 | } |
| 201 | |
| 202 | t.Logf("Handler invoked with %v", actual) |
| 203 | |
| 204 | var expected []string |
| 205 | switch callCount { |
| 206 | case 0: |
| 207 | expected = []string{"aaa", "bbb", "ccc"} |
| 208 | case 1: |
| 209 | expected = []string{"ddd", "eee", "fff"} |
| 210 | case 2: |
| 211 | expected = []string{"ggg", "hhh", "iii"} |
| 212 | default: |
| 213 | t.Fatalf("Unexpected call count: %d", callCount) |
| 214 | return false, nil |
| 215 | } |
| 216 | |
| 217 | testhelper.CheckDeepEquals(t, expected, actual) |
| 218 | |
| 219 | callCount++ |
| 220 | return true, nil |
| 221 | }) |
| 222 | testhelper.AssertNoErr(t, err) |
| 223 | testhelper.AssertEquals(t, callCount, 3) |
| 224 | } |