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 | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 23 | type SinglePageResult struct { |
| 24 | SinglePageBase |
| 25 | } |
| 26 | |
| 27 | func (r SinglePageResult) IsEmpty() (bool, error) { |
| 28 | is, err := ExtractSingleInts(r) |
| 29 | if err != nil { |
| 30 | return true, err |
| 31 | } |
| 32 | return len(is) == 0, nil |
| 33 | } |
| 34 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 35 | func ExtractSingleInts(page Page) ([]int, error) { |
| 36 | var response struct { |
| 37 | Ints []int `mapstructure:"ints"` |
| 38 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 39 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 40 | err := mapstructure.Decode(page.(SinglePageResult).Body, &response) |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 44 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 45 | return response.Ints, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 48 | func setupSinglePaged() Pager { |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 49 | testhelper.SetupHTTP() |
| 50 | client := createClient() |
| 51 | |
| 52 | testhelper.Mux.HandleFunc("/only", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame] | 53 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 54 | fmt.Fprintf(w, `{ "ints": [1, 2, 3] }`) |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 55 | }) |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 56 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 57 | createPage := func(r LastHTTPResponse) Page { |
| 58 | return SinglePageResult{SinglePageBase(r)} |
Ash Wilson | 5a25f54 | 2014-09-15 15:43:20 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 61 | return NewSinglePager(client, testhelper.Server.URL+"/only", createPage) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | func TestEnumerateSinglePaged(t *testing.T) { |
| 65 | callCount := 0 |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 66 | pager := setupSinglePaged() |
| 67 | defer testhelper.TeardownHTTP() |
| 68 | |
| 69 | err := pager.EachPage(func(page Page) (bool, error) { |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 70 | callCount++ |
| 71 | |
| 72 | expected := []int{1, 2, 3} |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 73 | actual, err := ExtractSingleInts(page) |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 74 | testhelper.AssertNoErr(t, err) |
| 75 | testhelper.CheckDeepEquals(t, expected, actual) |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 76 | return true, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 77 | }) |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 78 | testhelper.CheckNoErr(t, err) |
| 79 | testhelper.CheckEquals(t, 1, callCount) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 82 | // LinkedPager sample and test cases. |
| 83 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 84 | type LinkedPageResult struct { |
| 85 | LinkedPageBase |
| 86 | } |
| 87 | |
| 88 | func (r LinkedPageResult) IsEmpty() (bool, error) { |
| 89 | is, err := ExtractLinkedInts(r) |
| 90 | if err != nil { |
| 91 | return true, nil |
| 92 | } |
| 93 | return len(is) == 0, nil |
| 94 | } |
| 95 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 96 | func ExtractLinkedInts(page Page) ([]int, error) { |
| 97 | var response struct { |
| 98 | Ints []int `mapstructure:"ints"` |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 99 | } |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 100 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 101 | err := mapstructure.Decode(page.(LinkedPageResult).Body, &response) |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 102 | if err != nil { |
| 103 | return nil, err |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 104 | } |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 105 | |
| 106 | return response.Ints, nil |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 109 | func createLinked(t *testing.T) Pager { |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 110 | testhelper.SetupHTTP() |
| 111 | |
| 112 | testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame] | 113 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 114 | 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] | 115 | }) |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 116 | |
| 117 | testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame] | 118 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 119 | fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL) |
| 120 | }) |
| 121 | |
| 122 | testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame] | 123 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 124 | fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`) |
| 125 | }) |
| 126 | |
| 127 | client := createClient() |
| 128 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 129 | createPage := func(r LastHTTPResponse) Page { |
| 130 | return LinkedPageResult{LinkedPageBase(r)} |
Ash Wilson | 5a25f54 | 2014-09-15 15:43:20 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 133 | return NewLinkedPager(client, testhelper.Server.URL+"/page1", createPage) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | func TestEnumerateLinked(t *testing.T) { |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 137 | pager := createLinked(t) |
Ash Wilson | 4bf4fac | 2014-09-15 09:16:13 -0400 | [diff] [blame] | 138 | defer testhelper.TeardownHTTP() |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 139 | |
| 140 | callCount := 0 |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 141 | err := pager.EachPage(func(page Page) (bool, error) { |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 142 | actual, err := ExtractLinkedInts(page) |
| 143 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 144 | return false, err |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 147 | t.Logf("Handler invoked with %v", actual) |
| 148 | |
| 149 | var expected []int |
| 150 | switch callCount { |
| 151 | case 0: |
| 152 | expected = []int{1, 2, 3} |
| 153 | case 1: |
| 154 | expected = []int{4, 5, 6} |
| 155 | case 2: |
| 156 | expected = []int{7, 8, 9} |
| 157 | default: |
| 158 | t.Fatalf("Unexpected call count: %d", callCount) |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 159 | return false, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | if !reflect.DeepEqual(expected, actual) { |
| 163 | t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual) |
| 164 | } |
| 165 | |
| 166 | callCount++ |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 167 | return true, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 168 | }) |
| 169 | if err != nil { |
| 170 | t.Errorf("Unexpected error for page iteration: %v", err) |
| 171 | } |
| 172 | |
| 173 | if callCount != 3 { |
| 174 | t.Errorf("Expected 3 calls, but was %d", callCount) |
| 175 | } |
| 176 | } |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame] | 177 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 178 | // MarkerPager sample and test cases. |
| 179 | |
| 180 | type MarkerPageResult struct { |
| 181 | MarkerPageBase |
| 182 | } |
| 183 | |
| 184 | func (r MarkerPageResult) IsEmpty() (bool, error) { |
| 185 | results, err := ExtractMarkerStrings(r) |
| 186 | if err != nil { |
| 187 | return true, err |
| 188 | } |
| 189 | return len(results) == 0, err |
| 190 | } |
| 191 | |
| 192 | func (r MarkerPageResult) LastMark() (string, error) { |
| 193 | results, err := ExtractMarkerStrings(r) |
| 194 | if err != nil { |
| 195 | return "", err |
| 196 | } |
| 197 | if len(results) == 0 { |
| 198 | return "", nil |
| 199 | } |
| 200 | return results[len(results)-1], nil |
| 201 | } |
| 202 | |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame] | 203 | func createMarkerPaged(t *testing.T) Pager { |
| 204 | testhelper.SetupHTTP() |
| 205 | |
| 206 | testhelper.Mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) { |
| 207 | r.ParseForm() |
| 208 | ms := r.Form["marker"] |
| 209 | switch { |
| 210 | case len(ms) == 0: |
| 211 | fmt.Fprintf(w, "aaa\nbbb\nccc") |
| 212 | case len(ms) == 1 && ms[0] == "ccc": |
| 213 | fmt.Fprintf(w, "ddd\neee\nfff") |
| 214 | case len(ms) == 1 && ms[0] == "fff": |
| 215 | fmt.Fprintf(w, "ggg\nhhh\niii") |
| 216 | case len(ms) == 1 && ms[0] == "iii": |
| 217 | w.WriteHeader(http.StatusNoContent) |
| 218 | default: |
| 219 | t.Errorf("Request with unexpected marker: [%v]", ms) |
| 220 | } |
| 221 | }) |
| 222 | |
| 223 | client := createClient() |
| 224 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 225 | createPage := func(r LastHTTPResponse) MarkerPage { |
| 226 | p := MarkerPageResult{MarkerPageBase{LastHTTPResponse: r}} |
| 227 | p.MarkerPageBase.Self = p |
| 228 | return p |
Ash Wilson | 5a25f54 | 2014-09-15 15:43:20 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 231 | return NewMarkerPager(client, testhelper.Server.URL+"/page", createPage) |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | func ExtractMarkerStrings(page Page) ([]string, error) { |
Ash Wilson | c93fde7 | 2014-09-16 10:06:22 -0400 | [diff] [blame^] | 235 | content := page.(MarkerPageResult).Body.([]uint8) |
Ash Wilson | 5a25f54 | 2014-09-15 15:43:20 -0400 | [diff] [blame] | 236 | parts := strings.Split(string(content), "\n") |
| 237 | results := make([]string, 0, len(parts)) |
| 238 | for _, part := range parts { |
| 239 | if len(part) > 0 { |
| 240 | results = append(results, part) |
| 241 | } |
| 242 | } |
| 243 | return results, nil |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | func TestEnumerateMarker(t *testing.T) { |
| 247 | pager := createMarkerPaged(t) |
| 248 | defer testhelper.TeardownHTTP() |
| 249 | |
| 250 | callCount := 0 |
| 251 | err := pager.EachPage(func(page Page) (bool, error) { |
| 252 | actual, err := ExtractMarkerStrings(page) |
| 253 | if err != nil { |
| 254 | return false, err |
| 255 | } |
| 256 | |
| 257 | t.Logf("Handler invoked with %v", actual) |
| 258 | |
| 259 | var expected []string |
| 260 | switch callCount { |
| 261 | case 0: |
| 262 | expected = []string{"aaa", "bbb", "ccc"} |
| 263 | case 1: |
| 264 | expected = []string{"ddd", "eee", "fff"} |
| 265 | case 2: |
| 266 | expected = []string{"ggg", "hhh", "iii"} |
| 267 | default: |
| 268 | t.Fatalf("Unexpected call count: %d", callCount) |
| 269 | return false, nil |
| 270 | } |
| 271 | |
| 272 | testhelper.CheckDeepEquals(t, expected, actual) |
| 273 | |
| 274 | callCount++ |
| 275 | return true, nil |
| 276 | }) |
| 277 | testhelper.AssertNoErr(t, err) |
| 278 | testhelper.AssertEquals(t, callCount, 3) |
| 279 | } |