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